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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add noJestGlobals config, if false, no jest-circus globals #9306

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/jest-circus/src/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};
Expand Up @@ -30,6 +30,7 @@ const jestAdapter = async (
.requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
.default({
expand: globalConfig.expand,
noJestGlobals: !globalConfig.noJestGlobals && !config.noJestGlobals,
});

const getPrettier = () =>
Expand Down
Expand Up @@ -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<any>,
Expand All @@ -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 = (
Expand All @@ -97,7 +93,7 @@ export const initialize = ({
concurrent.skip = test.skip;

return concurrent;
})(nodeGlobal.test);
})(globals.test);

addEventHandler(eventHandler);

Expand Down
Expand Up @@ -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,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -257,6 +257,7 @@ export type GlobalConfig = {
listTests: boolean;
maxConcurrency: number;
maxWorkers: number;
noJestGlobals?: boolean;
noStackTrace: boolean;
nonFlagArgs: Array<string>;
noSCM?: boolean;
Expand Down Expand Up @@ -319,6 +320,7 @@ export type ProjectConfig = {
modulePathIgnorePatterns: Array<string>;
modulePaths?: Array<string>;
name: string;
noJestGlobals?: boolean;
prettierPath: string;
resetMocks: boolean;
resetModules: boolean;
Expand Down