use different babel preset when running mocha tests

我的未来我决定 提交于 2019-12-14 01:01:09

问题


My npm package build runs with babel and I configured a babel preset in my package.json with

"babel": { "presets": ["es2015"] }

I also configured a mocha test script with

"test": "mocha --compilers js:babel-core/register"

However, I would like to run my tests using a different babel preset than that one specified for my build.

Is it possible? I would you achieve that?


回答1:


You could create a file named babel-hook.js and put in it:

require("babel-register")({
  presets: [ /* whatever values you want here */ ],
});

then run Mocha like this:

mocha --require babel-hook

This will register Babel and you can use any configuration option you want with it, separate from anything in package.json.




回答2:


Babel accommodates environment variables so you could set a test environment variable and alter your presets accordingly:

In your package.json:

"babel": {
    "env": {
      "test": {
        "presets": [/* your test presets */]
      }
    },
    "presets": [/* your usual presets */]
}

Then, run your mocha command like so:

"test: BABEL_ENV=test mocha --compilers js:babel-core/register"


来源:https://stackoverflow.com/questions/43883973/use-different-babel-preset-when-running-mocha-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!