Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions e2e/__tests__/environmentTeardownError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import runJest from '../runJest';

// This test ensures that if a custom environment's teardown throws, Jest reports the real error, not an internal error.
describe('Environment Teardown Error', () => {
it('reports the error thrown from teardown() in a custom environment', () => {
const {stderr, exitCode} = runJest('environment-teardown-error');
expect(exitCode).toBe(1);
expect(stderr).toMatch('teardown error from custom environment');
// Should NOT contain the internal error that was seen in the regression
expect(stderr).not.toMatch(
'The "object" argument must be of type object. Received null',
);
});
});
17 changes: 17 additions & 0 deletions e2e/environment-teardown-error/EnvironmentWithTeardownError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const NodeEnvironment = require('jest-environment-node').default;

class EnvironmentWithTeardownError extends NodeEnvironment {
async teardown() {
await super.teardown();
throw new Error('teardown error from custom environment');
}
}

module.exports = EnvironmentWithTeardownError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('Environment must tear down with a correct error stack trace', () => {});
5 changes: 5 additions & 0 deletions e2e/environment-teardown-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "<rootDir>/EnvironmentWithTeardownError.js"
}
}
7 changes: 5 additions & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@
);
sourcemapSupport.resetRetrieveHandlers();

await environment.teardown();
isTornDown = true;
try {
await environment.teardown();

Check warning on line 221 in packages/jest-runner/src/runTest.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-runner/src/runTest.ts#L220-L221

Added lines #L220 - L221 were not covered by tests
} finally {
isTornDown = true;

Check warning on line 223 in packages/jest-runner/src/runTest.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-runner/src/runTest.ts#L223

Added line #L223 was not covered by tests
}
}
};

Expand Down
Loading