How to write a Jest configuration file

后端 未结 5 962
我在风中等你
我在风中等你 2021-02-02 07:17

I am trying to pass along a configuration file to Jest in order to run tests only from a given directory.

According to documentation, you can use config.testPathDi

5条回答
  •  再見小時候
    2021-02-02 07:44

    As of the current date of October 5, 2018, I was able to setup a jest configuration file with ease, without it requiring it to be in JSON format.

    My package.json scripts section:

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

    I decided to keep the jest-config file in the root path alongside package.json, so that's where it is pointing to, but you can put it anywhere you want. Here is the configuration file, albeit a little short:

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

    The Jest documentation in the "Configuring Jest" section (link here) says that you can create it with a module.exports object. They include a caveat that says, "Please keep in mind that the resulting configuration must be JSON-serializable", which adds to the confusion. "Delightful Javascript Testing!" Hah!

    P.S. The roots property in the config tells Jest where to look for all of your tests. Thought it might help.

提交回复
热议问题