Skip to content

Commit

Permalink
fix: use .not in the error message if it was used
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 7, 2024
1 parent 7df5dc6 commit a6a4fb5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/browser/src/client/tester/expect-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export async function setupExpectDom() {
}
chai.util.flag(this, '_poll.element', true)

const isNot = chai.util.flag(this, 'negate')
const name = chai.util.flag(this, '_name')
const isNot = chai.util.flag(this, 'negate') as boolean
const name = chai.util.flag(this, '_name') as string
// special case for `toBeInTheDocument` matcher
if (isNot && name === 'toBeInTheDocument') {
return elementOrLocator.query()
Expand Down
6 changes: 5 additions & 1 deletion packages/vitest/src/integrations/chai/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ export function createExpectPoll(expect: ExpectStatic): ExpectStatic['poll'] {
test.onFinished ??= []
test.onFinished.push(() => {
if (!awaited) {
const negated = chai.util.flag(assertion, 'negate') ? 'not.' : ''
const name = chai.util.flag(assertion, '_poll.element') ? 'element(locator)' : 'poll(assertion)'
const error = new Error(`expect.${name}.${String(key)}() was not awaited. This assertion is asynchronous and must be awaited:\n\nawait expect.${name}.${String(key)}()\n`)
const assertionString = `expect.${name}.${negated}${String(key)}()`
const error = new Error(
`${assertionString} was not awaited. This assertion is asynchronous and must be awaited:\n\nawait ${assertionString}\n`,
)
throw copyStackTrace(error, STACK_TRACE_ERROR)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/fixtures/fails/poll-no-awaited.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('poll is not awaited once', () => {

test('poll is not awaited several times', () => {
expect.poll(() => 3).toBe(3)
expect.poll(() => 'string').toBe('string')
expect.poll(() => 'string').not.toBe('correct')
})

test('poll is not awaited but there is an async assertion afterwards', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ exports[`should fail poll-no-awaited.test.ts > poll-no-awaited.test.ts 1`] = `
AssertionError: expected 3 to be 4 // Object.is equality
Error: expect.poll(assertion).toBe() was not awaited. This assertion is asynchronous and must be awaited:
Error: expect.poll(assertion).toBe() was not awaited. This assertion is asynchronous and must be awaited:
Error: expect.poll(assertion).toBe() was not awaited. This assertion is asynchronous and must be awaited:
Error: expect.poll(assertion).not.toBe() was not awaited. This assertion is asynchronous and must be awaited:
Error: expect.poll(assertion).toBe() was not awaited. This assertion is asynchronous and must be awaited:"
`;
Expand Down

0 comments on commit a6a4fb5

Please sign in to comment.