Skip to content

Commit

Permalink
Fix asynchronous test will fail due to timeout issue.
Browse files Browse the repository at this point in the history
* Send error event to Node.js process.
  • Loading branch information
H1Gdev committed Oct 13, 2017
1 parent afb4bf5 commit 057a611
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 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,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));
});
});
15 changes: 15 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,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);
});
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 @@ -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;
Expand Down

0 comments on commit 057a611

Please sign in to comment.