in packages/jest-runtime/src/index.ts [1979:2187]
private _createJestObjectFor(from: string): Jest {
const disableAutomock = () => {
this._shouldAutoMock = false;
return jestObject;
};
const enableAutomock = () => {
this._shouldAutoMock = true;
return jestObject;
};
const unmock = (moduleName: string) => {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
{conditions: this.cjsConditions},
);
this._explicitShouldMock.set(moduleID, false);
return jestObject;
};
const deepUnmock = (moduleName: string) => {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
{conditions: this.cjsConditions},
);
this._explicitShouldMock.set(moduleID, false);
this._transitiveShouldMock.set(moduleID, false);
return jestObject;
};
const mock: Jest['mock'] = (moduleName, mockFactory, options) => {
if (mockFactory !== undefined) {
return setMockFactory(moduleName, mockFactory, options);
}
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
{conditions: this.cjsConditions},
);
this._explicitShouldMock.set(moduleID, true);
return jestObject;
};
const setMockFactory = (
moduleName: string,
mockFactory: () => unknown,
options?: {virtual?: boolean},
) => {
this.setMock(from, moduleName, mockFactory, options);
return jestObject;
};
const mockModule: Jest['unstable_mockModule'] = (
moduleName,
mockFactory,
options,
) => {
if (typeof mockFactory !== 'function') {
throw new Error('`unstable_mockModule` must be passed a mock factory');
}
this.setModuleMock(from, moduleName, mockFactory, options);
return jestObject;
};
const clearAllMocks = () => {
this.clearAllMocks();
return jestObject;
};
const resetAllMocks = () => {
this.resetAllMocks();
return jestObject;
};
const restoreAllMocks = () => {
this.restoreAllMocks();
return jestObject;
};
const _getFakeTimers = () => {
if (
this.isTornDown ||
!(this._environment.fakeTimers || this._environment.fakeTimersModern)
) {
this._logFormattedReferenceError(
'You are trying to access a property or method of the Jest environment after it has been torn down.',
);
process.exitCode = 1;
}
return this._fakeTimersImplementation!;
};
const useFakeTimers: Jest['useFakeTimers'] = (type = 'modern') => {
if (type === 'legacy') {
this._fakeTimersImplementation = this._environment.fakeTimers;
} else {
this._fakeTimersImplementation = this._environment.fakeTimersModern;
}
this._fakeTimersImplementation!.useFakeTimers();
return jestObject;
};
const useRealTimers = () => {
_getFakeTimers().useRealTimers();
return jestObject;
};
const resetModules = () => {
this.resetModules();
return jestObject;
};
const isolateModules = (fn: () => void) => {
this.isolateModules(fn);
return jestObject;
};
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
const mocked =
this._moduleMocker.mocked?.bind(this._moduleMocker) ??
(() => {
throw new Error(
'Your test environment does not support `mocked`, please update it.',
);
});
const setTimeout = (timeout: number) => {
// @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587
this._environment.global[testTimeoutSymbol] = timeout;
return jestObject;
};
const retryTimes = (numTestRetries: number) => {
// @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587
this._environment.global[retryTimesSymbol] = numTestRetries;
return jestObject;
};
const jestObject: Jest = {
advanceTimersByTime: (msToRun: number) =>
_getFakeTimers().advanceTimersByTime(msToRun),
advanceTimersToNextTimer: (steps?: number) =>
_getFakeTimers().advanceTimersToNextTimer(steps),
autoMockOff: disableAutomock,
autoMockOn: enableAutomock,
clearAllMocks,
clearAllTimers: () => _getFakeTimers().clearAllTimers(),
createMockFromModule: (moduleName: string) =>
this._generateMock(from, moduleName),
deepUnmock,
disableAutomock,
doMock: mock,
dontMock: unmock,
enableAutomock,
fn,
genMockFromModule: (moduleName: string) =>
this._generateMock(from, moduleName),
getRealSystemTime: () => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this._environment.fakeTimersModern) {
return fakeTimers.getRealSystemTime();
} else {
throw new TypeError(
'getRealSystemTime is not available when not using modern timers',
);
}
},
getTimerCount: () => _getFakeTimers().getTimerCount(),
isMockFunction: this._moduleMocker.isMockFunction,
isolateModules,
mock,
mocked,
requireActual: this.requireActual.bind(this, from),
requireMock: this.requireMock.bind(this, from),
resetAllMocks,
resetModules,
restoreAllMocks,
retryTimes,
runAllImmediates: () => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this._environment.fakeTimers) {
fakeTimers.runAllImmediates();
} else {
throw new TypeError(
'runAllImmediates is not available when using modern timers',
);
}
},
runAllTicks: () => _getFakeTimers().runAllTicks(),
runAllTimers: () => _getFakeTimers().runAllTimers(),
runOnlyPendingTimers: () => _getFakeTimers().runOnlyPendingTimers(),
setMock: (moduleName: string, mock: unknown) =>
setMockFactory(moduleName, () => mock),
setSystemTime: (now?: number | Date) => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this._environment.fakeTimersModern) {
fakeTimers.setSystemTime(now);
} else {
throw new TypeError(
'setSystemTime is not available when not using modern timers',
);
}
},
setTimeout,
spyOn,
unmock,
unstable_mockModule: mockModule,
useFakeTimers,
useRealTimers,
};
return jestObject;
}