Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into userquin/feat-allow-run-individua…
Browse files Browse the repository at this point in the history
…l-tests-and-suites

# Conflicts:
#	packages/vitest/src/node/core.ts
  • Loading branch information
userquin committed Nov 13, 2024
2 parents 6873a7c + b915aa6 commit 06aaf99
Show file tree
Hide file tree
Showing 133 changed files with 3,501 additions and 1,229 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@c3a1bb2c992d77180ae65be6ae6c166cf40f857c # v45.0.3
uses: tj-actions/changed-files@4edd678ac3f81e2dc578756871e4d00c19191daf # v45.0.4
with:
files: |
docs/**
Expand Down
68 changes: 67 additions & 1 deletion docs/api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('element exists', async () => {
```

::: warning
`expect.poll` makes every assertion asynchronous, so do not forget to await it otherwise you might get unhandled promise rejections.
`expect.poll` makes every assertion asynchronous, so you need to await it. Since Vitest 2.2, if you forget to await it, the test will fail with a warning to do so.

`expect.poll` doesn't work with several matchers:

Expand Down Expand Up @@ -876,6 +876,68 @@ test('spy function', () => {
})
```

## toHaveBeenCalledBefore <Version>2.2.0</Version> {#tohavebeencalledbefore}

- **Type**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`

This assertion checks if a `Mock` was called before another `Mock`.

```ts
test('calls mock1 before mock2', () => {
const mock1 = vi.fn()
const mock2 = vi.fn()

mock1()
mock2()
mock1()

expect(mock1).toHaveBeenCalledBefore(mock2)
})
```

## toHaveBeenCalledAfter <Version>2.2.0</Version> {#tohavebeencalledafter}

- **Type**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`

This assertion checks if a `Mock` was called after another `Mock`.

```ts
test('calls mock1 after mock2', () => {
const mock1 = vi.fn()
const mock2 = vi.fn()

mock2()
mock1()
mock2()

expect(mock1).toHaveBeenCalledAfter(mock2)
})
```

## toHaveBeenCalledExactlyOnceWith <Version>2.2.0</Version> {#tohavebeencalledexactlyoncewith}

- **Type**: `(...args: any[]) => Awaitable<void>`

This assertion checks if a function was called exactly once and with certain parameters. Requires a spy function to be passed to `expect`.

```ts
import { expect, test, vi } from 'vitest'

const market = {
buy(subject: string, amount: number) {
// ...
},
}

test('spy function', () => {
const buySpy = vi.spyOn(market, 'buy')

market.buy('apples', 10)

expect(buySpy).toHaveBeenCalledExactlyOnceWith('apples', 10)
})
```

## toHaveBeenLastCalledWith

- **Type**: `(...args: any[]) => Awaitable<void>`
Expand Down Expand Up @@ -1185,6 +1247,8 @@ test('buyApples returns new stock id', async () => {

:::warning
If the assertion is not awaited, then you will have a false-positive test that will pass every time. To make sure that assertions are actually called, you may use [`expect.assertions(number)`](#expect-assertions).

Since Vitest 2.2, if a method is not awaited, Vitest will show a warning at the end of the test. In Vitest 3, the test will be marked as "failed" if the assertion is not awaited.
:::

## rejects
Expand Down Expand Up @@ -1214,6 +1278,8 @@ test('buyApples throws an error when no id provided', async () => {

:::warning
If the assertion is not awaited, then you will have a false-positive test that will pass every time. To make sure that assertions were actually called, you can use [`expect.assertions(number)`](#expect-assertions).

Since Vitest 2.2, if a method is not awaited, Vitest will show a warning at the end of the test. In Vitest 3, the test will be marked as "failed" if the assertion is not awaited.
:::

## expect.assertions
Expand Down
Loading

0 comments on commit 06aaf99

Please sign in to comment.