Skip to content

Commit ba5b2b1

Browse files
committed
fix: prevent double teardown on test environment error
1 parent fd3d6cf commit ba5b2b1

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import runJest from '../runJest';
2+
3+
// This test ensures that if a custom environment's teardown throws, Jest reports the real error, not an internal error.
4+
describe('Custom Environment Teardown Error', () => {
5+
it('reports the error thrown from teardown()', () => {
6+
const {stderr, exitCode} = runJest('custom-environment-teardown-error');
7+
expect(exitCode).toBe(1);
8+
expect(stderr).toMatch('teardown error from custom environment');
9+
// Should NOT contain the internal error that was seen in the regression
10+
expect(stderr).not.toMatch(
11+
'The "object" argument must be of type object. Received null',
12+
);
13+
});
14+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const NodeEnvironment = require('jest-environment-node').default;
2+
3+
class CustomEnvironment extends NodeEnvironment {
4+
async teardown() {
5+
await super.teardown();
6+
throw new Error('teardown error from custom environment');
7+
}
8+
}
9+
10+
module.exports = CustomEnvironment;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function hello() {
2+
return 'Hello, world!';
3+
}
4+
5+
test('hello returns Hello, world!', () => {
6+
expect(hello()).toBe('Hello, world!');
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "<rootDir>/CustomEnvironment.js"
4+
}
5+
}

packages/jest-runner/src/runTest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,11 @@ async function runTestInternal(
217217
);
218218
sourcemapSupport.resetRetrieveHandlers();
219219

220-
await environment.teardown();
221-
isTornDown = true;
220+
try {
221+
await environment.teardown();
222+
} finally {
223+
isTornDown = true;
224+
}
222225
}
223226
};
224227

0 commit comments

Comments
 (0)