Skip to content

Commit 291e836

Browse files
H1Gdevcpojer
authored andcommitted
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
1 parent f5d7993 commit 291e836

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

integration_tests/__tests__/jasmine_async.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,13 @@ describe('async jasmine', () => {
132132
expect(json.numPendingTests).toBe(1);
133133
expect(json.testResults[0].message).toMatch(/concurrent test fails/);
134134
});
135+
136+
it('async test fails', () => {
137+
const result = runJest.json('jasmine_async', ['async_test_fails.test.js']);
138+
139+
expect(result.status).toBe(1);
140+
expect(result.json.testResults[0].message).toEqual(
141+
expect.stringContaining('Expected value to be truthy, instead received'),
142+
);
143+
});
135144
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment jsdom
8+
*/
9+
10+
'use strict';
11+
12+
it('async test fails', done => {
13+
setTimeout(() => {
14+
expect(false).toBeTruthy();
15+
done();
16+
}, 1 * 1000);
17+
});

packages/jest-environment-jsdom/src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class JSDOMEnvironment {
1919
document: ?Object;
2020
fakeTimers: ?FakeTimers<number>;
2121
global: ?Global;
22+
errorEventListener: ?Function;
2223
moduleMocker: ?ModuleMocker;
2324

2425
constructor(config: ProjectConfig): void {
@@ -43,6 +44,13 @@ class JSDOMEnvironment {
4344
};
4445
}
4546

47+
this.errorEventListener = event => {
48+
if (event.error) {
49+
process.emit('uncaughtException', event.error);
50+
}
51+
};
52+
global.addEventListener('error', this.errorEventListener);
53+
4654
this.moduleMocker = new mock.ModuleMocker(global);
4755

4856
const timerConfig = {
@@ -67,8 +75,12 @@ class JSDOMEnvironment {
6775
this.fakeTimers.dispose();
6876
}
6977
if (this.global) {
78+
if (this.errorEventListener) {
79+
this.global.removeEventListener('error', this.errorEventListener);
80+
}
7081
this.global.close();
7182
}
83+
this.errorEventListener = null;
7284
this.global = null;
7385
this.document = null;
7486
this.fakeTimers = null;

0 commit comments

Comments
 (0)