Using webpack aliases in mocha tests
Okay so I realized that everything I was aliasing was in the src/
directory, so I simply needed to modify my npm run test
script.
{
"scripts": {
"test": "NODE_PATH=./src mocha ./src/**/test/spec.js --compilers js:babel-core/register --recursive"
}
}
Probably won't work for everyone, but that solved my issue.
You can also use a babel plugin I authored:
https://github.com/trayio/babel-plugin-webpack-alias
It will convert your aliased path to relative paths just by including a babel plugin to your .babelrc
.
I also encountered the same problem, but with this plugin I solved it.
https://www.npmjs.com/package/babel-plugin-webpack-aliases
The execution command of your "mocha" is not reading the webpack.config.js
, so it can not resolve the alias.
By setting this plugin, consider webpack.config.js
when compiling with "babel-core/register". As a result, the alias will also be valid during testing.
npm i -D babel-plugin-webpack-aliases
and add this setting to .babelrc
{
"plugins": [
[ "babel-plugin-webpack-aliases", { "config": "./webpack.config.js" } ]
]
}