Skip to content

Commit

Permalink
Fix util.promisify(setTimeout) on jest-fake-timers (#9180)
Browse files Browse the repository at this point in the history
* Fix util.promisify(setTimeout) on jest-fake-timers

* Assert for promisify on instalation describe

* Fix typo in var

* Change custom timeout to not call the resolve function

* Expect promise to be resolved

* Add changelog

* Apply review changes

* Fix eslint

* Update CHANGELOG.md

* Run CI again

* Run CI again

* Run CI again
  • Loading branch information
lourenci authored and jeysal committed Nov 29, 2019
1 parent 429877d commit 5759a85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -62,6 +62,7 @@
- `[jest-types]` [**BREAKING**] Use less `null | undefined` in config types ([#9200](https://github.com/facebook/jest/pull/9200))
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))
- `[jest-fake-timers]` Support `util.promisify` on `setTimeout` ([#9180](https://github.com/facebook/jest/pull/9180))

### Chore & Maintenance

Expand Down
15 changes: 15 additions & 0 deletions packages/jest-fake-timers/src/__tests__/jestFakeTimers.test.ts
Expand Up @@ -8,6 +8,7 @@
import {runInNewContext} from 'vm';
import mock = require('jest-mock');
import FakeTimers from '../jestFakeTimers';
import util = require('util');

const timerConfig = {
idToRef: (id: number) => id,
Expand Down Expand Up @@ -40,6 +41,20 @@ describe('FakeTimers', () => {
expect(global.setTimeout).not.toBe(undefined);
});

it('accepts to promisify setTimeout mock', async () => {
const global = ({process} as unknown) as NodeJS.Global;
const timers = new FakeTimers({
config,
global,
moduleMocker,
timerConfig,
});
timers.useFakeTimers();
const timeoutPromise = util.promisify(global.setTimeout)(0, 'resolved');
timers.runAllTimers();
await expect(timeoutPromise).resolves.toBe('resolved');
});

it('installs clearTimeout mock', () => {
const global = ({process} as unknown) as NodeJS.Global;
const timers = new FakeTimers({
Expand Down
10 changes: 9 additions & 1 deletion packages/jest-fake-timers/src/jestFakeTimers.ts
Expand Up @@ -8,6 +8,7 @@
import {ModuleMocker} from 'jest-mock';
import {StackTraceConfig, formatStackTrace} from 'jest-message-util';
import {setGlobal} from 'jest-util';
import util = require('util');

type Callback = (...args: Array<unknown>) => void;

Expand Down Expand Up @@ -368,6 +369,13 @@ export default class FakeTimers<TimerRef> {
// @ts-ignore TODO: figure out better typings here
this._moduleMocker.fn().mockImplementation(impl);

const promisifiableFakeSetTimeout = fn(this._fakeSetTimeout.bind(this));
promisifiableFakeSetTimeout[util.promisify.custom] = (
delay?: number,
arg?: any,
) =>
new Promise(resolve => promisifiableFakeSetTimeout(resolve, delay, arg));

// TODO: add better typings; these are mocks, but typed as regular timers
this._fakeTimerAPIs = {
clearImmediate: fn(this._fakeClearImmediate.bind(this)),
Expand All @@ -376,7 +384,7 @@ export default class FakeTimers<TimerRef> {
nextTick: fn(this._fakeNextTick.bind(this)),
setImmediate: fn(this._fakeSetImmediate.bind(this)),
setInterval: fn(this._fakeSetInterval.bind(this)),
setTimeout: fn(this._fakeSetTimeout.bind(this)),
setTimeout: promisifiableFakeSetTimeout,
};
}

Expand Down

0 comments on commit 5759a85

Please sign in to comment.