Categories
JavaScript Answers

How to write a Jest configuration file with JavaScript?

Spread the love

Sometimes, we want to write a Jest configuration file with JavaScript.

In this article, we’ll look at how to write a Jest configuration file with JavaScript.

How to write a Jest configuration file with JavaScript?

To write a Jest configuration file with JavaScript, we create a module with the config.

For instance, we write

{
  //...
  "scripts": {
    "start": "node server.js",
    "dev": "webpack-dev-server --hot --inline",
    "test": "jest --config ./jest-config.js",
    "build": "webpack",
    "postinstall": "webpack"
  }
  //...
}

to add the test script in package.json.

Then we write

module.exports = {
  verbose: true,
  setupTestFrameworkScriptFile: "./enzyme.js",
  roots: ["../__tests__"],
  modulePaths: ["./__stubs__"],
  moduleNameMapper: {
    ".scss$": "scss-stub.js",
  },
};

in jest-config.js to create a module with our Jest test config.

Conclusion

To write a Jest configuration file with JavaScript, we create a module with the config.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *