Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 833098c

Browse files
committed
changelog and tweak tests
1 parent a5365db commit 833098c

5 files changed

Lines changed: 14 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `[jest-runtime]` Improve CJS-from-ESM interop: `__esModule`/Babel default unwrap, broader named-export coverage, and shared CJS singleton across importers ([#16050](https://github.com/jestjs/jest/pull/16050))
2020
- `[jest-runtime]` Load `.js` files with ESM syntax but no `"type":"module"` marker as native ESM ([#16050](https://github.com/jestjs/jest/pull/16050))
2121
- `[jest-runtime]` Fix deadlocks and double-evaluation in concurrent ESM and wasm imports ([#16050](https://github.com/jestjs/jest/pull/16050))
22+
- `[jest-runtime]` Fix error when `require()` is called after the Jest environment has been torn down ([#15951](https://github.com/jestjs/jest/pull/15951))
2223

2324
### Chore & Maintenance
2425

e2e/__tests__/requireAfterTeardown.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ test('prints useful error for requires after test is done w/o `waitForUnhandledR
1616
const interestingLines = stderr.split('\n').slice(9, 18).join('\n');
1717

1818
expect(interestingLines).toMatchSnapshot();
19-
expect(stderr.split('\n')[19]).toMatch(
20-
'(__tests__/lateRequire.test.js:11:20)',
21-
);
19+
expect(stderr).toContain('(__tests__/lateRequire.test.js:11:20)');
2220
});
2321

2422
test('prints useful error for requires after test is done w/ `waitForUnhandledRejections`', () => {
@@ -29,7 +27,5 @@ test('prints useful error for requires after test is done w/ `waitForUnhandledRe
2927
const interestingLines = stderr.split('\n').slice(5, 14).join('\n');
3028

3129
expect(interestingLines).toMatchSnapshot();
32-
expect(stderr.split('\n')[16]).toMatch(
33-
'(__tests__/lateRequire.test.js:11:20)',
34-
);
30+
expect(stderr).toContain('(__tests__/lateRequire.test.js:11:20)');
3531
});

e2e/__tests__/requireAfterTeardownJasmine.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,5 @@ test.each`
2020
const interestingLines = stderr.split('\n').slice(9, 18).join('\n');
2121

2222
expect(interestingLines).toMatchSnapshot();
23-
expect(stderr.split('\n')[19]).toMatch(
24-
'(__tests__/lateRequire.test.js:11:20)',
25-
);
23+
expect(stderr).toContain('(__tests__/lateRequire.test.js:11:20)');
2624
});

packages/jest-runtime/src/__tests__/runtime_jest_fn.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,26 @@ describe('Runtime', () => {
7878
});
7979

8080
describe('require after environment torn down', () => {
81+
const originalExitCode = process.exitCode;
82+
83+
afterEach(() => {
84+
jest.restoreAllMocks();
85+
process.exitCode = originalExitCode;
86+
});
87+
8188
it('should log error and set exit code when require is called after teardown', async () => {
8289
const runtime = await createRuntime(__filename);
83-
const consoleErrorSpy = jest
84-
.spyOn(console, 'error')
85-
.mockImplementation(() => {});
86-
const originalExitCode = process.exitCode;
90+
jest.spyOn(console, 'error').mockImplementation(() => {});
8791

8892
runtime.teardown();
8993
runtime.requireModuleOrMock(runtime.__mockRootPath, 'RegularModule');
9094

9195
expect(process.exitCode).toBe(1);
92-
expect(consoleErrorSpy).toHaveBeenCalledWith(
96+
expect(console.error).toHaveBeenCalledWith(
9397
expect.stringContaining(
9498
'You are trying to `require` a file after the Jest environment has been torn down.',
9599
),
96100
);
97-
98-
consoleErrorSpy.mockRestore();
99-
process.exitCode = originalExitCode;
100101
});
101102
});
102103

packages/jest-runtime/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ export default class Runtime {
20562056
'You are trying to `require` a file after the Jest environment has been torn down.',
20572057
);
20582058
process.exitCode = 1;
2059-
// @ts-expect-error - exiting early, the process will be terminated
2059+
// @ts-expect-error: exiting early
20602060
return;
20612061
}
20622062

0 commit comments

Comments
 (0)