Skip to content

Commit

Permalink
Put back useful tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stekycz committed Nov 6, 2023
1 parent d5ade2d commit 2cfa23f
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for environment methods after test is done 1`] = `
exports[`prints useful error for environment methods after test is done w/ \`waitNextEventLoopTurnForUnhandledRejectionEvents\` 1`] = `
" 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 | }, 0);
13 | });
14 |"
`;

exports[`prints useful error for environment methods after test is done w/o \`waitNextEventLoopTurnForUnhandledRejectionEvents\` 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.
9 | test('access environment methods after done', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.
9 | test('access environment methods after done', () => {
10 | setTimeout(() => {
> 11 | jest.clearAllTimers();
| ^
12 | }, 0);
13 | });
14 |"
`;

exports[`prints useful error for environment methods after test is done 2`] = `
"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 |"
`;
14 changes: 13 additions & 1 deletion e2e/__tests__/__snapshots__/requireAfterTeardown.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for requires after test is done 1`] = `
exports[`prints useful error for requires after test is done w/ \`waitNextEventLoopTurnForUnhandledRejectionEvents\` 1`] = `
" 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 | }, 0);"
`;
exports[`prints useful error for requires after test is done w/o \`waitNextEventLoopTurnForUnhandledRejectionEvents\` 1`] = `
"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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.
9 | test('require after done', () => {
10 | setTimeout(() => {
> 11 | const double = require('../');
| ^
12 |
13 | expect(double(5)).toBe(10);
14 | }, 0);"
`;
exports[`prints useful error for requires after test is done 2`] = `
"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);"
`;
17 changes: 16 additions & 1 deletion e2e/__tests__/environmentAfterTeardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {skipSuiteOnJasmine} from '@jest/test-utils';
import runJest from '../runJest';

test('prints useful error for environment methods after test is done', () => {
skipSuiteOnJasmine();

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

Expand All @@ -16,3 +19,15 @@ test('prints useful error for environment methods after test is done', () => {
'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.',
);
});

test('prints useful error for environment methods after test is done w/ `waitNextEventLoopTurnForUnhandledRejectionEvents`', () => {
const {stderr} = runJest('environment-after-teardown', [
'--waitNextEventLoopTurnForUnhandledRejectionEvents',
]);
const interestingLines = stderr.split('\n').slice(5, 14).join('\n');

expect(interestingLines).toMatchSnapshot();
expect(stderr.split('\n')[5]).toMatch(
'ReferenceError: You are trying to access a property or method of the Jest environment outside of the scope of the test code.',
);
});
28 changes: 28 additions & 0 deletions e2e/__tests__/environmentAfterTeardownJasmine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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 {skipSuiteOnJestCircus} from '@jest/test-utils';
import runJest from '../runJest';

skipSuiteOnJestCircus();

test.each`
jestArgs
${[]}
${['--waitNextEventLoopTurnForUnhandledRejectionEvents']}
`(
'prints useful error for environment methods after test is done',
({jestArgs}) => {
const {stderr} = runJest('environment-after-teardown', jestArgs);
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.',
);
},
);
15 changes: 14 additions & 1 deletion e2e/__tests__/fakeTimersLegacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {isJestJasmineRun} from '@jest/test-utils';
import runJest from '../runJest';

describe('enableGlobally', () => {
Expand Down Expand Up @@ -38,12 +39,24 @@ describe('requestAnimationFrame', () => {
});

describe('setImmediate', () => {
test('fakes setImmediate', () => {
test('fakes setImmediate w/o `waitNextEventLoopTurnForUnhandledRejectionEvents`', () => {
const result = runJest('fake-timers-legacy/set-immediate');

expect(result.stderr).toMatch('setImmediate test');
expect(result.exitCode).toBe(0);
});

test('fakes setImmediate w/ `waitNextEventLoopTurnForUnhandledRejectionEvents`', () => {
// Jasmine runner does not handle unhandled promise rejections that are causing the test to fail in Jest circus
const expectedExitCode = isJestJasmineRun() ? 0 : 1;

const result = runJest('fake-timers-legacy/set-immediate', [
'--waitNextEventLoopTurnForUnhandledRejectionEvents',
]);

expect(result.stderr).toMatch('setImmediate test');
expect(result.exitCode).toBe(expectedExitCode);
});
});

describe('useRealTimers', () => {
Expand Down
18 changes: 17 additions & 1 deletion e2e/__tests__/requireAfterTeardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {skipSuiteOnJasmine} from '@jest/test-utils';
import runJest from '../runJest';

test('prints useful error for requires after test is done', () => {
skipSuiteOnJasmine();

test('prints useful error for requires after test is done w/o `waitNextEventLoopTurnForUnhandledRejectionEvents`', () => {
const {stderr} = runJest('require-after-teardown');

const interestingLines = stderr.split('\n').slice(9, 18).join('\n');
Expand All @@ -17,3 +20,16 @@ test('prints useful error for requires after test is done', () => {
'(__tests__/lateRequire.test.js:11:20)',
);
});

test('prints useful error for requires after test is done w/ `waitNextEventLoopTurnForUnhandledRejectionEvents`', () => {
const {stderr} = runJest('require-after-teardown', [
'--waitNextEventLoopTurnForUnhandledRejectionEvents',
]);

const interestingLines = stderr.split('\n').slice(5, 14).join('\n');

expect(interestingLines).toMatchSnapshot();
expect(stderr.split('\n')[16]).toMatch(
'(__tests__/lateRequire.test.js:11:20)',
);
});
26 changes: 26 additions & 0 deletions e2e/__tests__/requireAfterTeardownJasmine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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 {skipSuiteOnJestCircus} from '@jest/test-utils';
import runJest from '../runJest';

skipSuiteOnJestCircus();

test.each`
jestArgs
${[]}
${['--waitNextEventLoopTurnForUnhandledRejectionEvents']}
`('prints useful error for requires after test is done', ({jestArgs}) => {
const {stderr} = runJest('require-after-teardown', jestArgs);

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

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

0 comments on commit 2cfa23f

Please sign in to comment.