Skip to content

Commit

Permalink
test: fix async assertion warning (#6959)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Nov 25, 2024
1 parent 021944c commit a6c9771
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
14 changes: 10 additions & 4 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AssertionError } from 'node:assert'
import { stripVTControlCharacters } from 'node:util'
import { generateToBeMessage } from '@vitest/expect'
import { processError } from '@vitest/utils/error'
import { describe, expect, it, vi } from 'vitest'
import { beforeAll, describe, expect, it, vi } from 'vitest'

class TestError extends Error {}

Expand Down Expand Up @@ -917,14 +917,14 @@ describe('async expect', () => {
await expect((async () => {
throw new Error('err')
})()).rejects.toThrow('err')
expect((async () => {
await expect((async () => {
throw new TestError('error')
})()).rejects.toThrow(TestError)
const err = new Error('hello world')
expect((async () => {
await expect((async () => {
throw err
})()).rejects.toThrow(err)
expect((async () => {
await expect((async () => {
throw new Error('message')
})()).rejects.toThrow(expect.objectContaining({
message: expect.stringContaining('mes'),
Expand Down Expand Up @@ -1011,6 +1011,12 @@ describe('async expect', () => {
})

describe('promise auto queuing', () => {
// silence warning
beforeAll(() => {
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {})
return () => spy.mockRestore()
})

it.fails('fails', () => {
expect(new Promise((resolve, reject) => setTimeout(reject, 500)))
.resolves
Expand Down
4 changes: 2 additions & 2 deletions test/core/test/mocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ describe('default exported classes', () => {
})
})

test('async functions should be mocked', () => {
test('async functions should be mocked', async () => {
expect(asyncFunc()).toBeUndefined()
expect(vi.mocked(asyncFunc).mockResolvedValue).toBeDefined()
vi.mocked(asyncFunc).mockResolvedValue('foo')
expect(asyncFunc()).resolves.toBe('foo')
await expect(asyncFunc()).resolves.toBe('foo')
})

function getError(cb: () => void): string {
Expand Down
6 changes: 3 additions & 3 deletions test/core/test/snapshot-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ describe('snapshots', () => {
for (const [path, file] of Object.entries(files)) {
test(path, async () => {
const entries = JSON.parse(await file()) as any[]
expect(entries.map(i => objectToCSS(i[0], i[1])).join('\n'))
await expect(entries.map(i => objectToCSS(i[0], i[1])).join('\n'))
.toMatchFileSnapshot(path.replace('input.json', 'output.css'))
})
}
})

test('handle empty file', () => {
expect('').toMatchFileSnapshot('./fixtures/snapshot-empty.txt')
test('handle empty file', async () => {
await expect('').toMatchFileSnapshot('./fixtures/snapshot-empty.txt')
})
4 changes: 2 additions & 2 deletions test/core/test/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, test, vi } from 'vitest'
describe('waitFor', () => {
describe('options', () => {
test('timeout', async () => {
expect(async () => {
await expect(async () => {
await vi.waitFor(() => {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('waitFor', () => {
describe('waitUntil', () => {
describe('options', () => {
test('timeout', async () => {
expect(async () => {
await expect(async () => {
await vi.waitUntil(() => {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down

0 comments on commit a6c9771

Please sign in to comment.