Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add helper for getting Jest's config in e2e tests #9770

Merged
merged 4 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions e2e/__tests__/config.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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