Skip to content

Commit

Permalink
Implement node Timer api when running in node environment. (#4622)
Browse files Browse the repository at this point in the history
* Implement node Timer api when running in node environment.

* CR feedback, use toBeUndefined.

* CR feedback, turn Faketimer constructor args into an object..

* tweak Faketimer tests, assert if ref/unref are actually functions.
  • Loading branch information
skrivle authored and cpojer committed Oct 8, 2017
1 parent e04e451 commit 66329e8
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const JSDomEnvironment = require.requireActual('../');

describe('JSDomEnvironment', () => {
it('should configure setTimeout/setInterval to use the browser api', () => {
const env1 = new JSDomEnvironment({});

env1.fakeTimers.useFakeTimers();

const timer1 = env1.global.setTimeout(() => {}, 0);
const timer2 = env1.global.setInterval(() => {}, 0);

[timer1, timer2].forEach(timer => {
expect(typeof timer).toBe('number');
});
});
});
15 changes: 13 additions & 2 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import JSDom from 'jsdom';

class JSDOMEnvironment {
document: ?Object;
fakeTimers: ?FakeTimers;
fakeTimers: ?FakeTimers<number>;
global: ?Global;
moduleMocker: ?ModuleMocker;

Expand All @@ -44,7 +44,18 @@ class JSDOMEnvironment {
}

this.moduleMocker = new mock.ModuleMocker(global);
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);

const timerConfig = {
idToRef: (id: number) => id,
refToId: (ref: number) => ref,
};

this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
timerConfig,
});
}

dispose(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ describe('NodeEnvironment', () => {

expect(env1.global.global).toBe(env1.global);
});

it('should configure setTimeout/setInterval to use the node api', () => {
const env1 = new NodeEnvironment({});

env1.fakeTimers.useFakeTimers();

const timer1 = env1.global.setTimeout(() => {}, 0);
const timer2 = env1.global.setInterval(() => {}, 0);

[timer1, timer2].forEach(timer => {
expect(timer.id).not.toBeUndefined();
expect(typeof timer.ref).toBe('function');
expect(typeof timer.unref).toBe('function');
});
});
});
33 changes: 31 additions & 2 deletions packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import vm from 'vm';
import {FakeTimers, installCommonGlobals} from 'jest-util';
import mock from 'jest-mock';

type Timer = {|
id: number,
ref: () => Timer,
unref: () => Timer,
|};

class NodeEnvironment {
context: ?vm$Context;
fakeTimers: ?FakeTimers;
fakeTimers: ?FakeTimers<Timer>;
global: ?Global;
moduleMocker: ?ModuleMocker;

Expand All @@ -33,7 +39,30 @@ class NodeEnvironment {
global.setTimeout = setTimeout;
installCommonGlobals(global, config.globals);
this.moduleMocker = new mock.ModuleMocker(global);
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);

const timerIdToRef = (id: number) => ({
id,
ref() {
return this;
},
unref() {
return this;
},
});

const timerRefToId = (timer: Timer) => timer.id;

const timerConfig = {
idToRef: timerIdToRef,
refToId: timerRefToId,
};

this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
timerConfig,
});
}

dispose() {
Expand Down
Loading

0 comments on commit 66329e8

Please sign in to comment.