forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jest-jasmine2] Rewrite
QueueRunner
. (jestjs#3187)
* [jest-jasmine2] Rewrite `QueueRunner`. * Make `FakeTimer` test stack agnostic. * Update snapshot. Manually. * Fix `FakeTimer`. * Lint. * Do not use async. * Revert "Merge pull request jestjs#1 from wtgtybhertgeghgtwtg/wtgtybhertgeghgtwtg-patch-1" This reverts commit 9424111, reversing changes made to ea0bb8a.
- Loading branch information
1 parent
184ca29
commit 98bc0a4
Showing
9 changed files
with
280 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'use strict'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
const pTimeout = require('../p-timeout'); | ||
|
||
describe('pTimeout', () => { | ||
it('calls `clearTimeout` and resolves when `promise` resolves.', async () => { | ||
const onTimeout = jest.fn(); | ||
const promise = Promise.resolve(); | ||
await pTimeout(promise, 1000, clearTimeout, setTimeout, onTimeout); | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(clearTimeout).toHaveBeenCalled(); | ||
expect(onTimeout).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls `clearTimeout` and rejects when `promise` rejects.', async () => { | ||
const onTimeout = jest.fn(); | ||
const promise = Promise.reject(); | ||
try { | ||
await pTimeout(promise, 1000, clearTimeout, setTimeout, onTimeout); | ||
} catch (e) { } | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(clearTimeout).toHaveBeenCalled(); | ||
expect(onTimeout).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls `onTimeout` on timeout.', async () => { | ||
const onTimeout = jest.fn(); | ||
// A Promise that never resolves or rejects. | ||
const promise = new Promise(() => {}); | ||
const timeoutPromise = | ||
pTimeout(promise, 1000, clearTimeout, setTimeout, onTimeout); | ||
jest.runAllTimers(); | ||
await timeoutPromise; | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(onTimeout).toHaveBeenCalled(); | ||
}); | ||
}); |
120 changes: 120 additions & 0 deletions
120
packages/jest-jasmine2/src/__tests__/queueRunner-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'use strict'; | ||
|
||
const queueRunner = require('../queueRunner'); | ||
|
||
describe('queueRunner', () => { | ||
it('runs every function in the queue.', async () => { | ||
const fnOne = jest.fn(next => next()); | ||
const fnTwo = jest.fn(next => next()); | ||
const onComplete = jest.fn(); | ||
const options = { | ||
clearTimeout, | ||
fail: () => {}, | ||
onComplete, | ||
onException: () => {}, | ||
queueableFns: [{ | ||
fn: fnOne, | ||
}, { | ||
fn: fnTwo, | ||
}], | ||
setTimeout, | ||
}; | ||
await queueRunner(options); | ||
expect(fnOne).toHaveBeenCalled(); | ||
expect(fnTwo).toHaveBeenCalled(); | ||
expect(onComplete).toHaveBeenCalled(); | ||
}); | ||
|
||
it('exposes `fail` to `next`.', async () => { | ||
const fail = jest.fn(); | ||
const fnOne = jest.fn(next => next.fail()); | ||
const fnTwo = jest.fn(next => next()); | ||
const onComplete = jest.fn(); | ||
const options = { | ||
clearTimeout, | ||
fail, | ||
onComplete, | ||
onException: () => {}, | ||
queueableFns: [{ | ||
fn: fnOne, | ||
}, { | ||
fn: fnTwo, | ||
}], | ||
setTimeout, | ||
}; | ||
await queueRunner(options); | ||
expect(fnOne).toHaveBeenCalled(); | ||
expect(fail).toHaveBeenCalled(); | ||
// Even if `fail` is called, the queue keeps running. | ||
expect(fnTwo).toHaveBeenCalled(); | ||
expect(onComplete).toHaveBeenCalled(); | ||
}); | ||
|
||
it('passes errors to `onException`.', async () => { | ||
const error = new Error('The error a test throws.'); | ||
const fnOne = jest.fn(() => { | ||
throw error; | ||
}); | ||
const fnTwo = jest.fn(next => next()); | ||
const onComplete = jest.fn(); | ||
const onException = jest.fn(); | ||
const options = { | ||
clearTimeout, | ||
fail: () => {}, | ||
onComplete, | ||
onException, | ||
queueableFns: [{ | ||
fn: fnOne, | ||
}, { | ||
fn: fnTwo, | ||
}], | ||
setTimeout, | ||
}; | ||
await queueRunner(options); | ||
expect(fnOne).toHaveBeenCalled(); | ||
expect(onException).toHaveBeenCalledWith(error); | ||
// Even if one of them errors, the queue keeps running. | ||
expect(fnTwo).toHaveBeenCalled(); | ||
expect(onComplete).toHaveBeenCalled(); | ||
}); | ||
|
||
it('passes an error to `onException` on timeout.', async () => { | ||
const fnOne = jest.fn(next => {}); | ||
const fnTwo = jest.fn(next => next()); | ||
const onComplete = jest.fn(); | ||
const onException = jest.fn(); | ||
const options = { | ||
clearTimeout, | ||
fail: () => {}, | ||
onComplete, | ||
onException, | ||
queueableFns: [{ | ||
fn: fnOne, | ||
// It times out in zero seconds. | ||
timeout: () => 0, | ||
}, { | ||
fn: fnTwo, | ||
}], | ||
setTimeout, | ||
}; | ||
await queueRunner(options); | ||
expect(fnOne).toHaveBeenCalled(); | ||
expect(onException).toHaveBeenCalled(); | ||
// i.e. the `message` of the error passed to `onException`. | ||
expect(onException.mock.calls[0][0].message).toEqual( | ||
'Timeout - Async callback was not invoked within timeout specified ' + | ||
'by jasmine.DEFAULT_TIMEOUT_INTERVAL.', | ||
); | ||
expect(fnTwo).toHaveBeenCalled(); | ||
expect(onComplete).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.