【问题标题】:Tell Mocha to use CoffeeScript files by default告诉 Mocha 默认使用 CoffeeScript 文件
【发布时间】:2013-04-28 16:24:24
【问题描述】:

我目前正在尝试在 Mocha 中为我正在使用 Zappa.js 编写的应用程序设置测试。到目前为止,我一直在关注this tutorial,并将我需要的内容从 JS 转换为 Coffeescript。

但是,我在尝试运行测试时有点卡住了。我有一个 Makefile,目前看起来像这样:

REPORTER = dot

test:
  @NODE_ENV=test ./node_modules/.bin/mocha \
    --reporter $(REPORTER) \

.PHONY: test

我已经设置了我的 package.json 文件来运行这样的测试:

{
  "scripts": {
    "test": "make test"
  }
}

我发现的问题是,因为我也在尝试使用 Coffeescript 编写 Mocha 测试,所以当我运行“npm test”时,Mocha 不会在“test/”文件夹中提取任何测试.我知道我可以通过在终端中使用以下命令来告诉 Mocha 运行 .coffee 文件(有效):

mocha --compilers coffee:coffee-script

我想知道的是如何告诉 Mocha 默认使用 Coffeescript 文件?

【问题讨论】:

    标签: node.js makefile mocha.js zappa


    【解决方案1】:

    好的,我设法找到了解决我自己问题的方法,所以我想我会分享一下,以防其他人需要这个。

    注意:对于 CoffeeScript 1.7+ --require coffee-script 需要更改为 --require coffee-script/register

    解决方案是创建一个 Cakefile 而不是 Makefile,如下所示:

    #Cakefile
    
    {exec} = require "child_process"
    
    REPORTER = "min"
    
    task "test", "run tests", ->
      exec "NODE_ENV=test
        ./node_modules/.bin/mocha
        --compilers coffee:coffee-script
        --reporter #{REPORTER}
        --require coffee-script
        --require test/test_helper.coffee
        --colors
        ", (err, output) ->
          throw err if err
          console.log output
    

    然后把package.json改成这样:

    #package.json
    
    {
      "scripts": {
        "test": "cake test"
      }
    }
    

    最后我不得不使用以下方法将 Coffeescript 安装到项目中:

    npm install coffee-script
    

    并创建一个文件 test/test_helper.coffee,其中包含测试的全局声明。

    【讨论】:

    • 如果你希望贡献者能够执行你的测试,你会想要npm install coffee-script --save-dev
    • 对于 CoffeeScript 1.7+ --require coffee-script 需要更改为 --require coffee-script/register stackoverflow.com/a/9943255/167815
    【解决方案2】:

    我直接使用 npm 配置 mocha 测试

    package.json(仅限脚本)

    "scripts": {
      "start": "node app.js",
      "start-watch": "./node_modules/.bin/node-dev app.js",
      "test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test",
      "test-watch": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive ./test --watch"
    }
    

    然后通过运行来执行测试咖啡脚本文件

    npm test
    

    npm run-script test-watch
    

    【讨论】:

    • 确实在使用--require coffee-script/register时工作
    【解决方案3】:

    下面是一个有效的 Makefilepackage.json

    生成文件:

    REPORTER = dot
    COMPILER = coffee:coffee-script
    
    node_modules:
        @npm install
    
    test: node_modules
        @./node_modules/.bin/mocha --reporter $(REPORTER) --compilers $(COMPILER)
    
    clean: node_modules
        @$(RM) -r node_modules
    
    .PHONY: clean test
    

    package.json(仅限 devDependencies):

      "devDependencies": {
        "coffee-script": "~1.6.3",
        "chai": "~1.7.2",
        "mocha": "~1.12.0"
      }
    

    然后做:

    % make clean
    % make test
    

    【讨论】:

      猜你喜欢
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2019-04-02
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多