Skip to content

Commit 3bec765

Browse files
authored
Clear timers when disposing an environment. (#1242)
1 parent d546238 commit 3bec765

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @emails oncall+jsinfra
9+
*/
10+
'use strict';
11+
12+
const runJest = require('../runJest');
13+
14+
test('setImmediate', () => {
15+
const result = runJest('set_immediate', ['--verbose']);
16+
const stdout = result.stdout.toString();
17+
const stderr = result.stderr.toString();
18+
19+
expect(stdout).toMatch('setImmediate test');
20+
expect(stderr).toBe('');
21+
22+
expect(result.status).toBe(0);
23+
});
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 BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
test('setImmediate test', () => {
12+
expect(true).toBe(true);
13+
14+
setImmediate(() => {
15+
throw new Error('Scheduled Error');
16+
});
17+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class JSDOMEnvironment {
3434
}
3535

3636
dispose(): void {
37+
if (this.fakeTimers) {
38+
this.fakeTimers.clearAllTimers();
39+
}
3740
if (this.global) {
3841
this.global.close();
3942
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class NodeEnvironment {
3939
}
4040

4141
dispose() {
42+
if (this.fakeTimers) {
43+
this.fakeTimers.clearAllTimers();
44+
}
4245
this.global = null;
4346
this.fakeTimers = null;
4447
}

0 commit comments

Comments
 (0)