Skip to content

Commit

Permalink
test(jest-util): make basic assumptions in lolex true in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 25, 2017
1 parent 21b46d8 commit 1c16ddf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FakeTimers runAllTimers warns when trying to advance timers while real timers are used 1`] = `
"A function to advance timers was called but the timers API is not mocked with fake timers. Call \`jest.useFakeTimers()\` in this test or enable fake timers globally by setting \`\\"timers\\": \\"fake\\"\` in the configuration file. This warning is likely a result of a default configuration change in Jest 15.
Release Blog Post: https://facebook.github.io/jest/blog/2016/09/01/jest-15.html"
`;
exports[`FakeTimers runAllTimers warns when trying to advance timers while real timers are used 1`] = `"A function to advance timers was called but the timers API is not mocked with fake timers. Call \`jest.useFakeTimers()\` in this test or enable fake timers globally by setting \`\\"timers\\": \\"fake\\"\` in the configuration file"`;
60 changes: 42 additions & 18 deletions packages/jest-util/src/__tests__/fake_timers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ describe('FakeTimers', () => {
describe('construction', () => {
/* eslint-disable no-new */
it('installs setTimeout mock', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();
expect(global.setTimeout).not.toBe(undefined);
});

it('installs clearTimeout mock', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();
expect(global.clearTimeout).not.toBe(undefined);
});

it('installs setInterval mock', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();
expect(global.setInterval).not.toBe(undefined);
});

it('installs clearInterval mock', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();
expect(global.clearInterval).not.toBe(undefined);
Expand All @@ -58,6 +58,7 @@ describe('FakeTimers', () => {
it('mocks process.nextTick if it exists on global', () => {
const origNextTick = () => {};
const global = {
Date,
process: {
nextTick: origNextTick,
},
Expand All @@ -70,6 +71,7 @@ describe('FakeTimers', () => {
it('mocks setImmediate if it exists on global', () => {
const origSetImmediate = () => {};
const global = {
Date,
process,
setImmediate: origSetImmediate,
};
Expand All @@ -82,6 +84,7 @@ describe('FakeTimers', () => {
const origSetImmediate = () => {};
const origClearImmediate = () => {};
const global = {
Date,
clearImmediate: origClearImmediate,
process,
setImmediate: origSetImmediate,
Expand All @@ -95,6 +98,7 @@ describe('FakeTimers', () => {
describe('runAllTicks', () => {
it('runs all ticks, in order', () => {
const global = {
Date,
process: {
nextTick: () => {},
},
Expand Down Expand Up @@ -123,6 +127,7 @@ describe('FakeTimers', () => {
it('does nothing when no ticks have been scheduled', () => {
const nextTick = jest.genMockFn();
const global = {
Date,
process: {
nextTick,
},
Expand All @@ -137,6 +142,7 @@ describe('FakeTimers', () => {

it('only runs a scheduled callback once', () => {
const global = {
Date,
process: {
nextTick: () => {},
},
Expand All @@ -160,6 +166,7 @@ describe('FakeTimers', () => {
const nativeNextTick = jest.genMockFn();

const global = {
Date,
process: {
nextTick: nativeNextTick,
},
Expand All @@ -184,6 +191,7 @@ describe('FakeTimers', () => {
const nativeSetImmediate = jest.genMockFn();

const global = {
Date,
process,
setImmediate: nativeSetImmediate,
};
Expand All @@ -206,6 +214,7 @@ describe('FakeTimers', () => {
const nativeNextTick = jest.genMockFn();

const global = {
Date,
process: {
nextTick: nativeNextTick,
},
Expand All @@ -230,6 +239,7 @@ describe('FakeTimers', () => {
const nativeSetImmediate = jest.genMockFn();

const global = {
Date,
process,
setImmediate: nativeSetImmediate,
};
Expand All @@ -253,6 +263,7 @@ describe('FakeTimers', () => {
const nativeSetImmediate = jest.genMockFn();

const global = {
Date,
process,
setImmediate: nativeSetImmediate,
};
Expand All @@ -275,6 +286,7 @@ describe('FakeTimers', () => {

it('throws before allowing infinite recursion', () => {
const global = {
Date,
process: {
nextTick: () => {},
},
Expand Down Expand Up @@ -306,7 +318,7 @@ describe('FakeTimers', () => {

describe('runAllTimers', () => {
it('runs all timers in order', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand Down Expand Up @@ -349,6 +361,7 @@ describe('FakeTimers', () => {
it('does nothing when no timers have been scheduled', () => {
const nativeSetTimeout = jest.genMockFn();
const global = {
Date,
process,
setTimeout: nativeSetTimeout,
};
Expand All @@ -359,7 +372,7 @@ describe('FakeTimers', () => {
});

it('only runs a setTimeout callback once (ever)', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -375,7 +388,7 @@ describe('FakeTimers', () => {
});

it('runs callbacks with arguments after the interval', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -390,6 +403,7 @@ describe('FakeTimers', () => {
const nativeSetTimeout = jest.genMockFn();

const global = {
Date,
process,
setTimeout: nativeSetTimeout,
};
Expand All @@ -406,7 +420,7 @@ describe('FakeTimers', () => {
});

it('throws before allowing infinite recursion', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({
global,
maxLoops: 100,
Expand All @@ -423,14 +437,13 @@ describe('FakeTimers', () => {
timers.runAllTimers();
}).toThrow(
new Error(
"Ran 100 timers, and there are still more! Assuming we've hit an " +
'infinite recursion and bailing out...',
'Aborting after running 100 timers, assuming an infinite loop!',
),
);
});

it('also clears ticks', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -447,7 +460,7 @@ describe('FakeTimers', () => {

describe('advanceTimersByTime', () => {
it('runs timers in order', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand Down Expand Up @@ -486,15 +499,15 @@ describe('FakeTimers', () => {
});

it('does nothing when no timers have been scheduled', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

timers.advanceTimersByTime(100);
});

it('throws before allowing infinite recursion', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({
global,
maxLoops: 100,
Expand All @@ -520,7 +533,7 @@ describe('FakeTimers', () => {

describe('reset', () => {
it('resets all pending setTimeouts', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -533,7 +546,7 @@ describe('FakeTimers', () => {
});

it('resets all pending setIntervals', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -547,6 +560,7 @@ describe('FakeTimers', () => {

it('resets all pending ticks callbacks & immediates', () => {
const global = {
Date,
process: {
nextTick: () => {},
},
Expand All @@ -566,7 +580,7 @@ describe('FakeTimers', () => {
});

it('resets current advanceTimersByTime time cursor', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -587,6 +601,7 @@ describe('FakeTimers', () => {
const nativeSetImmediate = jest.genMockFn();

const global = {
Date,
process,
setImmediate: nativeSetImmediate,
};
Expand Down Expand Up @@ -638,7 +653,7 @@ describe('FakeTimers', () => {
});

it('does not run timers that were cleared in another timer', () => {
const global = {process};
const global = {Date, process};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
timers.useFakeTimers();

Expand All @@ -661,6 +676,7 @@ describe('FakeTimers', () => {
const nativeSetTimeout = jest.genMockFn();

const global = {
Date,
clearInterval: nativeClearInterval,
clearTimeout: nativeClearTimeout,
process,
Expand Down Expand Up @@ -706,6 +722,7 @@ describe('FakeTimers', () => {
const nativeSetTimeout = jest.genMockFn();

const global = {
Date,
clearInterval: nativeClearInterval,
clearTimeout: nativeClearTimeout,
process,
Expand Down Expand Up @@ -763,6 +780,7 @@ describe('FakeTimers', () => {
it('resets mock timer functions even if callback throws', () => {
const nativeSetTimeout = jest.genMockFn();
const global = {
Date,
process,
setTimeout: nativeSetTimeout,
};
Expand Down Expand Up @@ -792,6 +810,7 @@ describe('FakeTimers', () => {
const nativeClearInterval = jest.genMockFn();

const global = {
Date,
clearInterval: nativeClearInterval,
clearTimeout: nativeClearTimeout,
process,
Expand Down Expand Up @@ -820,6 +839,7 @@ describe('FakeTimers', () => {
const nativeProcessNextTick = jest.genMockFn();

const global = {
Date,
process: {nextTick: nativeProcessNextTick},
};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
Expand All @@ -839,6 +859,7 @@ describe('FakeTimers', () => {
const nativeClearImmediate = jest.genMockFn();

const global = {
Date,
clearImmediate: nativeClearImmediate,
process,
setImmediate: nativeSetImmediate,
Expand Down Expand Up @@ -866,6 +887,7 @@ describe('FakeTimers', () => {
const nativeClearInterval = jest.genMockFn();

const global = {
Date,
clearInterval: nativeClearInterval,
clearTimeout: nativeClearTimeout,
process,
Expand Down Expand Up @@ -894,6 +916,7 @@ describe('FakeTimers', () => {
const nativeProcessNextTick = jest.genMockFn();

const global = {
Date,
process: {nextTick: nativeProcessNextTick},
};
const timers = new FakeTimers({global, moduleMocker, timerConfig});
Expand All @@ -913,6 +936,7 @@ describe('FakeTimers', () => {
const nativeClearImmediate = jest.genMockFn();

const global = {
Date,
clearImmediate: nativeClearImmediate,
process,
setImmediate: nativeSetImmediate,
Expand Down

0 comments on commit 1c16ddf

Please sign in to comment.