From 5bda60b19cbf188d069b6e63c571908dec21e348 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 10:20:21 +0200 Subject: [PATCH 1/7] fix(circus): make sure to install globals before emitting `init` event --- .../legacy-code-todo-rewrite/jestAdapter.ts | 11 ++------- .../jestAdapterInit.ts | 23 ++++++++++++++++++- packages/jest-types/src/Circus.ts | 6 +++++ packages/jest-util/src/globsToMatcher.ts | 15 +++++------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 7ea8be508da0..aea197c64b60 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -43,24 +43,17 @@ const jestAdapter = async ( const {globals, snapshotState} = await initialize({ config, environment, + expect, getBabelTraverse, getPrettier, globalConfig, localRequire: runtime.requireModule.bind(runtime), parentProcess: process, sendMessageToJest, + setGlobalsForRuntime: runtime.setGlobalsForRuntime?.bind(runtime), testPath, }); - const runtimeGlobals = {expect, ...globals}; - // TODO: `jest-circus` might be newer than `jest-runtime` - remove `?.` for Jest 27 - runtime.setGlobalsForRuntime?.(runtimeGlobals); - - // TODO: `jest-circus` might be newer than `jest-config` - remove `??` for Jest 27 - if (config.injectGlobals ?? true) { - Object.assign(environment.global, runtimeGlobals); - } - if (config.timers === 'fake' || config.timers === 'legacy') { // during setup, this cannot be null (and it's fine to explode if it is) environment.fakeTimers!.useFakeTimers(); diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 2e0f38f08d13..a88a85f1af7b 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -38,19 +38,28 @@ import globals from '..'; type Process = NodeJS.Process; +type Expect = typeof import('expect'); + +interface JestGlobals extends Global.TestFrameworkGlobals { + expect: Expect; +} + export const initialize = async ({ config, environment, + expect, getPrettier, getBabelTraverse, globalConfig, localRequire, parentProcess, - testPath, sendMessageToJest, + setGlobalsForRuntime, + testPath, }: { config: Config.ProjectConfig; environment: JestEnvironment; + expect: Expect; getPrettier: () => null | any; getBabelTraverse: () => typeof BabelTraverse; globalConfig: Config.GlobalConfig; @@ -58,6 +67,7 @@ export const initialize = async ({ testPath: Config.Path; parentProcess: Process; sendMessageToJest?: TestFileEvent; + setGlobalsForRuntime?: (globals: JestGlobals) => void; }): Promise<{ globals: Global.TestFrameworkGlobals; snapshotState: SnapshotStateType; @@ -120,9 +130,20 @@ export const initialize = async ({ addEventHandler(environment.handleTestEvent.bind(environment)); } + // @ts-expect-error + const runtimeGlobals: JestGlobals = {expect, ...globals}; + // TODO: `jest-circus` might be newer than `jest-runtime` - remove `?.` for Jest 27 + setGlobalsForRuntime?.(runtimeGlobals); + + // TODO: `jest-circus` might be newer than `jest-config` - remove `??` for Jest 27 + if (config.injectGlobals ?? true) { + Object.assign(environment.global, runtimeGlobals); + } + await dispatch({ name: 'setup', parentProcess, + runtimeGlobals, testNamePattern: globalConfig.testNamePattern, }); diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index 5dec97bda397..b4c94d57c3b6 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -38,6 +38,11 @@ export interface EventHandler { export type Event = SyncEvent | AsyncEvent; +interface JestGlobals extends Global.TestFrameworkGlobals { + // we cannot type `expect` properly as it'd create circular dependencies + expect: unknown; +} + export type SyncEvent = | { asyncError: Error; @@ -77,6 +82,7 @@ export type AsyncEvent = // first action to dispatch. Good time to initialize all settings name: 'setup'; testNamePattern?: string; + runtimeGlobals: JestGlobals; parentProcess: Process; } | { diff --git a/packages/jest-util/src/globsToMatcher.ts b/packages/jest-util/src/globsToMatcher.ts index e100d42cfcbf..ad4285825efe 100644 --- a/packages/jest-util/src/globsToMatcher.ts +++ b/packages/jest-util/src/globsToMatcher.ts @@ -9,12 +9,11 @@ import micromatch = require('micromatch'); import type {Config} from '@jest/types'; import replacePathSepForGlob from './replacePathSepForGlob'; +type Matcher = (str: Config.Path) => boolean; + const globsToMatchersMap = new Map< string, - { - isMatch: (str: string) => boolean; - negated: boolean; - } + {isMatch: Matcher; negated: boolean} >(); const micromatchOptions = {dot: true}; @@ -36,13 +35,11 @@ const micromatchOptions = {dot: true}; * isMatch('pizza.js'); // true * isMatch('pizza.test.js'); // false */ -export default function globsToMatcher( - globs: Array, -): (path: Config.Path) => boolean { +export default function globsToMatcher(globs: Array): Matcher { if (globs.length === 0) { // Since there were no globs given, we can simply have a fast path here and // return with a very simple function. - return (_: Config.Path): boolean => false; + return () => false; } const matchers = globs.map(glob => { @@ -62,7 +59,7 @@ export default function globsToMatcher( return globsToMatchersMap.get(glob)!; }); - return (path: Config.Path): boolean => { + return path => { const replacedPath = replacePathSepForGlob(path); let kept = undefined; let negatives = 0; From b1f120969de8b4477197db4e300e09dd666ba1b4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 10:35:48 +0200 Subject: [PATCH 2/7] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f29887d759a1..ca4824eff86e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-circus]` Setup globals before emitting `init`, and include Jets globals in the `init` payload ([#10598](https://github.com/facebook/jest/pull/10598)) + ### Chore & Maintenance ### Performance From 0f243e7f292286e3f756b2f5b5a4954f90e72dba Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 10:36:00 +0200 Subject: [PATCH 3/7] ergh --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca4824eff86e..4f5ebc8741ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-circus]` Setup globals before emitting `init`, and include Jets globals in the `init` payload ([#10598](https://github.com/facebook/jest/pull/10598)) +- `[jest-circus]` Setup globals before emitting `init`, and include Jest globals in the `init` payload ([#10598](https://github.com/facebook/jest/pull/10598)) ### Chore & Maintenance From 4c1b3e720555686cc202ffbf0bdcdd0cb01798f1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 11:17:57 +0200 Subject: [PATCH 4/7] spread correct object --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index a88a85f1af7b..bce0bfa27483 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -130,8 +130,7 @@ export const initialize = async ({ addEventHandler(environment.handleTestEvent.bind(environment)); } - // @ts-expect-error - const runtimeGlobals: JestGlobals = {expect, ...globals}; + const runtimeGlobals: JestGlobals = {expect, ...globalsObject}; // TODO: `jest-circus` might be newer than `jest-runtime` - remove `?.` for Jest 27 setGlobalsForRuntime?.(runtimeGlobals); From d9ab859ebdaed3aa02e39845228d55b2cc0dde03 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 11:22:00 +0200 Subject: [PATCH 5/7] export Expect type --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 3 +-- .../jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index bce0bfa27483..fe12fc99d464 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -35,11 +35,10 @@ import {getTestID} from '../utils'; import run from '../run'; import testCaseReportHandler from '../testCaseReportHandler'; import globals from '..'; +import type {Expect} from './jestExpect'; type Process = NodeJS.Process; -type Expect = typeof import('expect'); - interface JestGlobals extends Global.TestFrameworkGlobals { expect: Expect; } diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts index 3b67860c255e..81a62f3cfe75 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts @@ -16,7 +16,9 @@ import { toThrowErrorMatchingSnapshot, } from 'jest-snapshot'; -export default (config: Pick): typeof expect => { +export type Expect = typeof expect; + +export default (config: Pick): Expect => { expect.setState({expand: config.expand}); expect.extend({ toMatchInlineSnapshot, From b5477f30f2a1841c6290384fdb383386b1901f0b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 11:34:16 +0200 Subject: [PATCH 6/7] don't pass around expect --- .../src/legacy-code-todo-rewrite/jestAdapter.ts | 9 +-------- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 9 +++++---- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index aea197c64b60..bddb5d5606a0 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import * as path from 'path'; import type {Config} from '@jest/types'; import type {JestEnvironment} from '@jest/environment'; import type {TestResult} from '@jest/test-result'; @@ -14,8 +13,7 @@ import type {RuntimeType as Runtime} from 'jest-runtime'; import type {SnapshotStateType} from 'jest-snapshot'; import {deepCyclicCopy} from 'jest-util'; -const FRAMEWORK_INITIALIZER = path.resolve(__dirname, './jestAdapterInit.js'); -const EXPECT_INITIALIZER = path.resolve(__dirname, './jestExpect.js'); +const FRAMEWORK_INITIALIZER = require.resolve('./jestAdapterInit'); const jestAdapter = async ( globalConfig: Config.GlobalConfig, @@ -32,10 +30,6 @@ const jestAdapter = async ( FRAMEWORK_INITIALIZER, ); - const expect = runtime - .requireInternalModule(EXPECT_INITIALIZER) - .default(globalConfig); - const getPrettier = () => config.prettierPath ? require(config.prettierPath) : null; const getBabelTraverse = () => require('@babel/traverse').default; @@ -43,7 +37,6 @@ const jestAdapter = async ( const {globals, snapshotState} = await initialize({ config, environment, - expect, getBabelTraverse, getPrettier, globalConfig, diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index fe12fc99d464..b8820e32fe80 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -35,7 +35,7 @@ import {getTestID} from '../utils'; import run from '../run'; import testCaseReportHandler from '../testCaseReportHandler'; import globals from '..'; -import type {Expect} from './jestExpect'; +import createExpect, {Expect} from './jestExpect'; type Process = NodeJS.Process; @@ -46,7 +46,6 @@ interface JestGlobals extends Global.TestFrameworkGlobals { export const initialize = async ({ config, environment, - expect, getPrettier, getBabelTraverse, globalConfig, @@ -58,7 +57,6 @@ export const initialize = async ({ }: { config: Config.ProjectConfig; environment: JestEnvironment; - expect: Expect; getPrettier: () => null | any; getBabelTraverse: () => typeof BabelTraverse; globalConfig: Config.GlobalConfig; @@ -129,7 +127,10 @@ export const initialize = async ({ addEventHandler(environment.handleTestEvent.bind(environment)); } - const runtimeGlobals: JestGlobals = {expect, ...globalsObject}; + const runtimeGlobals: JestGlobals = { + ...globalsObject, + expect: createExpect(globalConfig), + }; // TODO: `jest-circus` might be newer than `jest-runtime` - remove `?.` for Jest 27 setGlobalsForRuntime?.(runtimeGlobals); From fad6d6b4f4b6bf9db0b703bb7d980f45df254951 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 12:20:01 +0200 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93f5b553be9c..1a9662f3aec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-circus]` Setup globals before emitting `init`, and include Jest globals in the `init` payload ([#10598](https://github.com/facebook/jest/pull/10598)) +- `[jest-circus]` Setup globals before emitting `setup`, and include Jest globals in the `setup` payload ([#10598](https://github.com/facebook/jest/pull/10598)) - `[jest-mock]` Fix typings for `mockResolvedValue`, `mockResolvedValueOnce`, `mockRejectedValue` and `mockRejectedValueOnce` ([#10600](https://github.com/facebook/jest/pull/10600)) ### Chore & Maintenance