Search code examples
javascriptnode.jsgulpmocha.jsbabeljs

How to pass Babel7 config file path to mocha and gulp-mocha?


In Babel7 we have babel.config.js file. The path to this file we can pass via configFile or config-file argument, as I see in internet. However, I couldn't make mocha and gulp-mocha read babel.config.js.

This the way I run mocha:

./node_modules/.bin/mocha ./foo.spec.js --compilers js:@babel/register

This is the way I run gulpMocha

pipe(gulpMocha({
        reporter: 'spec',
        require: [
            '@babel/preset-env',
        ]
}))

How to pass Babel7 config file path to mocha and gulp-mocha?

If it is not possible for both of them, then, at least for one.


Solution

  • The solution was suggested here. For example, if project structure is like this:

    Project
    |-module1
    |-module2
    |-babel.config.js
    

    And we want to test module1. We create register.js file in Project/module1 with the following content:

    require("@babel/register")({
      rootMode: "upward"
    });
    

    After that in Project/module1 we run mocha this way: mocha --require register.js and babel.config.js will be found by babel.