From 291e83656646ac3fa19013df225288f2b9725f84 Mon Sep 17 00:00:00 2001 From: H1Gdev Date: Mon, 16 Oct 2017 05:14:11 +0900 Subject: [PATCH] Fix asynchronous test will fail due to timeout issue. (#4669) * Fix asynchronous test will fail due to timeout issue. * Send error event to Node.js process. * Run test in jsdom --- .../__tests__/jasmine_async.test.js | 9 +++++++++ .../__tests__/async_test_fails.test.js | 17 +++++++++++++++++ packages/jest-environment-jsdom/src/index.js | 12 ++++++++++++ 3 files changed, 38 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..809f1105049c 100644 --- a/integration_tests/__tests__/jasmine_async.test.js +++ b/integration_tests/__tests__/jasmine_async.test.js @@ -132,4 +132,13 @@ 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']); + + 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 new file mode 100644 index 000000000000..eca6497bf59c --- /dev/null +++ b/integration_tests/jasmine_async/__tests__/async_test_fails.test.js @@ -0,0 +1,17 @@ +/** + * 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. + * + * @jest-environment jsdom + */ + +'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 23a2a832d406..4557efa97237 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 = { @@ -67,8 +75,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;