From 2edd5c4724cfadb944a6a233a7ad8fb5a3e05996 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 Apr 2020 15:10:05 +0200 Subject: [PATCH] chore: add helper for getting Jest's config in e2e tests (#9770) --- e2e/__tests__/config.test.ts | 9 +++---- e2e/__tests__/esmConfigFile.test.ts | 25 ++++++++----------- e2e/__tests__/multiProjectRunner.test.ts | 15 +++-------- e2e/runJest.ts | 22 ++++++++++++++++ .../jest-core/src/lib/log_debug_messages.ts | 1 + packages/test-utils/src/ConditionalTest.ts | 7 ++++-- 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/e2e/__tests__/config.test.ts b/e2e/__tests__/config.test.ts index c4a1eee622fd..6d0d0cc57e32 100644 --- a/e2e/__tests__/config.test.ts +++ b/e2e/__tests__/config.test.ts @@ -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', [ @@ -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); }); diff --git a/e2e/__tests__/esmConfigFile.test.ts b/e2e/__tests__/esmConfigFile.test.ts index 893e258534cc..dc56308aa6bb 100644 --- a/e2e/__tests__/esmConfigFile.test.ts +++ b/e2e/__tests__/esmConfigFile.test.ts @@ -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', }); diff --git a/e2e/__tests__/multiProjectRunner.test.ts b/e2e/__tests__/multiProjectRunner.test.ts index ebb995a61f62..ba4d129e00c1 100644 --- a/e2e/__tests__/multiProjectRunner.test.ts +++ b/e2e/__tests__/multiProjectRunner.test.ts @@ -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'); @@ -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); @@ -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); diff --git a/e2e/runJest.ts b/e2e/runJest.ts index e35d11ec778c..98d1a6db4128 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -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'; @@ -214,3 +215,24 @@ export const runContinuous = function ( }, }; }; + +// return type matches output of logDebugMessages +export function getConfig( + dir: string, + args: Array = [], + options?: RunJestOptions, +): { + globalConfig: Config.GlobalConfig; + configs: Array; + version: string; +} { + const {exitCode, stdout} = runJest( + dir, + args.concat('--show-config'), + options, + ); + + expect(exitCode).toBe(0); + + return JSON.parse(stdout); +} diff --git a/packages/jest-core/src/lib/log_debug_messages.ts b/packages/jest-core/src/lib/log_debug_messages.ts index 57804ae9fcc0..944c3cb5a4c9 100644 --- a/packages/jest-core/src/lib/log_debug_messages.ts +++ b/packages/jest-core/src/lib/log_debug_messages.ts @@ -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, diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index 6046c5ca8c36..c501f1ff59af 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -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(); + }); } }