简体   繁体   English

如何使用mocha监视,编译和测试依赖于save的coffeescript

[英]How to make mocha watch, compile and test coffeescript with dependencies on save

I'm working on a project that uses coffeescript for development and testing. 我正在开发一个使用coffeescript进行开发和测试的项目。 I run the tests in node with mocha's --watch flag on so I can have the tests run automatically when I make changes. 我在节点中使用mocha的--watch标志运行测试,这样我可以在进行更改时自动运行测试。

While this works to some extent, only the ./test/test.*.coffee files are recompiled when something is saved. 虽然这在某种程度上起作用,但只有./test/test.*.coffee文件在保存时才会重新编译。 This is my directory structure: 这是我的目录结构:

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

The mocha watcher responds to file changes inside the /src and /test directories, but as long as only the files in the /test directory are recompiled continuous testing is kind of borked. mocha观察者响应/ src和/ test目录中的文件更改,但只要重新编译/ test目录中的文件,连续测试就会受到严重影响。 If I quit and restart the watcher process the source files are also recompiled. 如果我退出并重新启动观察程序进程,则还会重新编译源文件。 How can I make mocha have the coffee compiler run over the development files listed as dependencies inside the test files on each run? 我如何让mocha让咖啡编译器在每次运行时运行在测试文件中作为依赖项列出的开发文件?

Here is my answer using grunt.js 这是我使用grunt.js的答案

You will have to install grunt and few additionnal packges. 你将不得不安装grunt和几个额外的包。

npm install grunt grunt-contrib-coffee grunt-simple-mocha grunt-contrib-watch

And write this grunt.js file: 并写这个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']);

};

Note: I didn't have some detials on how you build / run your tests so you certainly have to addapt ;) 注意:我没有关于你如何构建/运行你的测试的一些detials,所以你当然必须addapt;)

Then run the grunt watch task : 然后运行grunt watch任务:

$>grunt watch

Using a Cakefile with flour : 使用带面粉的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 already watches the test/ folder, so you only need to watch src/ . Mocha已经在观看test/文件夹,因此您只需要观看src/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM