Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use different babel preset when running mocha tests

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?

like image 907
Matthias Simon Avatar asked Dec 11 '22 11:12

Matthias Simon


2 Answers

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"
like image 63
Mark McKelvy Avatar answered Dec 12 '22 23:12

Mark McKelvy


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.

like image 34
Louis Avatar answered Dec 12 '22 23:12

Louis