【问题标题】:how to pass compiler options to mocha如何将编译器选项传递给 mocha
【发布时间】:2013-12-01 17:18:57
【问题描述】:

我运行 mocha 命令来运行我的测试

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec

我希望将其他选项传递给咖啡脚本编译器(--bare 以避免将 .coffee 编译为 .js 时引入的外部闭包)。有没有办法做到这一点?我试过了

$ ./node_modules/.bin/mocha --compilers coffee:coffee-script --bare -R spec

但这看起来不对。它也没有说 --bare 不是 mocha 的有效选项。

  error: unknown option `--bare'

【问题讨论】:

    标签: coffeescript mocha.js


    【解决方案1】:

    --compiler 选项不支持此功能,但您可以编写一个脚本来使用您的选项激活编译器,然后使用 mocha 的 --require 选项来激活您的注册脚本。例如,在项目的根目录下创建一个名为 babelhook.js 的文件:

    // This file is required in mocha.opts
    // The only purpose of this file is to ensure
    // the babel transpiler is activated prior to any
    // test code, and using the same babel options
    
    require("babel-register")({
      experimental: true
    });
    

    然后将其添加到 mocha.opts:

    --require babelhook
    

    就是这样。 Mocha 在任何测试之前都需要 babelhook.js。

    【讨论】:

    • 不是 babel-register 吗?
    • @goldylucks 我相信babel/register 是 Babel v5,babel-register Babel v6。
    【解决方案2】:

    只需将.babelrc 文件添加到您的根目录即可。 然后 babel 的任何实例(构建、运行时、测试等)都会引用它。 https://babeljs.io/docs/usage/babelrc/

    您甚至可以为每个环境添加特定的配置选项。

    【讨论】:

    • 这似乎是最好的解决方案
    • 嗯,我想我不能通过 .babelrc 修改 babel-register 的“忽略”选项,但是如果我使用 babelhook 方式,那么它可以工作。
    • ignore 选项目前在.babelrc 中似乎不起作用phabricator.babeljs.io/T6726
    【解决方案3】:

    以防万一有人偶然发现这一点。 babel 中的 'experimental' 选项已被弃用。你的“babelhook.js”现在应该是:

    // This file is required in mocha.opts
    // The only purpose of this file is to ensure
    // the babel transpiler is activated prior to any
    // test code, and using the same babel options
    
    require("babel/register")({
      stage: 1
    });
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2013-06-29
      • 2020-06-27
      • 2017-11-01
      相关资源
      最近更新 更多