Skip to content

Commit

Permalink
Fix reporting of requires or Jest env method access after "teardown"
Browse files Browse the repository at this point in the history
  • Loading branch information
stekycz committed May 26, 2023
1 parent 5fd3805 commit 0681959
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 63 deletions.
20 changes: 17 additions & 3 deletions e2e/__tests__/__snapshots__/environmentAfterTeardown.test.ts.snap
@@ -1,13 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for environment methods after test is done 1`] = `
"ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js.
"FAIL __tests__/afterTeardown.test.js
✕ access environment methods after done (3 ms)
● access environment methods after done
ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code.
9 | test('access environment methods after done', () => {
10 | setTimeout(() => {
> 11 | jest.clearAllTimers();
| ^
12 | }, 100);
12 | }, 0);
13 | });
14 |"
14 |
at _getFakeTimers (../../packages/jest-runtime/build/index.js:1977:15)
at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.365 s, estimated 1 s
Ran all test suites."
`;
18 changes: 16 additions & 2 deletions e2e/__tests__/__snapshots__/requireAfterTeardown.test.ts.snap
@@ -1,13 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for requires after test is done 1`] = `
"ReferenceError: You are trying to \`import\` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js.
"FAIL __tests__/lateRequire.test.js
✕ require after done (5 ms)
● require after done
ReferenceError: You are trying to \`import\` a file outside of the scope of the test code.
9 | test('require after done', () => {
10 | setTimeout(() => {
> 11 | const double = require('../');
| ^
12 |
13 | expect(double(5)).toBe(10);
14 | }, 100);"
14 | }, 0);
at Runtime._execModule (../../packages/jest-runtime/build/index.js:1380:13)
at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.332 s, estimated 1 s
Ran all test suites."
`;
6 changes: 1 addition & 5 deletions e2e/__tests__/environmentAfterTeardown.test.ts
Expand Up @@ -9,10 +9,6 @@ import runJest from '../runJest';

test('prints useful error for environment methods after test is done', () => {
const {stderr} = runJest('environment-after-teardown');
const interestingLines = stderr.split('\n').slice(9, 18).join('\n');

expect(interestingLines).toMatchSnapshot();
expect(stderr.split('\n')[9]).toBe(
'ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js.',
);
expect(stderr).toMatchSnapshot();

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v16.x on ubuntu-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 1 + Received + 1 @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.652 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v18.x on ubuntu-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (15 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.687 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v14.x on ubuntu-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (4 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.86 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / Node LTS on Ubuntu with coverage (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (15 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.803 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node LTS on ubuntu-latest using jest-jasmine2 (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 13 + Received + 10 - FAIL __tests__/afterTeardown.test.js + PASS __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✓ access environment methods after done (1 ms) - ● access environment methods after done - - ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 0.901 s + Ran all test suites. + + ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js. 9 | test('access environment methods after done', () => { 10 | setTimeout(() => { > 11 | jest.clearAllTimers(); | ^ 12 | }, 0); 13 | }); 14 | - at _getFakeTimers (../../packages/jest-runtime/build/index.js:1977:15) - at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.365 s, estimated 1 s - Ran all test suites. + at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v19.x on ubuntu-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (23 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.966 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node LTS on windows-latest using jest-jasmine2 (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 13 + Received + 10 - FAIL __tests__/afterTeardown.test.js + PASS __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✓ access environment methods after done (1 ms) - ● access environment methods after done - - ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 0.825 s + Ran all test suites. + + ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js. 9 | test('access environment methods after done', () => { 10 | setTimeout(() => { > 11 | jest.clearAllTimers(); | ^ 12 | }, 0); 13 | }); 14 | - at _getFakeTimers (../../packages/jest-runtime/build/index.js:1977:15) - at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.365 s, estimated 1 s - Ran all test suites. + at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v14.x on windows-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (32 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.985 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node LTS on macos-latest using jest-jasmine2 (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 12 + Received + 9 - FAIL __tests__/afterTeardown.test.js + PASS __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✓ access environment methods after done (18 ms) - ● access environment methods after done - - ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 2.915 s + Ran all test suites. + + ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js. 9 | test('access environment methods after done', () => { 10 | setTimeout(() => { > 11 | jest.clearAllTimers(); | ^ 12 | }, 0); 13 | }); 14 | - at _getFakeTimers (../../packages/jest-runtime/build/index.js:1977:15) at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.365 s, estimated 1 s - Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v16.x on windows-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (30 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 1.037 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v18.x on windows-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (15 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 1.042 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v19.x on windows-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (12 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 0.921 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node v16.x on macos-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (28 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 2.091 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/environmentAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node v14.x on macos-latest (2/4)

prints useful error for environment methods after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for environment methods after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/afterTeardown.test.js - ✕ access environment methods after done (3 ms) + ✕ access environment methods after done (9 ms) ● access environment methods after done ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.clearAllTimers [as _onTimeout] (__tests__/afterTeardown.test.js:11:10) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.365 s, estimated 1 s + Time: 2.009 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/environmentAfterTeardown.test.ts:13:18)
});
7 changes: 1 addition & 6 deletions e2e/__tests__/requireAfterTeardown.test.ts
Expand Up @@ -10,10 +10,5 @@ import runJest from '../runJest';
test('prints useful error for requires after test is done', () => {
const {stderr} = runJest('require-after-teardown');

const interestingLines = stderr.split('\n').slice(9, 18).join('\n');

expect(interestingLines).toMatchSnapshot();
expect(stderr.split('\n')[19]).toMatch(
new RegExp('(__tests__/lateRequire.test.js:11:20)'),
);
expect(stderr).toMatchSnapshot();

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v16.x on ubuntu-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (4 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 0.647 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v18.x on ubuntu-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (4 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 0.667 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v14.x on ubuntu-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (18 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 0.896 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / Node LTS on Ubuntu with coverage (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (17 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 0.773 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node LTS on ubuntu-latest using jest-jasmine2 (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 14 + Received + 21 - FAIL __tests__/lateRequire.test.js + PASS __tests__/lateRequire.test.js - ✕ require after done (5 ms) - - ● require after done - - ReferenceError: You are trying to `import` a file outside of the scope of the test code. + ✓ require after done + + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 0.918 s + Ran all test suites. + + ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js. 9 | test('require after done', () => { 10 | setTimeout(() => { > 11 | const double = require('../'); | ^ 12 | 13 | expect(double(5)).toBe(10); 14 | }, 0); - at Runtime._execModule (../../packages/jest-runtime/build/index.js:1380:13) - at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.332 s, estimated 1 s - Ran all test suites. + at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) + /home/runner/work/jest/jest/e2e/require-after-teardown/__tests__/lateRequire.test.js:12 + expect(double(5)).toBe(10); + ^ + + TypeError: double is not a function + at Timeout.double [as _onTimeout] (/home/runner/work/jest/jest/e2e/require-after-teardown/__tests__/lateRequire.test.js:13:12) + at listOnTimeout (node:internal/timers:569:17) + at processTimers (node:internal/timers:512:7) + + Node.js v18.16.0 at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu / Node v19.x on ubuntu-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (18 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 0.965 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node LTS on windows-latest using jest-jasmine2 (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 14 + Received + 21 - "FAIL __tests__/lateRequire.test.js + "PASS __tests__/lateRequire.test.js - ✕ require after done (5 ms) - - ● require after done - - ReferenceError: You are trying to `import` a file outside of the scope of the test code. + ✓ require after done + + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 0.914 s + Ran all test suites. + + ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js. 9 | test('require after done', () => { 10 | setTimeout(() => { > 11 | const double = require('../'); | ^ 12 | 13 | expect(double(5)).toBe(10); 14 | }, 0); - at Runtime._execModule (../../packages/jest-runtime/build/index.js:1380:13) - at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.332 s, estimated 1 s - Ran all test suites." + at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) + D:\a\jest\jest\e2e\require-after-teardown\__tests__\lateRequire.test.js:12 + expect(double(5)).toBe(10); + ^ + + TypeError: double is not a function + at Timeout.double [as _onTimeout] (D:\a\jest\jest\e2e\require-after-teardown\__tests__\lateRequire.test.js:13:12) + at listOnTimeout (node:internal/timers:569:17) + at processTimers (node:internal/timers:512:7) + + Node.js v18.16.0" at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v14.x on windows-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (15 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 1 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node LTS on macos-latest using jest-jasmine2 (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 13 + Received + 20 - FAIL __tests__/lateRequire.test.js + PASS __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✓ require after done (1 ms) - ● require after done - - ReferenceError: You are trying to `import` a file outside of the scope of the test code. + Test Suites: 1 passed, 1 total + Tests: 1 passed, 1 total + Snapshots: 0 total + Time: 2.703 s + Ran all test suites. + + ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js. 9 | test('require after done', () => { 10 | setTimeout(() => { > 11 | const double = require('../'); | ^ 12 | 13 | expect(double(5)).toBe(10); 14 | }, 0); - at Runtime._execModule (../../packages/jest-runtime/build/index.js:1380:13) - at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) - - Test Suites: 1 failed, 1 total - Tests: 1 failed, 1 total - Snapshots: 0 total - Time: 0.332 s, estimated 1 s - Ran all test suites. + at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) + /Users/runner/work/jest/jest/e2e/require-after-teardown/__tests__/lateRequire.test.js:12 + expect(double(5)).toBe(10); + ^ + + TypeError: double is not a function + at Timeout.double [as _onTimeout] (/Users/runner/work/jest/jest/e2e/require-after-teardown/__tests__/lateRequire.test.js:13:12) + at listOnTimeout (node:internal/timers:569:17) + at processTimers (node:internal/timers:512:7) + + Node.js v18.16.0 at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v16.x on windows-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (16 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 1.014 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v18.x on windows-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (8 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 1.015 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-windows / Node v19.x on windows-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (41 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 1.179 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node v16.x on macos-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (23 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 2.321 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)

Check failure on line 13 in e2e/__tests__/requireAfterTeardown.test.ts

View workflow job for this annotation

GitHub Actions / test-macos / Node v14.x on macos-latest (2/4)

prints useful error for requires after test is done

expect(received).toMatchSnapshot() Snapshot name: `prints useful error for requires after test is done 1` - Snapshot - 2 + Received + 2 @@ -1,7 +1,7 @@ FAIL __tests__/lateRequire.test.js - ✕ require after done (5 ms) + ✕ require after done (15 ms) ● require after done ReferenceError: You are trying to `import` a file outside of the scope of the test code. @@ -17,7 +17,7 @@ at Timeout.require [as _onTimeout] (__tests__/lateRequire.test.js:11:20) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total - Time: 0.332 s, estimated 1 s + Time: 2.246 s Ran all test suites. at Object.toMatchSnapshot (e2e/__tests__/requireAfterTeardown.test.ts:13:18)
});
Expand Up @@ -9,5 +9,5 @@
test('access environment methods after done', () => {
setTimeout(() => {
jest.clearAllTimers();
}, 100);
}, 0);
});
2 changes: 1 addition & 1 deletion e2e/require-after-teardown/__tests__/lateRequire.test.js
Expand Up @@ -11,5 +11,5 @@ test('require after done', () => {
const double = require('../');

expect(double(5)).toBe(10);
}, 100);
}, 0);
});
Expand Up @@ -33,6 +33,7 @@ const jestAdapter = async (
globalConfig,
localRequire: runtime.requireModule.bind(runtime),
parentProcess: process,
runtime,
sendMessageToJest,
setGlobalsForRuntime: runtime.setGlobalsForRuntime.bind(runtime),
testPath,
Expand Down
Expand Up @@ -16,6 +16,7 @@ import {
} from '@jest/test-result';
import type {Circus, Config, Global} from '@jest/types';
import {formatExecError, formatResultsErrors} from 'jest-message-util';
import type Runtime from 'jest-runtime';
import {
SnapshotState,
addSerializer,
Expand All @@ -30,6 +31,7 @@ import {
getState as getRunnerState,
} from '../state';
import testCaseReportHandler from '../testCaseReportHandler';
import {unhandledRejectionHandler} from '../unhandledRejectionHandler';
import {getTestID} from '../utils';

interface RuntimeGlobals extends Global.TestFrameworkGlobals {
Expand All @@ -39,6 +41,7 @@ interface RuntimeGlobals extends Global.TestFrameworkGlobals {
export const initialize = async ({
config,
environment,
runtime,
globalConfig,
localRequire,
parentProcess,
Expand All @@ -48,6 +51,7 @@ export const initialize = async ({
}: {
config: Config.ProjectConfig;
environment: JestEnvironment;
runtime: Runtime;
globalConfig: Config.GlobalConfig;
localRequire: <T = unknown>(path: string) => T;
testPath: string;
Expand Down Expand Up @@ -129,6 +133,8 @@ export const initialize = async ({
addEventHandler(testCaseReportHandler(testPath, sendMessageToJest));
}

addEventHandler(unhandledRejectionHandler(runtime));

// Return it back to the outer scope (test runner outside the VM).
return {globals: globalsObject, snapshotState};
};
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-circus/src/state.ts
Expand Up @@ -9,12 +9,10 @@ import type {Circus} from '@jest/types';
import eventHandler from './eventHandler';
import formatNodeAssertErrors from './formatNodeAssertErrors';
import {STATE_SYM} from './types';
import unhandledRejectionHandler from './unhandledRejectionHandler';
import {makeDescribe} from './utils';

const eventHandlers: Array<Circus.EventHandler> = [
eventHandler,
unhandledRejectionHandler,
formatNodeAssertErrors,
];

Expand Down
94 changes: 51 additions & 43 deletions packages/jest-circus/src/unhandledRejectionHandler.ts
Expand Up @@ -6,6 +6,7 @@
*/

import type {Circus} from '@jest/types';
import type Runtime from 'jest-runtime';
import {addErrorToEachTestUnderDescribe, invariant} from './utils';

// Global values can be overwritten by mocks or tests. We'll capture
Expand All @@ -18,55 +19,62 @@ const untilNextEventLoopTurn = async () => {
});
};

const unhandledRejectionHandler: Circus.EventHandler = async (
event,
state,
): Promise<void> => {
if (event.name === 'hook_success' || event.name === 'hook_failure') {
// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();
export const unhandledRejectionHandler = (
runtime: Runtime,
): Circus.EventHandler => {
return async (event, state) => {
if (event.name === 'hook_start') {
runtime.enterTestCode();
} else if (event.name === 'hook_success' || event.name === 'hook_failure') {
runtime.leaveTestCode();

const {test, describeBlock, hook} = event;
const {asyncError, type} = hook;
// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();

if (type === 'beforeAll') {
invariant(describeBlock, 'always present for `*All` hooks');
for (const error of state.unhandledRejectionErrorByPromise.values()) {
addErrorToEachTestUnderDescribe(describeBlock, error, asyncError);
}
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
for (const error of state.unhandledRejectionErrorByPromise.values()) {
state.unhandledErrors.push([error, asyncError]);
const {test, describeBlock, hook} = event;
const {asyncError, type} = hook;

if (type === 'beforeAll') {
invariant(describeBlock, 'always present for `*All` hooks');
for (const error of state.unhandledRejectionErrorByPromise.values()) {
addErrorToEachTestUnderDescribe(describeBlock, error, asyncError);
}
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
for (const error of state.unhandledRejectionErrorByPromise.values()) {
state.unhandledErrors.push([error, asyncError]);
}
} else {
invariant(test, 'always present for `*Each` hooks');
for (const error of test.unhandledRejectionErrorByPromise.values()) {
test.errors.push([error, asyncError]);
}
}
} else {
} else if (event.name === 'test_fn_start') {
runtime.enterTestCode();
} else if (
event.name === 'test_fn_success' ||
event.name === 'test_fn_failure'
) {
runtime.leaveTestCode();

// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();

const {test} = event;
invariant(test, 'always present for `*Each` hooks');

for (const error of test.unhandledRejectionErrorByPromise.values()) {
test.errors.push([error, asyncError]);
test.errors.push([error, event.test.asyncError]);
}
}
} else if (
event.name === 'test_fn_success' ||
event.name === 'test_fn_failure'
) {
// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();

const {test} = event;
invariant(test, 'always present for `*Each` hooks');
} else if (event.name === 'teardown') {
// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();

for (const error of test.unhandledRejectionErrorByPromise.values()) {
test.errors.push([error, event.test.asyncError]);
state.unhandledErrors.push(
...state.unhandledRejectionErrorByPromise.values(),
);
}
} else if (event.name === 'teardown') {
// We need to give event loop the time to actually execute `rejectionHandled`, `uncaughtException` or `unhandledRejection` events
await untilNextEventLoopTurn();

state.unhandledErrors.push(
...state.unhandledRejectionErrorByPromise.values(),
);
}
};
};

export default unhandledRejectionHandler;
29 changes: 29 additions & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -208,6 +208,7 @@ export default class Runtime {
private readonly esmConditions: Array<string>;
private readonly cjsConditions: Array<string>;
private isTornDown = false;
private isInsideTestCode: boolean | undefined;

constructor(
config: Config.ProjectConfig,
Expand Down Expand Up @@ -562,6 +563,11 @@ export default class Runtime {
// @ts-expect-error - exiting
return;
}
if (this.isInsideTestCode === false) {
throw new ReferenceError(
'You are trying to `import` a file outside of the scope of the test code.',
);
}

if (specifier === '@jest/globals') {
const fromCache = this._esmoduleRegistry.get('@jest/globals');
Expand Down Expand Up @@ -706,6 +712,11 @@ export default class Runtime {
process.exitCode = 1;
return;
}
if (this.isInsideTestCode === false) {
throw new ReferenceError(
'You are trying to `import` a file outside of the scope of the test code.',
);
}

if (module.status === 'unlinked') {
// since we might attempt to link the same module in parallel, stick the promise in a weak map so every call to
Expand Down Expand Up @@ -1342,6 +1353,14 @@ export default class Runtime {
this._moduleMocker.clearAllMocks();
}

enterTestCode(): void {
this.isInsideTestCode = true;
}

leaveTestCode(): void {
this.isInsideTestCode = false;
}

teardown(): void {
this.restoreAllMocks();
this.resetModules();
Expand Down Expand Up @@ -1485,6 +1504,11 @@ export default class Runtime {
process.exitCode = 1;
return;
}
if (this.isInsideTestCode === false) {
throw new ReferenceError(
'You are trying to `import` a file outside of the scope of the test code.',
);
}

// If the environment was disposed, prevent this module from being executed.
if (!this._environment.global) {
Expand Down Expand Up @@ -2169,6 +2193,11 @@ export default class Runtime {
);
process.exitCode = 1;
}
if (this.isInsideTestCode === false) {
throw new ReferenceError(
'You are trying to access a property or method of the Jest environment outside of the scope of the test code.',
);
}

return this._fakeTimersImplementation!;
};
Expand Down

0 comments on commit 0681959

Please sign in to comment.