-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to build mocha with webpack #2448
Comments
Can we start there? What is that reason and could whatever you want to achieve be done without? The amount of scope creep webpack adds, like having to bundle your test runner seems like the real issue to me here |
If you're using // webpack.config.mocha.js
plugins: [
...
entry: ["./test/all.js"],
new HtmlWebpackPlugin({
cache: true,
filename: "test/index.html",
showErrors: true,
template: "./test/support/index.html",
title: "Mocha Browser Tests",
}),
...
], // test/all.js
const context = require.context(
"../src", // Root directory
true, // Recursive
/.+\.test\.js$/ // Test pattern
);
// Require each within build
context.keys().forEach(context); <!-- test/support/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><%= htmlWebpackPlugin.options.title %></title>
<link href="https://npmcdn.com/[email protected]/mocha.css" rel="stylesheet" />
<script src="https://npmcdn.com/[email protected]/mocha.js"></script>
</head>
<body>
<!-- A container element for the visual Mocha results -->
<div id="mocha"></div>
<!-- Mocha setup and initiation code -->
<script>
mocha.setup('bdd');
window.onload = function() {
var runner = mocha.run();
var failedTests = [];
runner.on('end', function() {
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});
runner.on('fail', logFailure);
function logFailure(test, err){
var flattenTitles = function(test){
var titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};
failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
});
};
};
</script>
</body>
</html> |
I'm building chrome extension that allows to run tests with mocha. @ericclemmons Anyway, guys, could you shortly explain for what reason there is |
@vitalets That's the entry point for Browserify. It's used when building the browser version of Mocha ( Webpack may not respect the Regardless, the entry point should be I'm going to close this, as I don't think there's anything to be done here, but feel free to ask more questions here or in Gitter. |
@boneskull thanks for explanation! |
A nagging problem. // Karma configuration
const path = require('path');
const webpack = require('webpack');
module.exports = function (config) {
config.set({
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha'],
// webpack configuration
webpack: {
module: {
// Suppress warning from mocha: "Critical dependency: the request of a dependency is an expression"
// @see https://webpack.js.org/configuration/module/#module-contexts
exprContextCritical: false,
// doesn't append `babel-loader` because `jsdiff` package will cause some issue.
// @see https://github.com/kpdecker/jsdiff/issues/161
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
},
{
test: /\.tsx?$/,
include: path.resolve(__dirname, 'source'),
exclude: path.resolve(__dirname, 'test'),
enforce: 'post',
use: {
loader: 'istanbul-instrumenter-loader',
options: {esModules: true},
},
},
],
},
// Suppress fatal error: Cannot resolve module 'fs'
// @relative https://github.com/pugjs/pug-loader/issues/8
// @see https://github.com/webpack/docs/wiki/Configuration#node
node: {
fs: 'empty',
},
}
// Other code...
});
}; |
No, we're not going to go into that, Webpack is important for a lot of devs whether you like it or not. We can start with why Mocha's Module not found: Error: Can't resolve 'fs' in '/Users/andy/react-fader/node_modules/mkdirp'
@ ./node_modules/mkdirp/index.js 2:9-22
@ ./node_modules/mocha/lib/reporters/xunit.js
@ ./node_modules/mocha/lib/reporters/index.js
@ ./node_modules/mocha/lib/mocha.js
@ ./node_modules/mocha/browser-entry.js
@ ./test/index.js If I mean, what hack are you guys using to build |
@boneskull I dug into this some more, and I believe Mocha doesn't really use the
Mocha is using This is why even though Webpack supports the I see in your build config you are telling Browserify to ignore So Mocha should ensure that when run on the client it won't run any code that does |
you’re welcome to send a PR if it doesn’t break the build. |
…ression' in mocha, found in mochajs/mocha#2448
@vitalets webpack only supports using the browserify supports the spec, hence why builds work fine using that. |
I struggled with some of the same issues expressed on this thread, so here's my solution just in case it helps someone else. My requirements were that I could run the tests using karma but I also had a webpage that allowed me to run the mocha tests in a browser. I did manage to get something working using an html page based on the html that @ericclemmons posted. However, I was pretty unhappy with my approach because all my other modules were being bundled nicely with webpack, but then I had a few files that were sitting outside of that, and I was using things like the webpack copy plugin to move mocha.js to the right place in the build and it all looked wrong. So, I wanted to load mocha using webpack so it was managed in the same way as all my other modules (I think that's a fairly normal thing to want, despite some of the comments on this thread). It would be straightforward to shim mocha using exports-loader if you only wanted to run in a browser, but karma-mocha provides its own version of mocha which will conflict with the version pulled in using webpack if you do this. I did finally get it working by:
|
@tstibbs - Would you mind posting a link to your working project somewhere? |
@rafikhan You'll have to have a dig around, but the following should be most of what you need: https://github.com/tstibbs/geo-bagging/blob/master/ui/test/suite/mocha-wrapper.js (to be clear I'm not saying my approach is right or even valid, I'm just saying it appears to be working for me - on a small personal project with just a handful of tests) |
I think added in 9.0.1 |
Applying directly @aovchinn's solution still displayed in console the error:
However I was successful using |
For some reason I need to bundle mocha with webpack.
I see
browser
section in mocha package.json so it looks like webpack should exclude node specific require's.But when i try
var mocha = require('mocha')
I'm getting warnings:and errors:
Can anybody help to fix this?
How to correctly use
browser-entry.js
?thanks!
The text was updated successfully, but these errors were encountered: