Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-circus] Omit expect.hasAssertions() errors if a test already has errors #14866

Merged
merged 1 commit into from
Jan 29, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
- `[jest-circus]` Replace recursive `makeTestResults` implementation with iterative one ([#14760](https://github.com/jestjs/jest/pull/14760))
- `[jest-circus]` Omit `expect.hasAssertions()` errors if a test already has errors ([#14866](https://github.com/jestjs/jest/pull/14866))
- `[jest-circus, jest-expect, jest-snapshot]` Pass `test.failing` tests when containing failing snapshot matchers ([#14313](https://github.com/jestjs/jest/pull/14313))
- `[jest-cli]` [**BREAKING**] Validate CLI flags that require arguments receives them ([#14783](https://github.com/jestjs/jest/pull/14783))
- `[jest-config]` Make sure to respect `runInBand` option ([#14578](https://github.com/jestjs/jest/pull/14578))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 {beforeEach, it} from '@jest/globals';
import type {Circus} from '@jest/types';
import {eventHandler} from '../jestAdapterInit';

beforeEach(() => expect.hasAssertions());

it("pushes a hasAssertion() error if there's no assertions/errors", () => {
const event: Circus.Event = {
name: 'test_done',
test: {errors: []} as unknown as Circus.TestEntry,
};
const beforeLength = event.test.errors.length;

eventHandler(event);

expect(event.test.errors).toHaveLength(beforeLength + 1);
expect(event.test.errors).toEqual([
expect.getState().isExpectingAssertionsError,
]);
});

it("omits hasAssertion() errors if there's already an error", () => {
const errors = [new Error('ruh roh'), new Error('not good')];
const event: Circus.Event = {
name: 'test_done',
test: {errors} as unknown as Circus.TestEntry,
};
const beforeLength = event.test.errors.length;

eventHandler(event);

expect(event.test.errors).toHaveLength(beforeLength);
expect(event.test.errors).not.toContain(
expect.getState().isExpectingAssertionsError,
);

// Ensure test state is not accidentally leaked by e.g. not calling extractExpectedAssertionsErrors() at all.
expect(expect.getState().isExpectingAssertions).toBe(false);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../../"}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ const handleSnapshotStateAfterRetry =
}
};

const eventHandler = async (event: Circus.Event) => {
// Exported for direct access from unit tests.
export const eventHandler = async (event: Circus.Event): Promise<void> => {
switch (event.name) {
case 'test_start': {
jestExpect.setState({
Expand All @@ -273,9 +274,13 @@ const eventHandler = async (event: Circus.Event) => {
};

const _addExpectedAssertionErrors = (test: Circus.TestEntry) => {
const {isExpectingAssertions} = jestExpect.getState();
const failures = jestExpect.extractExpectedAssertionsErrors();
const errors = failures.map(failure => failure.error);
test.errors = [...test.errors, ...errors];
if (isExpectingAssertions && test.errors.length > 0) {
// Only show errors from `expect.hasAssertions()` when no other failure has happened.
return;
}
test.errors.push(...failures.map(failure => failure.error));
};

// Get suppressed errors from ``jest-matchers`` that weren't throw during
Expand All @@ -285,6 +290,6 @@ const _addSuppressedErrors = (test: Circus.TestEntry) => {
const {suppressedErrors} = jestExpect.getState();
jestExpect.setState({suppressedErrors: []});
if (suppressedErrors.length > 0) {
test.errors = [...test.errors, ...suppressedErrors];
test.errors.push(...suppressedErrors);
}
};
Loading