I'm working on a react project by using Webpack, webpack-dev-server and Jest test suite. My Questions is: how can I run a watch script triggered by webpack-dev-server, and allow Jest watch if my tests fail or not?
Example:
I was thinking something like that in my package.json file:
"scripts": {
"build": "NODE_ENV=production webpack -p --config webpack.config.js",
"watch": "webpack-dev-server --progress --colors --hot --inline && npm run test",
"test": "jest"
}
Obviously, this doesn't work.
Below is my package.json file and my webpack.config.js file.
package.json
{
"scripts": {
"build": "NODE_ENV=production webpack -p --config webpack.config.js",
"watch": "webpack-dev-server --progress --colors --hot --inline",
"test": "jest"
},
"author": "Resultados Digitais",
"license": "ISC",
"jest": {
"transform": {
".*": "./node_modules/babel-jest"
},
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
},
"unmockedModulePathPatterns": [
"node_modules/react/",
"node_modules/enzyme/"
]
}
. . .
}
webpack.config.json
var path = require('path');
var CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: ['babel-polyfill', './src/js/index.jsx'],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
enforce: "pre",
test: /\.jsx$/,
exclude: /node_modules/,
loader: "eslint-loader",
},
{
test: /\.jsx?$/,
exclude: [/node_modules/],
use: [{
loader: 'babel-loader',
options: { presets: ['es2015', 'react', 'stage-0'] }
}]
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader'}
]
},
{
test: /\.less$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader'},
{ loader: 'less-loader' },
]
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: {
loader: 'url?limit=10000!img?progressive=true'
}
},
{ test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff' },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream' },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml' }
]
},
resolveLoader: { moduleExtensions: ["-loader"] },
devtool: 'source-map',
resolve: {
extensions: ['*', '.js', '.jsx']
},
plugins: [
new CopyWebpackPlugin([
{ from: './src/html' },
{ from: './src/img/favicon.ico', to: './img'}
])
],
devServer: {
inline: true,
hot: true,
contentBase: path.join(__dirname, 'dist'),
port: 5000
}
}
appreciate any help.
Jest can be used in projects that use webpack to manage assets, styles, and compilation.
Jest will execute different test files potentially in parallel, potentially in a different order from run to run. Per file, it will run all describe blocks first and then run tests in sequence, in the order it encountered them while executing the describe blocks.
Install npm-run-all
as a dev dependency, which allows you to run several scripts at once.
https://www.npmjs.com/package/npm-run-all
Example assuming both the following "start"
and "test"
scripts work individually:
{
"test": "jest --watch",
"start": "webpack-dev-server --progress --colors --hot --inline",
"dev": "npm-run-all --parallel test start"
}
You simply start both with npm run dev
on your terminal and ready to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With