Skip to content

Commit 4f0469b

Browse files
authored
ref(dev): Centralize jest config (#4907)
Currently, we configure jest using a mix of `package.json` entries and `jest.config.js` files, and there's a great deal of repetition between configs. To make things DRYer and easier to update (as will happen in future PRs), this aims to fix that by creating a centralized jest config from which all others can inherit. To facilitate this inheritance, all config has been removed from `package.json` files and moved into `jest.config.js` files. This change was done in a few distinct stages: - Extracting the config which was identical across many packages into a central config file, and fixing a mistake they all contained, namely that they were using the regular tsconfig file rather than the test-specific one. - In the packages which were using exactly that config, creating a new `jest.config.js` file and inheriting directly from the central jest config with no changes. - In the packages whose config varied only slightly from the boilerplate config, creating a new `jest.config.js` file and inheriting from the central file with small changes. This also required adding `.tsx` files to the central config. - In the browser package, moving the existing `jest.config.js` for the unit tests to the repo root level and refactoring it to inherit from the central file. This also required specifying a coverage directory in the central config and modifying the browser package's yarn test commands. - In the node integration test package, refactoring the existing `jest.config.js` to inherit from the central file. This also required creating a test-specific tsconfig to match the one in other packages. - Finally, making a small optimization (narrowing the scope of where to look for tests) in the now universally-used central config.
1 parent 05504b3 commit 4f0469b

33 files changed

+82
-283
lines changed

jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
rootDir: process.cwd(),
3+
collectCoverage: true,
4+
transform: {
5+
'^.+\\.ts$': 'ts-jest',
6+
'^.+\\.tsx$': 'ts-jest',
7+
},
8+
coverageDirectory: '<rootDir>/coverage',
9+
moduleFileExtensions: ['js', 'ts', 'tsx'],
10+
testEnvironment: 'node',
11+
testMatch: ['<rootDir>/**/*.test.ts', '<rootDir>/**/*.test.tsx'],
12+
globals: {
13+
'ts-jest': {
14+
tsconfig: '<rootDir>/tsconfig.test.json',
15+
diagnostics: false,
16+
},
17+
},
18+
testPathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/node_modules/'],
19+
};

packages/browser/jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const baseConfig = require('../../jest.config.js');
2+
3+
module.exports = {
4+
...baseConfig,
5+
testEnvironment: 'jsdom',
6+
testMatch: ['<rootDir>/test/unit/**/*.test.ts'],
7+
};

packages/browser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@
7272
"size:check:es5": "cat build/bundles/bundle.min.js | gzip -9 | wc -c | awk '{$1=$1/1024; print \"ES5: \",$1,\"kB\";}'",
7373
"size:check:es6": "cat build/bundles/bundle.es6.min.js | gzip -9 | wc -c | awk '{$1=$1/1024; print \"ES6: \",$1,\"kB\";}'",
7474
"test": "run-s test:unit",
75-
"test:unit": "jest --config test/unit/jest.config.js",
75+
"test:unit": "jest",
7676
"test:integration": "test/integration/run.js",
7777
"test:integration:checkbrowsers": "node scripts/checkbrowsers.js",
7878
"test:package": "node test/package/npm-build.js && rm test/package/tmp.js",
79-
"test:unit:watch": "jest --config test/unit/jest.config.js --watch",
79+
"test:unit:watch": "jest --watch",
8080
"test:integration:watch": "test/integration/run.js --watch"
8181
},
8282
"volta": {

packages/browser/test/unit/jest.config.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/core/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../jest.config.js');

packages/core/package.json

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,5 @@
5252
"volta": {
5353
"extends": "../../package.json"
5454
},
55-
"jest": {
56-
"collectCoverage": true,
57-
"transform": {
58-
"^.+\\.ts$": "ts-jest"
59-
},
60-
"moduleFileExtensions": [
61-
"js",
62-
"ts"
63-
],
64-
"testEnvironment": "node",
65-
"testMatch": [
66-
"**/*.test.ts"
67-
],
68-
"globals": {
69-
"ts-jest": {
70-
"tsConfig": "./tsconfig.json",
71-
"diagnostics": false
72-
}
73-
}
74-
},
7555
"sideEffects": false
7656
}

packages/gatsby/jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const baseConfig = require('../../jest.config.js');
2+
3+
module.exports = {
4+
...baseConfig,
5+
setupFiles: ['<rootDir>/test/setEnvVars.ts'],
6+
testEnvironment: 'jsdom',
7+
};

packages/gatsby/package.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,5 @@
6262
"volta": {
6363
"extends": "../../package.json"
6464
},
65-
"jest": {
66-
"collectCoverage": true,
67-
"transform": {
68-
"^.+\\.ts$": "ts-jest",
69-
"^.+\\.tsx$": "ts-jest"
70-
},
71-
"moduleFileExtensions": [
72-
"js",
73-
"ts",
74-
"tsx"
75-
],
76-
"testEnvironment": "jsdom",
77-
"testMatch": [
78-
"**/*.test.ts",
79-
"**/*.test.tsx"
80-
],
81-
"globals": {
82-
"ts-jest": {
83-
"tsConfig": "./tsconfig.json",
84-
"diagnostics": false
85-
}
86-
},
87-
"setupFiles": [
88-
"<rootDir>/test/setEnvVars.ts"
89-
]
90-
},
9165
"sideEffects": false
9266
}

packages/hub/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../jest.config.js');

packages/hub/package.json

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,5 @@
4949
"volta": {
5050
"extends": "../../package.json"
5151
},
52-
"jest": {
53-
"collectCoverage": true,
54-
"transform": {
55-
"^.+\\.ts$": "ts-jest"
56-
},
57-
"moduleFileExtensions": [
58-
"js",
59-
"ts"
60-
],
61-
"testEnvironment": "node",
62-
"testMatch": [
63-
"**/*.test.ts"
64-
],
65-
"globals": {
66-
"ts-jest": {
67-
"tsConfig": "./tsconfig.json",
68-
"diagnostics": false
69-
}
70-
}
71-
},
7252
"sideEffects": false
7353
}

0 commit comments

Comments
 (0)