![](/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.