From 3c6b1206e197dfa10efd7db560a95b65f2541139 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 10:59:22 +0200 Subject: [PATCH 1/9] Add simple rAF polyfill in jsdom environment Closes #4545 --- .../__tests__/request-animation_frame.test.js | 19 +++++++++++++++++++ .../__tests__/request_animation_frame.test.js | 18 ++++++++++++++++++ .../request_animation_frame/package.json | 5 +++++ packages/jest-environment-jsdom/src/index.js | 6 ++++++ 4 files changed, 48 insertions(+) create mode 100644 integration_tests/__tests__/request-animation_frame.test.js create mode 100644 integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js create mode 100644 integration_tests/request_animation_frame/package.json diff --git a/integration_tests/__tests__/request-animation_frame.test.js b/integration_tests/__tests__/request-animation_frame.test.js new file mode 100644 index 000000000000..383291d1f604 --- /dev/null +++ b/integration_tests/__tests__/request-animation_frame.test.js @@ -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); +}); diff --git a/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js new file mode 100644 index 000000000000..2fa9164d3d67 --- /dev/null +++ b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js @@ -0,0 +1,18 @@ +/** + * 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', () => { + expect(true).toBe(true); + + requestAnimationFrame(() => { + throw new Error('Scheduled Error'); + }); +}); diff --git a/integration_tests/request_animation_frame/package.json b/integration_tests/request_animation_frame/package.json new file mode 100644 index 000000000000..0ded940b7cb7 --- /dev/null +++ b/integration_tests/request_animation_frame/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "jsdom" + } +} diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 2f957d2e9ac9..501608becc6b 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -32,6 +32,12 @@ class JSDOMEnvironment { this.global.Error.stackTraceLimit = 100; installCommonGlobals(global, config.globals); + if (!this.global.requestAnimationFrame) { + this.global.requestAnimationFrame = callback => { + this.global.setTimeout(callback, 0); + }; + } + this.moduleMocker = new mock.ModuleMocker(global); this.fakeTimers = new FakeTimers(global, this.moduleMocker, config); } From 617562dbbc33f539d73969a129bb906d7c926724 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 11:06:43 +0200 Subject: [PATCH 2/9] Fix flow error --- packages/jest-environment-jsdom/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 501608becc6b..9427d1313c77 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -32,9 +32,9 @@ class JSDOMEnvironment { this.global.Error.stackTraceLimit = 100; installCommonGlobals(global, config.globals); - if (!this.global.requestAnimationFrame) { - this.global.requestAnimationFrame = callback => { - this.global.setTimeout(callback, 0); + if (!global.requestAnimationFrame) { + global.requestAnimationFrame = callback => { + global.setTimeout(callback, 0); }; } From 484c26db33f1abd2bd9fdb10b6ecefe6b7e6080d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 11:43:16 +0200 Subject: [PATCH 3/9] Tweak test --- .../__tests__/request_animation_frame.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js index 2fa9164d3d67..4c16ba8756dc 100644 --- a/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js +++ b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js @@ -9,10 +9,12 @@ 'use strict'; -test('requestAnimationFrame test', () => { - expect(true).toBe(true); +test('requestAnimationFrame test', done => { + expect.hasAssertions(); requestAnimationFrame(() => { - throw new Error('Scheduled Error'); + expect(true).toBe(true); + + done(); }); }); From 08d58c54824e0f3b4b13c8354e13a2f27aac5be7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 12:50:03 +0200 Subject: [PATCH 4/9] Try to log out stderr on CI --- integration_tests/__tests__/request-animation_frame.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration_tests/__tests__/request-animation_frame.test.js b/integration_tests/__tests__/request-animation_frame.test.js index 383291d1f604..681859c7e062 100644 --- a/integration_tests/__tests__/request-animation_frame.test.js +++ b/integration_tests/__tests__/request-animation_frame.test.js @@ -14,6 +14,8 @@ test('requestAnimationFrame', () => { const result = runJest('request_animation_frame', ['--verbose']); const stderr = result.stderr.toString(); + console.log(stderr); + expect(stderr).toMatch('requestAnimationFrame test'); expect(result.status).toBe(0); }); From 23dd2bc650c8a904caa49fa8f7ca541320194c0e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 22:10:06 +0200 Subject: [PATCH 5/9] Use snake case naming for test file --- ...st-animation_frame.test.js => request_animation_frame.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename integration_tests/__tests__/{request-animation_frame.test.js => request_animation_frame.test.js} (100%) diff --git a/integration_tests/__tests__/request-animation_frame.test.js b/integration_tests/__tests__/request_animation_frame.test.js similarity index 100% rename from integration_tests/__tests__/request-animation_frame.test.js rename to integration_tests/__tests__/request_animation_frame.test.js From 7c91e679d9afa34fb9d58af4280e0f29accddcda Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 22:14:45 +0200 Subject: [PATCH 6/9] Update to newest yarn on ci --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 6c905cf67bab..937f0bab5e78 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 From 78b5ebf320df4237a1d8b8bd2d408d2d4c288340 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 22:20:30 +0200 Subject: [PATCH 7/9] Revert "Try to log out stderr on CI" This reverts commit 08d58c54824e0f3b4b13c8354e13a2f27aac5be7. --- integration_tests/__tests__/request_animation_frame.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/integration_tests/__tests__/request_animation_frame.test.js b/integration_tests/__tests__/request_animation_frame.test.js index 681859c7e062..383291d1f604 100644 --- a/integration_tests/__tests__/request_animation_frame.test.js +++ b/integration_tests/__tests__/request_animation_frame.test.js @@ -14,8 +14,6 @@ test('requestAnimationFrame', () => { const result = runJest('request_animation_frame', ['--verbose']); const stderr = result.stderr.toString(); - console.log(stderr); - expect(stderr).toMatch('requestAnimationFrame test'); expect(result.status).toBe(0); }); From b3f85a2772d2fc1a00a742c3d3721abce4256d9f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 22:20:51 +0200 Subject: [PATCH 8/9] Remove extra -- from appveyor to avoid warning on newer yarn --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 937f0bab5e78..7e1d847a2f78 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 From 49680286f5d2ca022cf0113998abdcd3202ff826 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 30 Sep 2017 22:25:02 +0200 Subject: [PATCH 9/9] Include time since window initialised in rAF implementation --- .../__tests__/request_animation_frame.test.js | 3 ++- packages/jest-environment-jsdom/src/index.js | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js index 4c16ba8756dc..8f1ff5cbfabf 100644 --- a/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js +++ b/integration_tests/request_animation_frame/__tests__/request_animation_frame.test.js @@ -12,8 +12,9 @@ test('requestAnimationFrame test', done => { expect.hasAssertions(); - requestAnimationFrame(() => { + requestAnimationFrame(timestamp => { expect(true).toBe(true); + expect(timestamp).toBeGreaterThan(0); done(); }); diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 9427d1313c77..f03b6543c92b 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -22,6 +22,7 @@ class JSDOMEnvironment { moduleMocker: ?ModuleMocker; constructor(config: ProjectConfig): void { + const jsdomInitialized = process.hrtime(); // lazy require this.document = JSDom.jsdom('', { url: config.testURL, @@ -34,7 +35,11 @@ class JSDOMEnvironment { if (!global.requestAnimationFrame) { global.requestAnimationFrame = callback => { - global.setTimeout(callback, 0); + const hr = process.hrtime(jsdomInitialized); + const hrInNano = hr[0] * 1e9 + hr[1]; + const hrInMicro = hrInNano / 1e6; + + return global.setTimeout(callback, 0, hrInMicro); }; }