Skip to content

Limit use of realTimers to Jest's fakeTimers #652

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,42 @@ describe('query container validation throws when validation fails', () => {
})
})

test('should always use realTimers before using callback', () => {
test('should always use realTimers before using callback when timers are faked with useFakeTimers', () => {
const originalSetTimeout = globalObj.setTimeout

// legacy timers use mocks and do not rely on a clock instance
jest.useFakeTimers('legacy')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
})
expect(globalObj.setTimeout._isMockFunction).toBe(true);
expect(globalObj.setTimeout.clock).toBeUndefined();

jest.useRealTimers()

// modern timers use a clock instance instead of a mock
jest.useFakeTimers('modern')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
})
expect(globalObj.setTimeout._isMockFunction).toBeUndefined();
expect(globalObj.setTimeout.clock).toBeDefined();
})

test('should not use realTimers when timers are not faked with useFakeTimers', () => {
const originalSetTimeout = globalObj.setTimeout;

// useFakeTimers is not used, timers are faked in some other way
const fakedSetTimeout = (callback) => {
callback();
};
fakedSetTimeout.clock = jest.fn();

globalObj.setTimeout = fakedSetTimeout;

runWithRealTimers(() => {
expect(fakedSetTimeout).toEqual(globalObj.setTimeout)
})

globalObj.setTimeout = originalSetTimeout;
});
27 changes: 21 additions & 6 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ const globalObj = typeof window === 'undefined' ? global : window

// Currently this fn only supports jest timers, but it could support other test runners in the future.
function runWithRealTimers(callback) {
const usingJestAndTimers = typeof jest !== 'undefined' && typeof globalObj.setTimeout !== 'undefined';
const usingLegacyJestFakeTimers = usingJestAndTimers && typeof globalObj.setTimeout._isMockFunction !== 'undefined' && globalObj.setTimeout._isMockFunction;

let usingModernJestFakeTimers = false;
if (
usingJestAndTimers &&
typeof globalObj.setTimeout.clock !== "undefined" &&
typeof jest.getRealSystemTime !== "undefined"
) {
try {
// jest.getRealSystemTime is only supported for Jest's `modern` fake timers and otherwise throws
jest.getRealSystemTime();
usingModernJestFakeTimers = true;
} catch {
// not using Jest's modern fake timers
}
}

const usingJestFakeTimers =
globalObj.setTimeout &&
(globalObj.setTimeout._isMockFunction ||
typeof globalObj.setTimeout.clock !== 'undefined') &&
typeof jest !== 'undefined'
usingLegacyJestFakeTimers || usingModernJestFakeTimers;

if (usingJestFakeTimers) {
jest.useRealTimers()
Expand All @@ -15,7 +30,7 @@ function runWithRealTimers(callback) {
const callbackReturnValue = callback()

if (usingJestFakeTimers) {
jest.useFakeTimers()
jest.useFakeTimers(usingModernJestFakeTimers ? 'modern' : 'legacy')
}

return callbackReturnValue
Expand All @@ -36,7 +51,7 @@ function getTimeFunctions() {
}
}

const {clearTimeoutFn, setImmediateFn, setTimeoutFn} = runWithRealTimers(
const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers(
getTimeFunctions,
)

Expand Down