时间过得真快,2018 年 6 月已经过去了。由于网上没有太多关于此的文档,我想捐出我的 2 美分。
我目前有一个设置工作,我将我的测试与 webpack 捆绑在一起,监视更改以便自动重新运行测试。
我正在使用karma-webpack 使用Alternative usage 部分中解释的配置,我认为这是解决问题中提出的问题的正确方法。
karma.conf.js
{
...
files: [
// only specify one entry point
// and require all tests in there
'test/index_test.js'
],
preprocessors: {
// add webpack as preprocessor
'test/index_test.js': [ 'webpack' ]
},
...
}
test/index_test.js
// require all modules ending in "_test" from the
// current directory and all subdirectories
const testsContext = require.context(".", true, /_test$/)
testsContext.keys().forEach(testsContext)
在我看来,按照@Adi Prasetyo 的建议关注整个捆绑包的变化是错误的。我们应该只注意测试文件和其中导入的文件的变化,这可以通过最后一个 URL 中显示的配置来实现。
对于热重载的工作非常重要(至少在我的情况下是它工作的原因),您需要设置 webpack-dev-middleware 配置,以便在某些测试文件中发生更改时更新测试包或在其中导入的文件。这是我正在使用的配置:
karma.config.js
{
...
webpackMiddleware: {
watchOptions: {
aggregateTimeout: 300,
poll: 1000, // customize depending on PC power
},
},
...
}
更多信息here。