From 2ad75af20d39fec5545405759b556d30e53e25f1 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Mon, 6 Dec 2021 07:51:38 +0200 Subject: [PATCH 1/3] refactor: use Symbol to pass `testTimeout` --- packages/jest-jasmine2/src/index.ts | 8 ++++++-- packages/jest-jasmine2/src/jasmine/Env.ts | 12 +++++++----- packages/jest-jasmine2/src/jasmine/jasmineLight.ts | 4 +++- packages/jest-jasmine2/src/types.ts | 1 - packages/jest-runtime/src/index.ts | 8 ++------ 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index efd1b01c30ec..7db6e8cdd340 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -23,6 +23,8 @@ const JASMINE = require.resolve('./jasmine/jasmineLight'); const jestEachBuildDir = path.dirname(require.resolve('jest-each')); +const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); + export default async function jasmine2( globalConfig: Config.GlobalConfig, config: Config.ProjectConfig, @@ -130,10 +132,12 @@ export default async function jasmine2( configurable: true, enumerable: true, get() { - return this._DEFAULT_TIMEOUT_INTERVAL; + // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 + return environment.global[testTimeoutSymbol]; }, set(value) { - this._DEFAULT_TIMEOUT_INTERVAL = value; + // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 + environment.global[testTimeoutSymbol] = value; }, }); } diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 20975e1e9550..079b9c1fd974 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -49,6 +49,8 @@ import type { import type {default as Spec, SpecResult} from './Spec'; import type Suite from './Suite'; +const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); + export default function (j$: Jasmine) { return class Env { specFilter: (spec: Spec) => boolean; @@ -510,7 +512,7 @@ export default function (j$: Jasmine) { queueableFn: { fn, timeout() { - return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; + return timeout || (global as any)[testTimeoutSymbol]; }, }, throwOnExpectationFailure, @@ -622,7 +624,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.beforeEach({ fn: beforeEachFunction, timeout() { - return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; + return timeout || (global as any)[testTimeoutSymbol]; }, }); }; @@ -631,7 +633,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.beforeAll({ fn: beforeAllFunction, timeout() { - return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; + return timeout || (global as any)[testTimeoutSymbol]; }, }); }; @@ -640,7 +642,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.afterEach({ fn: afterEachFunction, timeout() { - return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; + return timeout || (global as any)[testTimeoutSymbol]; }, }); }; @@ -649,7 +651,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.afterAll({ fn: afterAllFunction, timeout() { - return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; + return timeout || (global as any)[testTimeoutSymbol]; }, }); }; diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index b54a79d8927e..1c25c06100df 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -40,10 +40,12 @@ import Timer from './Timer'; import createSpy from './createSpy'; import SpyRegistry from './spyRegistry'; +const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); + export const create = function (createOptions: Record): Jasmine { const j$ = {...createOptions} as Jasmine; - j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.testTimeout || 5000; + (global as any)[testTimeoutSymbol] = createOptions.testTimeout || 5000; j$.getEnv = function () { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env()); diff --git a/packages/jest-jasmine2/src/types.ts b/packages/jest-jasmine2/src/types.ts index 4d0ff244005e..5e980f6b09f0 100644 --- a/packages/jest-jasmine2/src/types.ts +++ b/packages/jest-jasmine2/src/types.ts @@ -74,7 +74,6 @@ type JasmineMatcher = { export type JasmineMatchersObject = {[id: string]: JasmineMatcher}; export type Jasmine = { - _DEFAULT_TIMEOUT_INTERVAL: number; DEFAULT_TIMEOUT_INTERVAL: number; currentEnv_: ReturnType['prototype']; getEnv: () => ReturnType['prototype']; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 8640b45ea405..13cd14cbb242 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1926,12 +1926,8 @@ export default class Runtime { const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker); const setTimeout = (timeout: number) => { - if (this._environment.global.jasmine) { - this._environment.global.jasmine._DEFAULT_TIMEOUT_INTERVAL = timeout; - } else { - // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 - this._environment.global[testTimeoutSymbol] = timeout; - } + // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 + this._environment.global[testTimeoutSymbol] = timeout; return jestObject; }; From 4d175b4ef92655122b82d777ee4e40c2db928428 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Mon, 6 Dec 2021 10:03:19 +0200 Subject: [PATCH 2/3] use getter and setter --- packages/jest-jasmine2/src/index.ts | 8 ++------ packages/jest-jasmine2/src/jasmine/Env.ts | 12 +++++------- packages/jest-jasmine2/src/jasmine/jasmineLight.ts | 13 ++++++++++++- packages/jest-jasmine2/src/types.ts | 1 + 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 7db6e8cdd340..efd1b01c30ec 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -23,8 +23,6 @@ const JASMINE = require.resolve('./jasmine/jasmineLight'); const jestEachBuildDir = path.dirname(require.resolve('jest-each')); -const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); - export default async function jasmine2( globalConfig: Config.GlobalConfig, config: Config.ProjectConfig, @@ -132,12 +130,10 @@ export default async function jasmine2( configurable: true, enumerable: true, get() { - // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 - return environment.global[testTimeoutSymbol]; + return this._DEFAULT_TIMEOUT_INTERVAL; }, set(value) { - // @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587 - environment.global[testTimeoutSymbol] = value; + this._DEFAULT_TIMEOUT_INTERVAL = value; }, }); } diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 079b9c1fd974..20975e1e9550 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -49,8 +49,6 @@ import type { import type {default as Spec, SpecResult} from './Spec'; import type Suite from './Suite'; -const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); - export default function (j$: Jasmine) { return class Env { specFilter: (spec: Spec) => boolean; @@ -512,7 +510,7 @@ export default function (j$: Jasmine) { queueableFn: { fn, timeout() { - return timeout || (global as any)[testTimeoutSymbol]; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }, throwOnExpectationFailure, @@ -624,7 +622,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.beforeEach({ fn: beforeEachFunction, timeout() { - return timeout || (global as any)[testTimeoutSymbol]; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -633,7 +631,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.beforeAll({ fn: beforeAllFunction, timeout() { - return timeout || (global as any)[testTimeoutSymbol]; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -642,7 +640,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.afterEach({ fn: afterEachFunction, timeout() { - return timeout || (global as any)[testTimeoutSymbol]; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -651,7 +649,7 @@ export default function (j$: Jasmine) { currentDeclarationSuite.afterAll({ fn: afterAllFunction, timeout() { - return timeout || (global as any)[testTimeoutSymbol]; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index 1c25c06100df..be334b91531a 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -45,7 +45,18 @@ const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); export const create = function (createOptions: Record): Jasmine { const j$ = {...createOptions} as Jasmine; - (global as any)[testTimeoutSymbol] = createOptions.testTimeout || 5000; + Object.defineProperty(j$, '_DEFAULT_TIMEOUT_INTERVAL', { + configurable: true, + enumerable: true, + get() { + return ( + (global as any)[testTimeoutSymbol] || createOptions.testTimeout || 5000 + ); + }, + set(value) { + (global as any)[testTimeoutSymbol] = value; + }, + }); j$.getEnv = function () { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env()); diff --git a/packages/jest-jasmine2/src/types.ts b/packages/jest-jasmine2/src/types.ts index 5e980f6b09f0..4d0ff244005e 100644 --- a/packages/jest-jasmine2/src/types.ts +++ b/packages/jest-jasmine2/src/types.ts @@ -74,6 +74,7 @@ type JasmineMatcher = { export type JasmineMatchersObject = {[id: string]: JasmineMatcher}; export type Jasmine = { + _DEFAULT_TIMEOUT_INTERVAL: number; DEFAULT_TIMEOUT_INTERVAL: number; currentEnv_: ReturnType['prototype']; getEnv: () => ReturnType['prototype']; From 872314c64e053a0a1c5128d9db59bc8dd68b4d83 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Thu, 10 Feb 2022 12:47:07 +0200 Subject: [PATCH 3/3] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 341e2533493d..e78e10001802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924)) - `[jest-environment-node]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340)) - `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323)) +- `[jest-jasmine2, jest-runtime]` [**BREAKING**] Use `Symbol` to pass `jest.setTimeout` value instead of `jasmine` specific logic ([#12124](https://github.com/facebook/jest/pull/12124)) - `[jest-snapshot]` [**BREAKING**] Migrate to ESM ([#12342](https://github.com/facebook/jest/pull/12342)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343))