3

I'm trying to convert a project from browserify+mochify to webpack.

The webpack docs demonstrate how to use mocha-loader to run the tests with webpack-dev-server, but assumes a single entry point into the tests.

All the existing tests were designed with mochify in mind which does not require a single entry point as it recursively bundles ./test/*.js.

Dustin Wyatt
  • 4,046
  • 5
  • 31
  • 60

2 Answers2

2

The setup below sort of works for me. It still uses mochify to run the tests (because it has all the phantomjs interfacing), but doesn't rely on anything from browserify. If you run webpack --watch, it reruns all tests when a file changes.

webpack.config.js:

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:

// 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);
});

Another approach is described here: https://stackoverflow.com/a/32386750/675011

Community
  • 1
  • 1
Remko
  • 823
  • 6
  • 16
0

This is not exactly an answer, but it solved the problem for me. The reason I wanted to combine mochify and webpack was that I wanted to use the browser console to debug my mocha tests. Since my mocha tests themselves don't rely on a browser, it was enough for me to finally realize I could use a node debugger and it would bring up the Chrome console, (almost) solving my problem. node-inspector is the node debugger, but I'm using babel, so I needed babel-node-debug, but that doesn't yet work with babel6, but there's an unmerged pull request that fixes it: https://github.com/CrabDude/babel-node-debug/pull/12.

Sigfried
  • 2,943
  • 3
  • 31
  • 43
  • Debugging with mocha is having further complications. `bode-debug _mocha index.js` is sort of working for me. – Sigfried Feb 03 '16 at 11:52