-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jest-circus] Omit
expect.hasAssertions()
errors if a test already …
…has errors (#14866)
- Loading branch information
Showing
4 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/jest-circus/src/legacy-code-todo-rewrite/__tests__/jestAdapterInit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/jest-circus/src/legacy-code-todo-rewrite/__tests__/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../../../../tsconfig.test.json", | ||
"include": ["./**/*"], | ||
"references": [{"path": "../../../"}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters