7
votes

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.

2
I think there should be a loader called jest-loader which using webpack loader api and will run jest before emit resource, then generate cover reports or emit errors. Unfortunately, I couldn't find a loader like that, maybe you can write one 😝ifmos

2 Answers

4
votes

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 test start"
}

You simply start both with npm run devon your terminal and ready to go.

2
votes

Change your test script in package.json from this "test": "jest" to this "test": "jest --watch".

Then just use 2 terminals: In one terminal you run npm run watch and in the other you run npm run test.