![](/img/trans.png)
[英]Coffeescript unit test failing with Mocha, Should, Node.js
[英]How to make mocha watch, compile and test coffeescript with dependencies on save
我正在開發一個使用coffeescript進行開發和測試的項目。 我在節點中使用mocha的--watch標志運行測試,這樣我可以在進行更改時自動運行測試。
雖然這在某種程度上起作用,但只有./test/test.*.coffee文件在保存時才會重新編譯。 這是我的目錄結構:
/src/coffee
-- # Dev files go here
/test/
-- # Test files go here
mocha觀察者響應/ src和/ test目錄中的文件更改,但只要重新編譯/ test目錄中的文件,連續測試就會受到嚴重影響。 如果我退出並重新啟動觀察程序進程,則還會重新編譯源文件。 我如何讓mocha讓咖啡編譯器在每次運行時運行在測試文件中作為依賴項列出的開發文件?
這是我使用grunt.js的答案
你將不得不安裝grunt和幾個額外的包。
npm install grunt grunt-contrib-coffee grunt-simple-mocha grunt-contrib-watch
並寫這個grunt.js文件:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.initConfig({
coffee:{
dev:{
files:{
'src/*.js':'src/coffee/*.coffee',
}
},
test:{
files:{
'test/test.*.js':'test/test.*.coffee'
}
}
},
simplemocha:{
dev:{
src:"test/test.js",
options:{
reporter: 'spec',
slow: 200,
timeout: 1000
}
}
},
watch:{
all:{
files:['src/coffee/*', 'test/*.coffee'],
tasks:['buildDev', 'buildTest', 'test']
}
}
});
grunt.registerTask('test', 'simplemocha:dev');
grunt.registerTask('buildDev', 'coffee:dev');
grunt.registerTask('buildTest', 'coffee:test');
grunt.registerTask('watch', ['buildDev', 'buildTest', 'test', 'watch:all']);
};
注意:我沒有關於你如何構建/運行你的測試的一些detials,所以你當然必須addapt;)
然后運行grunt watch任務:
$>grunt watch
使用帶面粉的Cakefile:
flour = require 'flour'
cp = require 'child_process'
task 'build', ->
bundle 'src/coffee/*.coffee', 'lib/project.js'
task 'watch', ->
invoke 'build'
watch 'src/coffee/', -> invoke 'build'
task 'test', ->
invoke 'watch'
cp.spawn 'mocha --watch', [], {stdio: 'inherit'}
Mocha已經在觀看test/
文件夾,因此您只需要觀看src/
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.