Skip to content

Commit

Permalink
Add simple rAF polyfill in jsdom environment (jestjs#4568)
Browse files Browse the repository at this point in the history
* Add simple rAF polyfill in jsdom environment

Closes jestjs#4545

* Fix flow error

* Tweak test

* Try to log out stderr on CI

* Use snake case naming for test file

* Update to newest yarn on ci

* Revert "Try to log out stderr on CI"

This reverts commit 08d58c5.

* Remove extra -- from appveyor to avoid warning on newer yarn

* Include time since window initialised in rAF implementation
  • Loading branch information
SimenB authored and tabrindle committed Oct 2, 2017
1 parent 9c2b802 commit 1c6119c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ init:
install:
- ps: Install-Product node $env:nodejs_version x64
- node --version
- curl -fsSL -o yarn.js https://github.com/yarnpkg/yarn/releases/download/v0.28.4/yarn-0.28.4.js
- curl -fsSL -o yarn.js https://github.com/yarnpkg/yarn/releases/download/v1.1.0/yarn-1.1.0.js
- node ./yarn.js --version
- node ./yarn.js install
- node ./yarn.js run build
Expand All @@ -20,7 +20,7 @@ cache:
- .eslintcache

test_script:
- node ./yarn.js run jest -- --color
- node ./yarn.js run jest --color

# Don't actually build.
build: off
Expand Down
19 changes: 19 additions & 0 deletions integration_tests/__tests__/request_animation_frame.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

test('requestAnimationFrame', () => {
const result = runJest('request_animation_frame', ['--verbose']);
const stderr = result.stderr.toString();

expect(stderr).toMatch('requestAnimationFrame test');
expect(result.status).toBe(0);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.
*/

/* eslint-env browser */

'use strict';

test('requestAnimationFrame test', done => {
expect.hasAssertions();

requestAnimationFrame(timestamp => {
expect(true).toBe(true);
expect(timestamp).toBeGreaterThan(0);

done();
});
});
5 changes: 5 additions & 0 deletions integration_tests/request_animation_frame/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "jsdom"
}
}
11 changes: 11 additions & 0 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class JSDOMEnvironment {
moduleMocker: ?ModuleMocker;

constructor(config: ProjectConfig): void {
const jsdomInitialized = process.hrtime();
// lazy require
this.document = JSDom.jsdom('<!DOCTYPE html>', {
url: config.testURL,
Expand All @@ -32,6 +33,16 @@ class JSDOMEnvironment {
this.global.Error.stackTraceLimit = 100;
installCommonGlobals(global, config.globals);

if (!global.requestAnimationFrame) {
global.requestAnimationFrame = callback => {
const hr = process.hrtime(jsdomInitialized);
const hrInNano = hr[0] * 1e9 + hr[1];
const hrInMicro = hrInNano / 1e6;

return global.setTimeout(callback, 0, hrInMicro);
};
}

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

0 comments on commit 1c6119c

Please sign in to comment.