Skip to content

Commit c765827

Browse files
committed
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 f544a36 commit c765827

33 files changed

+82
-283
lines changed

jest.config.js

+19
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

+7
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

+2-2
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

-16
This file was deleted.

packages/core/jest.config.js

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

packages/core/package.json

-20
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

+7
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

-26
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

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

packages/hub/package.json

-20
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
}

packages/integrations/jest.config.js

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

packages/integrations/package.json

-20
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,5 @@
5454
"volta": {
5555
"extends": "../../package.json"
5656
},
57-
"jest": {
58-
"collectCoverage": true,
59-
"transform": {
60-
"^.+\\.ts$": "ts-jest"
61-
},
62-
"moduleFileExtensions": [
63-
"js",
64-
"ts"
65-
],
66-
"testEnvironment": "node",
67-
"testMatch": [
68-
"**/*.test.ts"
69-
],
70-
"globals": {
71-
"ts-jest": {
72-
"tsConfig": "./tsconfig.json",
73-
"diagnostics": false
74-
}
75-
}
76-
},
7757
"sideEffects": false
7858
}

packages/minimal/jest.config.js

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

packages/minimal/package.json

-20
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
}

packages/nextjs/jest.config.js

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

packages/nextjs/package.json

-20
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,6 @@
7676
"volta": {
7777
"extends": "../../package.json"
7878
},
79-
"jest": {
80-
"collectCoverage": true,
81-
"transform": {
82-
"^.+\\.ts$": "ts-jest"
83-
},
84-
"moduleFileExtensions": [
85-
"js",
86-
"ts"
87-
],
88-
"testEnvironment": "node",
89-
"testMatch": [
90-
"**/*.test.ts"
91-
],
92-
"globals": {
93-
"ts-jest": {
94-
"tsConfig": "./tsconfig.json",
95-
"diagnostics": false
96-
}
97-
}
98-
},
9979
"sideEffects": [
10080
"./cjs/index.server.js",
10181
"./esm/index.server.js",
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const config = {
2-
transform: {
3-
'^.+\\.ts$': 'ts-jest',
4-
},
5-
testEnvironment: 'node',
1+
const baseConfig = require('../../jest.config.js');
2+
3+
module.exports = {
4+
...baseConfig,
65
testMatch: ['**/test.ts'],
7-
moduleFileExtensions: ['js', 'ts'],
86
};
9-
10-
module.exports = config;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"extends": "../../tsconfig.json",
3+
4+
"include": ["**/*.ts"],
5+
36
"compilerOptions": {
7+
// package-specific options
48
"esModuleInterop": true,
5-
"types": ["jest", "node"]
6-
},
7-
"include": ["**/*.ts", "jest.config.js"],
8-
"exclude": ["node_modules"]
9+
"types": ["node"]
10+
}
911
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
4+
"include": ["**/*.ts"],
5+
6+
"compilerOptions": {
7+
// should include all types from `./tsconfig.json` plus types for all test frameworks used
8+
"types": ["node", "jest"]
9+
10+
// other package-specific, test-specific options
11+
}
12+
}

packages/node/jest.config.js

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

packages/node/package.json

-20
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,5 @@
6565
},
6666
"volta": {
6767
"extends": "../../package.json"
68-
},
69-
"jest": {
70-
"collectCoverage": true,
71-
"transform": {
72-
"^.+\\.ts$": "ts-jest"
73-
},
74-
"moduleFileExtensions": [
75-
"js",
76-
"ts"
77-
],
78-
"testEnvironment": "node",
79-
"testMatch": [
80-
"**/*.test.ts"
81-
],
82-
"globals": {
83-
"ts-jest": {
84-
"tsConfig": "./tsconfig.json",
85-
"diagnostics": false
86-
}
87-
}
8868
}
8969
}

packages/react/jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const baseConfig = require('../../jest.config.js');
2+
3+
module.exports = {
4+
...baseConfig,
5+
testEnvironment: 'jsdom',
6+
};

packages/react/package.json

-23
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,5 @@
7777
"volta": {
7878
"extends": "../../package.json"
7979
},
80-
"jest": {
81-
"collectCoverage": true,
82-
"transform": {
83-
"^.+\\.ts$": "ts-jest",
84-
"^.+\\.tsx$": "ts-jest"
85-
},
86-
"moduleFileExtensions": [
87-
"js",
88-
"ts",
89-
"tsx"
90-
],
91-
"testEnvironment": "jsdom",
92-
"testMatch": [
93-
"**/*.test.ts",
94-
"**/*.test.tsx"
95-
],
96-
"globals": {
97-
"ts-jest": {
98-
"tsConfig": "./tsconfig.json",
99-
"diagnostics": false
100-
}
101-
}
102-
},
10380
"sideEffects": false
10481
}

packages/serverless/jest.config.js

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

packages/serverless/package.json

+1-21
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,5 @@
6868
"volta": {
6969
"extends": "../../package.json"
7070
},
71-
"sideEffects": false,
72-
"jest": {
73-
"collectCoverage": true,
74-
"transform": {
75-
"^.+\\.ts$": "ts-jest"
76-
},
77-
"moduleFileExtensions": [
78-
"js",
79-
"ts"
80-
],
81-
"testEnvironment": "node",
82-
"testMatch": [
83-
"**/*.test.ts"
84-
],
85-
"globals": {
86-
"ts-jest": {
87-
"tsConfig": "./tsconfig.json",
88-
"diagnostics": false
89-
}
90-
}
91-
}
71+
"sideEffects": false
9272
}

packages/tracing/jest.config.js

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

0 commit comments

Comments
 (0)