Skip to content

Commit 2edd5c4

Browse files
authored
chore: add helper for getting Jest's config in e2e tests (#9770)
1 parent edf8c0a commit 2edd5c4

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed

e2e/__tests__/config.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import runJest from '../runJest';
8+
import runJest, {getConfig} from '../runJest';
99

1010
test('config as JSON', () => {
1111
const result = runJest('verbose-reporter', [
@@ -70,14 +70,11 @@ test('works with jsdom testEnvironmentOptions config JSON', () => {
7070
});
7171

7272
test('negated flags override previous flags', () => {
73-
const {stdout} = runJest('verbose-reporter', [
74-
'--show-config',
73+
const {globalConfig} = getConfig('verbose-reporter', [
7574
'--silent',
7675
'--no-silent',
7776
'--silent',
7877
]);
7978

80-
const parsedConfig = JSON.parse(stdout);
81-
82-
expect(parsedConfig.globalConfig.silent).toEqual(true);
79+
expect(globalConfig.silent).toEqual(true);
8380
});

e2e/__tests__/esmConfigFile.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,41 @@
66
*/
77

88
import {onNodeVersions} from '@jest/test-utils';
9-
import {json as runWithJson} from '../runJest';
9+
import {getConfig} from '../runJest';
1010

1111
test('reads config from cjs file', () => {
12-
const {json, exitCode} = runWithJson('esm-config/cjs', ['--show-config'], {
12+
const {configs} = getConfig('esm-config/cjs', [], {
1313
skipPkgJsonCheck: true,
1414
});
1515

16-
expect(exitCode).toBe(0);
17-
expect(json.configs).toHaveLength(1);
18-
expect(json.configs[0].displayName).toEqual({
16+
expect(configs).toHaveLength(1);
17+
expect(configs[0].displayName).toEqual({
1918
color: 'white',
2019
name: 'Config from cjs file',
2120
});
2221
});
2322

2423
// not unflagged for other versions yet. Update this range if that changes
25-
onNodeVersions('^13.2.0', () => {
24+
onNodeVersions('>=13.2.0', () => {
2625
test('reads config from mjs file', () => {
27-
const {json, exitCode} = runWithJson('esm-config/mjs', ['--show-config'], {
26+
const {configs} = getConfig('esm-config/mjs', [], {
2827
skipPkgJsonCheck: true,
2928
});
3029

31-
expect(exitCode).toBe(0);
32-
expect(json.configs).toHaveLength(1);
33-
expect(json.configs[0].displayName).toEqual({
30+
expect(configs).toHaveLength(1);
31+
expect(configs[0].displayName).toEqual({
3432
color: 'white',
3533
name: 'Config from mjs file',
3634
});
3735
});
3836

3937
test('reads config from js file when package.json#type=module', () => {
40-
const {json, exitCode} = runWithJson('esm-config/js', ['--show-config'], {
38+
const {configs} = getConfig('esm-config/js', [], {
4139
skipPkgJsonCheck: true,
4240
});
4341

44-
expect(exitCode).toBe(0);
45-
expect(json.configs).toHaveLength(1);
46-
expect(json.configs[0].displayName).toEqual({
42+
expect(configs).toHaveLength(1);
43+
expect(configs[0].displayName).toEqual({
4744
color: 'white',
4845
name: 'Config from js file',
4946
});

e2e/__tests__/multiProjectRunner.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import {wrap} from 'jest-snapshot-serializer-raw';
1111
import {cleanup, extractSummary, sortLines, writeFiles} from '../Utils';
12-
import runJest from '../runJest';
12+
import runJest, {getConfig} from '../runJest';
1313

1414
const DIR = path.resolve(tmpdir(), 'multi-project-runner-test');
1515

@@ -509,14 +509,7 @@ describe("doesn't bleed module file extensions resolution with multiple workers"
509509
};`,
510510
});
511511

512-
const {stdout: configOutput} = runJest(DIR, [
513-
'--show-config',
514-
'--projects',
515-
'project1',
516-
'project2',
517-
]);
518-
519-
const {configs} = JSON.parse(configOutput);
512+
const {configs} = getConfig(DIR, ['--projects', 'project1', 'project2']);
520513

521514
expect(configs).toHaveLength(2);
522515

@@ -559,9 +552,7 @@ describe("doesn't bleed module file extensions resolution with multiple workers"
559552
`,
560553
});
561554

562-
const {stdout: configOutput} = runJest(DIR, ['--show-config']);
563-
564-
const {configs} = JSON.parse(configOutput);
555+
const {configs} = getConfig(DIR);
565556

566557
expect(configs).toHaveLength(2);
567558

e2e/runJest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as path from 'path';
1010
import * as fs from 'fs';
1111
import {Writable} from 'stream';
1212
import execa = require('execa');
13+
import type {Config} from '@jest/types';
1314
import type {FormattedTestResults} from '@jest/test-result';
1415
import stripAnsi = require('strip-ansi');
1516
import {normalizeIcons} from './Utils';
@@ -214,3 +215,24 @@ export const runContinuous = function (
214215
},
215216
};
216217
};
218+
219+
// return type matches output of logDebugMessages
220+
export function getConfig(
221+
dir: string,
222+
args: Array<string> = [],
223+
options?: RunJestOptions,
224+
): {
225+
globalConfig: Config.GlobalConfig;
226+
configs: Array<Config.ProjectConfig>;
227+
version: string;
228+
} {
229+
const {exitCode, stdout} = runJest(
230+
dir,
231+
args.concat('--show-config'),
232+
options,
233+
);
234+
235+
expect(exitCode).toBe(0);
236+
237+
return JSON.parse(stdout);
238+
}

packages/jest-core/src/lib/log_debug_messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {Config} from '@jest/types';
99

1010
const VERSION = require('../../package.json').version;
1111

12+
// if the output here changes, update `getConfig` in e2e/runJest.ts
1213
export default function logDebugMessages(
1314
globalConfig: Config.GlobalConfig,
1415
configs: Array<Config.ProjectConfig> | Config.ProjectConfig,

packages/test-utils/src/ConditionalTest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ export function onNodeVersions(
4141
versionRange: string,
4242
testBody: () => void,
4343
): void {
44+
const description = `on node ${versionRange}`;
4445
if (!semver.satisfies(process.versions.node, versionRange)) {
45-
describe.skip(`[SKIP] tests that require node ${versionRange}`, () => {
46+
describe.skip(description, () => {
4647
testBody();
4748
});
4849
} else {
49-
testBody();
50+
describe(description, () => {
51+
testBody();
52+
});
5053
}
5154
}
5255

0 commit comments

Comments
 (0)