Skip to content

Commit

Permalink
[jest-circus] Don't show expect.hasAssertions() errors if a test is…
Browse files Browse the repository at this point in the history
… already failing.

In the case of early-throwing tests, this is often just spammy.
  • Loading branch information
danbeam committed Jan 25, 2024
1 parent 8c78a08 commit 2771bd7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
@@ -0,0 +1,47 @@
/**
* 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, expect, 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 beforeLength = errors.length;

const event: Circus.Event = {
name: 'test_done',
test: {errors} as unknown as Circus.TestEntry,
};

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);
});
@@ -0,0 +1,5 @@
{
"extends": "../../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../../"}]
}
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 @@ -274,8 +275,11 @@ const eventHandler = async (event: Circus.Event) => {

const _addExpectedAssertionErrors = (test: Circus.TestEntry) => {
const failures = jestExpect.extractExpectedAssertionsErrors();
const errors = failures.map(failure => failure.error);
test.errors = [...test.errors, ...errors];
if (test.errors.length > 0 && jestExpect.getState().isExpectingAssertions) {
// 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 +289,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);
}
};

0 comments on commit 2771bd7

Please sign in to comment.