Unit testing 如何让mocha观看、编译和测试依赖于save的coffeescript

Unit testing 如何让mocha观看、编译和测试依赖于save的coffeescript,unit-testing,node.js,tdd,coffeescript,mocha.js,Unit Testing,Node.js,Tdd,Coffeescript,Mocha.js,我正在做一个使用coffeescript进行开发和测试的项目。我在打开mocha的--watch标志的情况下在节点中运行测试,以便在进行更改时自动运行测试 虽然这在某种程度上是可行的,但在保存某些内容时,只会重新编译./test/test.*.coffee文件。这是我的目录结构: /src/coffee -- # Dev files go here /test/ -- # Test files go here mocha watcher对/src和/test目录中的文件更改做出响应,但只要只重

我正在做一个使用coffeescript进行开发和测试的项目。我在打开mocha的--watch标志的情况下在节点中运行测试,以便在进行更改时自动运行测试

虽然这在某种程度上是可行的,但在保存某些内容时,只会重新编译./test/test.*.coffee文件。这是我的目录结构:

/src/coffee
-- # Dev files go here
/test/
-- # Test files go here

mocha watcher对/src和/test目录中的文件更改做出响应,但只要只重新编译/test目录中的文件,连续测试就有点失败。如果退出并重新启动watcher进程,源文件也会重新编译。如何让mocha让coffee编译器在每次运行时都在测试文件中作为依赖项列出的开发文件上运行?

下面是我使用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']);

};
注意:我没有关于如何构建/运行测试的详细信息,因此您必须添加apt;)

然后运行grunt监视任务:

$>grunt watch
将Cakefile用于:


Mocha已经监视了
test/
文件夹,所以您只需要监视
src/

就好像您需要一个构建工具。看一看或者谢谢。尝试了使用simplemocha的Grunt,但是我在使用simplemocha时遇到了一般的JS错误。现在我要试试面粉。我正在编写一个基于grunt.js的anwser,它很容易实现,但我不确定你如何将文件编译成js,以及你是否使用js或coffee运行测试。非常感谢你的回答!有没有可能只用一个Makefile就可以做到这一点?安装grunt simple mocha时可能会出现问题,它已经在使用grunt的grunt v0.4,而npm目前仍有grunt的v0.3。所以我检查了grunt simple mocha的v0.2,使其与npm的old grunt一起工作
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'}