From 057a611aba056e0e651ff022be6ead6bede4b4b3 Mon Sep 17 00:00:00 2001 From: H1Gdev Date: Wed, 11 Oct 2017 18:16:23 +0900 Subject: [PATCH 1/2] Fix asynchronous test will fail due to timeout issue. * Send error event to Node.js process. --- integration_tests/__tests__/jasmine_async.test.js | 8 ++++++++ .../__tests__/async_test_fails.test.js | 15 +++++++++++++++ packages/jest-environment-jsdom/src/index.js | 12 ++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 integration_tests/jasmine_async/__tests__/async_test_fails.test.js diff --git a/integration_tests/__tests__/jasmine_async.test.js b/integration_tests/__tests__/jasmine_async.test.js index e0ff9120f1fe..ef12d6aee346 100644 --- a/integration_tests/__tests__/jasmine_async.test.js +++ b/integration_tests/__tests__/jasmine_async.test.js @@ -132,4 +132,12 @@ describe('async jasmine', () => { expect(json.numPendingTests).toBe(1); expect(json.testResults[0].message).toMatch(/concurrent test fails/); }); + + it('async test fails', () => { + const result = runJest.json('jasmine_async', ['async_test_fails.test.js']); + const stdout = result.stdout; + + const expected = 'Expected value to be truthy, instead received'; + expect(stdout).toEqual(expect.stringContaining(expected)); + }); }); diff --git a/integration_tests/jasmine_async/__tests__/async_test_fails.test.js b/integration_tests/jasmine_async/__tests__/async_test_fails.test.js new file mode 100644 index 000000000000..cddb894fa874 --- /dev/null +++ b/integration_tests/jasmine_async/__tests__/async_test_fails.test.js @@ -0,0 +1,15 @@ +/** + * 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'; + +it('async test fails', done => { + setTimeout(() => { + expect(false).toBeTruthy(); + done(); + }, 1 * 1000); +}); diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 05ae31ec7a2b..e76dfed0cd07 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -19,6 +19,7 @@ class JSDOMEnvironment { document: ?Object; fakeTimers: ?FakeTimers; global: ?Global; + errorEventListener: ?Function; moduleMocker: ?ModuleMocker; constructor(config: ProjectConfig): void { @@ -43,6 +44,13 @@ class JSDOMEnvironment { }; } + this.errorEventListener = event => { + if (event.error) { + process.emit('uncaughtException', event.error); + } + }; + global.addEventListener('error', this.errorEventListener); + this.moduleMocker = new mock.ModuleMocker(global); const timerConfig = { @@ -63,8 +71,12 @@ class JSDOMEnvironment { this.fakeTimers.dispose(); } if (this.global) { + if (this.errorEventListener) { + this.global.removeEventListener('error', this.errorEventListener); + } this.global.close(); } + this.errorEventListener = null; this.global = null; this.document = null; this.fakeTimers = null; From 3309ee1b3b18434d2542123cb1ad6ab9d3a983a9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 14 Oct 2017 09:10:42 +0200 Subject: [PATCH 2/2] Run test in jsdom --- integration_tests/__tests__/jasmine_async.test.js | 7 ++++--- .../jasmine_async/__tests__/async_test_fails.test.js | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/integration_tests/__tests__/jasmine_async.test.js b/integration_tests/__tests__/jasmine_async.test.js index ef12d6aee346..809f1105049c 100644 --- a/integration_tests/__tests__/jasmine_async.test.js +++ b/integration_tests/__tests__/jasmine_async.test.js @@ -135,9 +135,10 @@ describe('async jasmine', () => { it('async test fails', () => { const result = runJest.json('jasmine_async', ['async_test_fails.test.js']); - const stdout = result.stdout; - const expected = 'Expected value to be truthy, instead received'; - expect(stdout).toEqual(expect.stringContaining(expected)); + expect(result.status).toBe(1); + expect(result.json.testResults[0].message).toEqual( + expect.stringContaining('Expected value to be truthy, instead received'), + ); }); }); diff --git a/integration_tests/jasmine_async/__tests__/async_test_fails.test.js b/integration_tests/jasmine_async/__tests__/async_test_fails.test.js index cddb894fa874..eca6497bf59c 100644 --- a/integration_tests/jasmine_async/__tests__/async_test_fails.test.js +++ b/integration_tests/jasmine_async/__tests__/async_test_fails.test.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @jest-environment jsdom */ 'use strict';