Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any approach to inject or ignore global variables when babel compiling?

There is a __DEVTOOLS__ global variable in my source code. And normally I use webpack DefinePlugin to define it as boolean value.

new webpack.DefinePlugin({
    __DEVTOOLS__: true
})

But in my mocha test I only want to use babel/register and don't want to use wepack in my test. Is there any approach to inject global variables just like webpack DefinePlugin or at least ignore the global variables when it compile?

like image 233
Chen-Tsu Lin Avatar asked Aug 18 '15 03:08

Chen-Tsu Lin


2 Answers

I recently ran into this issue and adding global.__DEVTOOLS__ = true; to a before block, or any where in a test, did not work for me. I ended up solving this be creating a separate script that is passed to mocha cli via the --require flag.

// config/mocha-setup.js

require('babel-register');

global.__DEVTOOLS__ = true;

Then configure the test command:

// package.json

scripts: {
  test: mocha --require ./config/mocha-setup
}
like image 187
Brandon Avatar answered Nov 08 '22 20:11

Brandon


With Mocha you can assign properties to the global object, which I think will achieve what you want:

global.__DEVTOOLS__ = true;

You can put this wherever you want, e.g. a before block, but if you already have a helper.js or setup.js file that you load before your tests, that would be a good place to put it.

like image 6
Jordan Running Avatar answered Nov 08 '22 20:11

Jordan Running