Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple rAF polyfill in jsdom environment #4568

Merged
merged 9 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have it beneath the JSDom.jsdom call, but that makes stuff fail with an odd flow error...

// 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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently working on getting it upstreamed into JSDOM, so I read some of the spec. I don't know if people use this argument, or expect it to be in any way accurate, but this is a best effort 🙂

};
}

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