Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
mocha-webpack
Advanced tools
Precompiles your server-side webpack bundles before running mocha. Inspired by karma-webpack alternatives usage, but this is for Node.js!
Looking for a test runner for the browser? Use karma-webpack instead.
Work in progress...
You're building universal javascript applications with webpacks awesome features like including css or images and wanna test your code also in Node?
No problem! Just precompile your tests before running mocha:
$ webpack test.js output.js && mocha output.js
Seems pretty easy. But there are some disadvantages with this approach:
This project is a optimized version of this simple approach with following features:
The recommended approach is to install mocha-webpack locally in your project's directory.
# install mocha, webpack & mocha-webpack as devDependencies
$ npm install --save-dev mocha webpack mocha-webpack
This will install mocha
, webpack
and mocha-webpack
packages into node_modules
in your project directory and also save these as devDependencies
in your package.json.
Congratulations, you are ready to run mocha-webpack for the first time in your project!
# display version of mocha-webpack
$ node ./node_modules/mocha-webpack/bin/mocha-webpack -v
# display available commands & options of mocha-webpack
$ node ./node_modules/mocha-webpack/bin/mocha-webpack --help
Typing node ./node_modules/mocha-webpack/bin/mocha-webpack ....
is just annoying and you might find it useful to configure your run commands as npm scripts inside your package.json
.
package.json
...
"scripts": {
"test": "mocha-webpack --webpack-config webpack.config-test.js \"src/**/*.test.js\"",
},
...
This allows you to run your test command simply by just typing npm run test
.
In addition, the defined command tells mocha-webpack to use the provided webpack config file webpack.config-test.js
and to execute all tests matching the pattern src/**/*.test.js
.
webpack.config-test.js - example config
var nodeExternals = require('webpack-node-externals');
module.exports = {
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
};
Maybe you noticed that entry, output.filename and output.path are completely missing in this config. mocha-webpack does this automatically for you and if you would specify it anyway, it will be overridden by mocha-webpack.
Note: mocha-webpack emits the generated files currently into ./tmp/mocha-webpack
. So you should make sure that this folder is ignored in your .gitignore
file. In future versions this could be unnecessary.
mocha-webpack will attempt to load mocha-webpack.opts
as a configuration file in your working directory. The lines in this file are combined with any command-line arguments. The command-line arguments take precedence. Imagine you have the following mocha-webpack.opts file:
mocha-webpack.opts
--colors
--webpack-config webpack.config-test.js
src/**/*.test.js
and call mocha-webpack with
$ mocha-webpack --growl
then it's equivalent to
$ mocha-webpack --growl --colors --webpack-config webpack.config-test.js "src/**/*.test.js"
For using sourcemaps with mocha-webpack you just need to enable sourcemaps in your webpack config and use source-map-support to apply sourcemaps.
$ npm install --save-dev source-map-support
webpack.config-test.js
var nodeExternals = require('webpack-node-externals');
module.exports = {
output: {
// sourcemap support for IntelliJ/Webstorm
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
}
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
devtool: "cheap-module-source-map" // faster than 'source-map'
};
mocha-webpack.opts
--require source-map-support/register
run a single test
mocha-webpack --webpack-config webpack.config-test.js simple.test.js
run all tests by glob
mocha-webpack --webpack-config webpack.config-test.js "test/**/*.js"
run all tests in directory "test" (add --recursive
to include subdirectories)
mocha-webpack --webpack-config webpack.config-test.js test
run all tests in directory "test" matching the file pattern "*.test.js"
mocha-webpack --webpack-config webpack.config-test.js --glob "*.test.js" test
Watch mode? just add --watch
mocha-webpack --webpack-config webpack.config-test.js --watch test
see mocha-webpack --help
MIT
FAQs
mocha cli with webpack support
The npm package mocha-webpack receives a total of 6,051 weekly downloads. As such, mocha-webpack popularity was classified as popular.
We found that mocha-webpack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.