Skip to content

Commit

Permalink
chore: add helper for getting Jest's config in e2e tests (#9770)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 5, 2020
1 parent edf8c0a commit 2edd5c4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
9 changes: 3 additions & 6 deletions e2e/__tests__/config.test.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import runJest from '../runJest';
import runJest, {getConfig} from '../runJest';

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

test('negated flags override previous flags', () => {
const {stdout} = runJest('verbose-reporter', [
'--show-config',
const {globalConfig} = getConfig('verbose-reporter', [
'--silent',
'--no-silent',
'--silent',
]);

const parsedConfig = JSON.parse(stdout);

expect(parsedConfig.globalConfig.silent).toEqual(true);
expect(globalConfig.silent).toEqual(true);
});
25 changes: 11 additions & 14 deletions e2e/__tests__/esmConfigFile.test.ts
Expand Up @@ -6,44 +6,41 @@
*/

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

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

expect(exitCode).toBe(0);
expect(json.configs).toHaveLength(1);
expect(json.configs[0].displayName).toEqual({
expect(configs).toHaveLength(1);
expect(configs[0].displayName).toEqual({
color: 'white',
name: 'Config from cjs file',
});
});

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

expect(exitCode).toBe(0);
expect(json.configs).toHaveLength(1);
expect(json.configs[0].displayName).toEqual({
expect(configs).toHaveLength(1);
expect(configs[0].displayName).toEqual({
color: 'white',
name: 'Config from mjs file',
});
});

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

expect(exitCode).toBe(0);
expect(json.configs).toHaveLength(1);
expect(json.configs[0].displayName).toEqual({
expect(configs).toHaveLength(1);
expect(configs[0].displayName).toEqual({
color: 'white',
name: 'Config from js file',
});
Expand Down
15 changes: 3 additions & 12 deletions e2e/__tests__/multiProjectRunner.test.ts
Expand Up @@ -9,7 +9,7 @@ import {tmpdir} from 'os';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import {cleanup, extractSummary, sortLines, writeFiles} from '../Utils';
import runJest from '../runJest';
import runJest, {getConfig} from '../runJest';

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

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

const {stdout: configOutput} = runJest(DIR, [
'--show-config',
'--projects',
'project1',
'project2',
]);

const {configs} = JSON.parse(configOutput);
const {configs} = getConfig(DIR, ['--projects', 'project1', 'project2']);

expect(configs).toHaveLength(2);

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

const {stdout: configOutput} = runJest(DIR, ['--show-config']);

const {configs} = JSON.parse(configOutput);
const {configs} = getConfig(DIR);

expect(configs).toHaveLength(2);

Expand Down
22 changes: 22 additions & 0 deletions e2e/runJest.ts
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path';
import * as fs from 'fs';
import {Writable} from 'stream';
import execa = require('execa');
import type {Config} from '@jest/types';
import type {FormattedTestResults} from '@jest/test-result';
import stripAnsi = require('strip-ansi');
import {normalizeIcons} from './Utils';
Expand Down Expand Up @@ -214,3 +215,24 @@ export const runContinuous = function (
},
};
};

// return type matches output of logDebugMessages
export function getConfig(
dir: string,
args: Array<string> = [],
options?: RunJestOptions,
): {
globalConfig: Config.GlobalConfig;
configs: Array<Config.ProjectConfig>;
version: string;
} {
const {exitCode, stdout} = runJest(
dir,
args.concat('--show-config'),
options,
);

expect(exitCode).toBe(0);

return JSON.parse(stdout);
}
1 change: 1 addition & 0 deletions packages/jest-core/src/lib/log_debug_messages.ts
Expand Up @@ -9,6 +9,7 @@ import type {Config} from '@jest/types';

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

// if the output here changes, update `getConfig` in e2e/runJest.ts
export default function logDebugMessages(
globalConfig: Config.GlobalConfig,
configs: Array<Config.ProjectConfig> | Config.ProjectConfig,
Expand Down
7 changes: 5 additions & 2 deletions packages/test-utils/src/ConditionalTest.ts
Expand Up @@ -41,12 +41,15 @@ export function onNodeVersions(
versionRange: string,
testBody: () => void,
): void {
const description = `on node ${versionRange}`;
if (!semver.satisfies(process.versions.node, versionRange)) {
describe.skip(`[SKIP] tests that require node ${versionRange}`, () => {
describe.skip(description, () => {
testBody();
});
} else {
testBody();
describe(description, () => {
testBody();
});
}
}

Expand Down

0 comments on commit 2edd5c4

Please sign in to comment.