Is there any way to extend the jest configuration file?

In the node application, where I use Jest to check client side code (testEnvironment: "jsdom") and server side code (testEnvironment: "node"), and also to collect code coverage for client and server side.

I am currently using 4 Jest configuration files with a lot of redundant configuration to accomplish this.

customer

{ "bail": true, "verbose": true, "notify": true, "scriptPreprocessor": "./node_modules/babel-jest", "testPathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build" ], "testRegex": "\\.test\\.js" } 

customer coverage

 { "bail": true, "verbose": true, "notify": true, "scriptPreprocessor": "./node_modules/babel-jest", "testPathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build" ], "testRegex": "\\.test\\.js", "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"], "collectCoverage": true, "coverageDirectory": "./coverage", "coveragePathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build", "./test" ], "coverageThreshold": { "global": { "branches": 100, "functions": 100, "lines": 100, "statements": 100 } } } 

Server

 { "bail": true, "verbose": true, "notify": true, "scriptPreprocessor": "./node_modules/babel-jest", "testPathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build" ], "testRegex": "\\.test\\.js", "testEnvironment": "node" } 

server reach

 { "bail": true, "verbose": true, "notify": true, "scriptPreprocessor": "./node_modules/babel-jest", "testPathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build" ], "testRegex": "\\.test\\.js", "testEnvironment": "node", "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"], "collectCoverage": true, "coverageDirectory": "./coverage", "coveragePathIgnorePatterns": [ "./node_modules", "./coverage", "./dist", "./build", "./test" ], "coverageThreshold": { "global": { "branches": 100, "functions": 100, "lines": 100, "statements": 100 } } } 

How can I achieve this without repeating my configuration 4 times? I looked at the preset configuration option. Using this, I have to create a separate package for each configuration. Is this the recommended way?

+5
source share
1 answer

Yes, since Jest v20 you can define config as a JS file and use it to share the common parts of such configurations. Documents for setting up Jest .

By default, Jest searches for:

  • jest.config.js
  • "jest" entry in package.json

... and treats the parent directory as rootDir .

Also, do not forget to check the projects parameter, which simplifies the launch of Jest inside monorepos (for example, client + server code in the same code base). See this answer for reference: Testing two environments with a joke

+1
source

Source: https://habr.com/ru/post/1260090/


All Articles