Skip to content

Commit

Permalink
Fix asynchronous test will fail due to timeout issue. (#4669)
Browse files Browse the repository at this point in the history
* Fix asynchronous test will fail due to timeout issue.

* Send error event to Node.js process.

* Run test in jsdom
  • Loading branch information
H1Gdev authored and cpojer committed Oct 15, 2017
1 parent f5d7993 commit 291e836
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions integration_tests/__tests__/jasmine_async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
});
});
17 changes: 17 additions & 0 deletions integration_tests/jasmine_async/__tests__/async_test_fails.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
12 changes: 12 additions & 0 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class JSDOMEnvironment {
document: ?Object;
fakeTimers: ?FakeTimers<number>;
global: ?Global;
errorEventListener: ?Function;
moduleMocker: ?ModuleMocker;

constructor(config: ProjectConfig): void {
Expand All @@ -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 = {
Expand All @@ -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;
Expand Down

0 comments on commit 291e836

Please sign in to comment.