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?
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With