Skip to content

Commit

Permalink
fix: assertions error message (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dewey Ou committed Mar 25, 2022
1 parent f4f26cb commit f52cd93
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -15,7 +15,7 @@ if (!Object.prototype.hasOwnProperty.call(global, MATCHERS_OBJECT)) {
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberError: null,
expectedAssertionsNumberErrorGen: null,
}
Object.defineProperty(global, MATCHERS_OBJECT, {
value: {
Expand Down Expand Up @@ -558,13 +558,13 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
chai.expect,
'assertions',
function assertions(expected: number) {
const error = new Error(`expected number of assertions to be ${expected}, but got ${getState().assertionCalls}`)
const errorGen = () => new Error(`expected number of assertions to be ${expected}, but got ${getState().assertionCalls}`)
if (Error.captureStackTrace)
Error.captureStackTrace(error, assertions)
Error.captureStackTrace(errorGen(), assertions)

setState({
expectedAssertionsNumber: expected,
expectedAssertionsNumberError: error,
expectedAssertionsNumberErrorGen: errorGen,
})
},
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/integrations/chai/types.ts
Expand Up @@ -28,7 +28,7 @@ export interface MatcherState {
) => boolean
expand?: boolean
expectedAssertionsNumber?: number | null
expectedAssertionsNumberError?: Error | null
expectedAssertionsNumberErrorGen?: (() => Error) | null
isExpectingAssertions?: boolean
isExpectingAssertionsError?: Error | null
isNot: boolean
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -90,14 +90,14 @@ export async function runTest(test: Test) {
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberError: null,
expectedAssertionsNumberErrorGen: null,
testPath: test.suite.file?.filepath,
currentTestName: getFullName(test),
})
await getFn(test)()
const { assertionCalls, expectedAssertionsNumber, expectedAssertionsNumberError, isExpectingAssertions, isExpectingAssertionsError } = getState()
const { assertionCalls, expectedAssertionsNumber, expectedAssertionsNumberErrorGen, isExpectingAssertions, isExpectingAssertionsError } = getState()
if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
throw expectedAssertionsNumberError
throw expectedAssertionsNumberErrorGen!()
if (isExpectingAssertions === true && assertionCalls === 0)
throw isExpectingAssertionsError

Expand Down
20 changes: 19 additions & 1 deletion test/core/test/jest-expect.test.ts
Expand Up @@ -124,7 +124,6 @@ describe('jest-expect', () => {
{ name: 'Mohammad' },
]))


expect('Mohammad').toEqual(expect.stringMatching(/Moh/))
expect('Mohammad').not.toEqual(expect.stringMatching(/jack/))

Expand Down Expand Up @@ -219,6 +218,25 @@ describe('jest-expect', () => {
expect(1).toBe(1)
})

it('assertions when asynchronous code', async() => {
expect.assertions(3)
await Promise.all([
expect(1).toBe(1),
expect(1).toBe(1),
expect(1).toBe(1),
])
})

it.fails('assertions when asynchronous code', async() => {
// Error: expected number of assertions to be 2, but got 3
expect.assertions(2)
await Promise.all([
expect(1).toBe(1),
expect(1).toBe(1),
expect(1).toBe(1),
])
})

it.fails('has assertions', () => {
expect.hasAssertions()
})
Expand Down

0 comments on commit f52cd93

Please sign in to comment.