我想用webpack捆绑测试到一个包,我可以直接传递给摩卡。我的WebPack配置看起来是这样的:需要测试文件与webpack用于摩卡或类似
module.exports = {
entry: ...,
output: ...,
module: {
rules: [
{
test: /\.jsx?(.erb)?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
'react',
[ 'latest', { 'es2015': { 'modules': false } } ]
]
}
}
]
},
plugins: [],
resolve: {
extensions: [ '.js', '.jsx' ],
modules: [
path.resolve('../app/javascript'),
path.resolve('../vendor/node_modules')
]
},
resolveLoader: {
modules: [ path.resolve('../vendor/node_modules') ]
}
}
在其他地方(从https://github.com/rails/webpacker)
我的切入点是这样的,这是我见过的引用:
var context = require.context('../path/to/tests', true, /.+\.test\.js?$/);
context.keys().forEach(context);
module.exports = context;
现在这个工程,并产生一束,说tests.js,我可以传递给摩卡:
$ mocha tests.js
但是,这使得webpack在每次更改时重新编译所有测试文件,这非常慢。对于我的应用程序代码,在使用常规导入语句导入模块的情况下,webpack仅重新编译已更改的文件。
更改我的切入点是这样的:
require('../path/to/tests/foo.test.js');
require('../path/to/tests/bar.test.js');
require('../path/to/tests/baz.test.js');
...
似乎收到预期的效果,但也有数以百计的测试文件,它似乎很麻烦有手动导入或要求每一个。