Skip to content

Commit 2a094ad

Browse files
committed
feat(config): add defineConfig and mergeConfig functions
1 parent 855864e commit 2a094ad

File tree

14 files changed

+376
-0
lines changed

14 files changed

+376
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## main
22

3+
### Features
4+
5+
- `[jest-config]` Add `defineConfig` and `mergeConfig` helpers for type-safe Jest config ([#15844](https://github.com/jestjs/jest/pull/15844))
6+
37
## 30.2.0
48

59
### Chore & Maintenance

e2e/__tests__/config.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,48 @@ test('negated flags override previous flags', () => {
7575

7676
expect(globalConfig.silent).toBe(true);
7777
});
78+
79+
test('should work with define config function taking in config object', () => {
80+
const result = runJest('config-utils', [
81+
'--config=jest.config.ts',
82+
'__tests__/simple.test.js',
83+
]);
84+
85+
expect(result.exitCode).toBe(0);
86+
});
87+
88+
test('should work with define config function taking in callback', () => {
89+
const result = runJest('config-utils', [
90+
'--config=jest.callback.config.ts',
91+
'__tests__/simple.test.js',
92+
]);
93+
94+
expect(result.exitCode).toBe(0);
95+
});
96+
97+
test('should work with merged config of 2 config objects', () => {
98+
const result = runJest('config-utils', [
99+
'--config=jest.merge.config.ts',
100+
'__tests__/merge.test.js',
101+
]);
102+
103+
expect(result.exitCode).toBe(0);
104+
});
105+
106+
test('should work with merged config of one config object with a define function config', () => {
107+
const result = runJest('config-utils', [
108+
'--config=jest.merge-with-define.config.ts',
109+
'__tests__/merge.test.js',
110+
]);
111+
112+
expect(result.exitCode).toBe(0);
113+
});
114+
115+
test('should work with merged config as callback for define function config', () => {
116+
const result = runJest('config-utils', [
117+
'--config=jest.merge-with-callback.config.ts',
118+
'__tests__/merge.test.js',
119+
]);
120+
121+
expect(result.exitCode).toBe(0);
122+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('should work', () => {
9+
expect(typeof globalThis.window).toBe('object');
10+
expect(1 + 1).toBe(2);
11+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('should work', () => {
9+
expect(1 + 1).toBe(2);
10+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-config-loader esbuild-register
8+
*/
9+
10+
import {type Config, defineConfig} from 'jest';
11+
12+
export default defineConfig(async () => {
13+
const baseConfig = await new Promise<Config>(resolve => {
14+
setTimeout(() => {
15+
resolve({
16+
testEnvironment: 'node',
17+
});
18+
});
19+
});
20+
21+
return {
22+
...baseConfig,
23+
displayName: 'callback-config',
24+
};
25+
});

e2e/config-utils/jest.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-config-loader esbuild-register
8+
*/
9+
10+
import {defineConfig} from 'jest';
11+
12+
export default defineConfig({
13+
displayName: 'simple-config',
14+
testEnvironment: 'node',
15+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-config-loader esbuild-register
8+
*/
9+
10+
import {defineConfig, mergeConfig} from 'jest';
11+
import jestConfig from './jest.config';
12+
13+
export default defineConfig(() =>
14+
mergeConfig(
15+
jestConfig,
16+
defineConfig({
17+
displayName: 'merge-with-callback',
18+
testEnvironment: 'jsdom',
19+
}),
20+
),
21+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-config-loader esbuild-register
8+
*/
9+
10+
import {defineConfig, mergeConfig} from 'jest';
11+
import jestConfig from './jest.config';
12+
13+
export default mergeConfig(
14+
jestConfig,
15+
defineConfig({
16+
displayName: 'merge-with-define-config',
17+
testEnvironment: 'jsdom',
18+
}),
19+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-config-loader esbuild-register
8+
*/
9+
10+
import {mergeConfig} from 'jest';
11+
import baseConfig from './jest.config';
12+
13+
export default mergeConfig(baseConfig, {
14+
displayName: 'merge-config',
15+
testEnvironment: 'jsdom',
16+
});

e2e/config-utils/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

0 commit comments

Comments
 (0)