diff --git a/packages/jest-circus/src/index.ts b/packages/jest-circus/src/index.ts index 10056acc358b..7c2ed471e548 100644 --- a/packages/jest-circus/src/index.ts +++ b/packages/jest-circus/src/index.ts @@ -12,6 +12,8 @@ import {ErrorWithStack, isPromise} from 'jest-util'; import {Circus, Global} from '@jest/types'; import {dispatch} from './state'; +import expect = require('expect'); + type THook = (fn: Circus.HookFn, timeout?: number) => void; type DescribeFn = ( blockName: Circus.BlockName, @@ -199,17 +201,43 @@ const test: Global.It = (() => { return test; })(); -const it: Global.It = test; +const it = test; + +const fdescribe = describe.only; +const fit = it.only; +const xit = it.skip; +const xdescribe = describe.skip; +const xtest = it.skip; export type Event = Circus.Event; export type State = Circus.State; -export {afterAll, afterEach, beforeAll, beforeEach, describe, it, test}; +export { + afterAll, + afterEach, + beforeAll, + beforeEach, + describe, + expect, + fdescribe, + fit, + it, + test, + xdescribe, + xit, + xtest, +}; export default { afterAll, afterEach, beforeAll, beforeEach, describe, + expect, + fdescribe, + fit, it, test, + xdescribe, + xit, + xtest, }; 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 202744c87b54..7850aa696e07 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -30,6 +30,7 @@ const jestAdapter = async ( .requireInternalModule(path.resolve(__dirname, './jestExpect.js')) .default({ expand: globalConfig.expand, + noJestGlobals: !globalConfig.noJestGlobals && !config.noJestGlobals, }); const getPrettier = () => 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 0b0f92095ba3..702f41dc108b 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -53,22 +53,18 @@ export const initialize = ({ testPath: Config.Path; parentProcess: Process; }) => { + if (!globalConfig.noJestGlobals && !config.noJestGlobals) { + const nodeGlobal = global as Global.Global; + Object.assign(nodeGlobal, globals); + } + if (globalConfig.testTimeout) { getRunnerState().testTimeout = globalConfig.testTimeout; } const mutex = throat(globalConfig.maxConcurrency); - const nodeGlobal = global as Global.Global; - Object.assign(nodeGlobal, globals); - - nodeGlobal.xit = nodeGlobal.it.skip; - nodeGlobal.xtest = nodeGlobal.it.skip; - nodeGlobal.xdescribe = nodeGlobal.describe.skip; - nodeGlobal.fit = nodeGlobal.it.only; - nodeGlobal.fdescribe = nodeGlobal.describe.only; - - nodeGlobal.test.concurrent = (test => { + (globals.test as Global.ItConcurrent).concurrent = (test => { const concurrent = ( testName: string, testFn: () => Promise, @@ -81,7 +77,7 @@ export const initialize = ({ // that will result in this test to be skipped, so we'll be executing the promise function anyway, // even if it ends up being skipped. const promise = mutex(() => testFn()); - nodeGlobal.test(testName, () => promise, timeout); + globals.test(testName, () => promise, timeout); }; concurrent.only = ( @@ -97,7 +93,7 @@ export const initialize = ({ concurrent.skip = test.skip; return concurrent; - })(nodeGlobal.test); + })(globals.test); addEventHandler(eventHandler); 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 fa5436dbaf94..0e2d0fe2e6da 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts @@ -15,8 +15,8 @@ import { toThrowErrorMatchingSnapshot, } from 'jest-snapshot'; -export default (config: {expand: boolean}) => { - global.expect = expect; +export default (config: {expand: boolean; noJestGlobals: boolean}) => { + if (!config.noJestGlobals) global.expect = expect; expect.setState({ expand: config.expand, }); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index aca89876a7c1..f0843d03f84e 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -257,6 +257,7 @@ export type GlobalConfig = { listTests: boolean; maxConcurrency: number; maxWorkers: number; + noJestGlobals?: boolean; noStackTrace: boolean; nonFlagArgs: Array; noSCM?: boolean; @@ -319,6 +320,7 @@ export type ProjectConfig = { modulePathIgnorePatterns: Array; modulePaths?: Array; name: string; + noJestGlobals?: boolean; prettierPath: string; resetMocks: boolean; resetModules: boolean;