Skip to content

Commit

Permalink
remove spying on fake timer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 26, 2018
1 parent cd8003a commit 4f56345
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 109 deletions.
9 changes: 5 additions & 4 deletions examples/timer/__tests__/infinite_timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
jest.useFakeTimers();

it('schedules a 10-second timer after 1 second', () => {
jest.spyOn(global, 'setTimeout');
const infiniteTimerGame = require('../infiniteTimerGame');
const callback = jest.fn();

infiniteTimerGame(callback);

// At this point in time, there should have been a single call to
// setTimeout to schedule the end of the game in 1 second.
expect(setTimeout.mock.calls.length).toBe(1);
expect(setTimeout.mock.calls[0][1]).toBe(1000);
expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);

// Fast forward and exhaust only currently pending timers
// (but not any new timers that get created during that process)
Expand All @@ -24,6 +25,6 @@ it('schedules a 10-second timer after 1 second', () => {

// And it should have created a new timer to start the game over in
// 10 seconds
expect(setTimeout.mock.calls.length).toBe(2);
expect(setTimeout.mock.calls[1][1]).toBe(10000);
expect(setTimeout).toBeCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 10000);
});
11 changes: 7 additions & 4 deletions examples/timer/__tests__/timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
jest.useFakeTimers();

describe('timerGame', () => {
beforeEach(() => {
jest.spyOn(global, 'setTimeout');
});
it('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
timerGame();

expect(setTimeout.mock.calls.length).toBe(1);
expect(setTimeout.mock.calls[0][1]).toBe(1000);
expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toBeCalledWith(expect.any(Function), 1000);
});

it('calls the callback after 1 second via runAllTimers', () => {
Expand All @@ -27,7 +30,7 @@ describe('timerGame', () => {

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback.mock.calls.length).toBe(1);
expect(callback).toBeCalledTimes(1);
});

it('calls the callback after 1 second via advanceTimersByTime', () => {
Expand All @@ -44,6 +47,6 @@ describe('timerGame', () => {

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback.mock.calls.length).toBe(1);
expect(callback).toBeCalledTimes(1);
});
});
1 change: 0 additions & 1 deletion packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class JSDOMEnvironment {
this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class NodeEnvironment {
this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/jest-jasmine2/src/__tests__/p_timeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jest.useFakeTimers();
import pTimeout from '../p_timeout';

describe('pTimeout', () => {
beforeEach(() => {
jest.spyOn(global, 'setTimeout');
jest.spyOn(global, 'clearTimeout');
});

it('calls `clearTimeout` and resolves when `promise` resolves.', async () => {
const onTimeout = jest.fn();
const promise = Promise.resolve();
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"slash": "^2.0.0",
"source-map": "^0.6.0"
},
"devDependencies": {
"jest-mock": "^23.2.0"
},
"engines": {
"node": ">= 6"
}
Expand Down
Loading

0 comments on commit 4f56345

Please sign in to comment.