关于mocha.js:当您没有测试的单个入口点时,如何使用webpack运行mocha测试?

How to run mocha tests with webpack when you don't have a single entry point to tests?

我正在尝试将项目从browserify mochify转换为webpack

webpack文档演示了如何使用mocha-loaderwebpack-dev-server来运行测试,但是假定了进入测试的单个入口。

所有现有测试在设计时都考虑了mochify,因为它递归地捆绑了./test/*.js,因此不需要单个入口点。


下面的设置适合??我。它仍然使用mochify来运行测试(因为它具有所有phantomjs接口),但不依赖browserify中的任何内容。如果运行webpack --watch,它将在文件更改时重新运行所有测试。

webpack.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var path = require("path");
var child_process = require('child_process');

module.exports = {
  entry: {
    tests:"./tests.js"
  },
  output: {
    filename:"tests.js", // Should be a unique name
    path:"/tmp"
  },
  plugins: [
    // Automatically run all tests when webpack is done
    function () {
      this.plugin("done", function (stats) {
        child_process.execSync('mochify /tmp/tests.js', { stdio: 'inherit'});
      });
    }
  ],
};

tests.js

1
2
3
4
5
6
7
8
// List all test dirs here if you have multiple
var contexts = [
  require.context('./dir1/test', true, /\\.js$/),
  require.context('./dir2/test', true, /\\.js$/)
];
contexts.forEach(function (context) {
  context.keys().forEach(context);
});

此处介绍了另一种方法:https://stackoverflow.com/a/32386750/675011


这不是一个确切的答案,但是它为我解决了这个问题。我想结合使用mochify和webpack的原因是我想使用浏览器控制台调试我的mocha测试。由于我的Mocha测试本身不依赖浏览器,因此我终于意识到我可以使用节点调试器,它可以启动Chrome控制台,(几乎)解决了我的问题。 node-inspector是节点调试器,但是我使用的是babel,所以我需要babel-node-debug,但是现在还不能与babel6一起使用,但是有一个未合并的pull请求可以修复它:https:// github。 com / CrabDude / babel-node-debug / pull / 12。