Skip to content

Commit

Permalink
docs: update snapshot section to provide details on how to use with c…
Browse files Browse the repository at this point in the history
…oncurrent test (#2733)

Co-authored-by: Tom Thumb <github@asdfjkalsdfla.com>
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
  • Loading branch information
3 people committed Feb 14, 2023
1 parent 9e03f2b commit b67a5fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/api/expect.md
Expand Up @@ -1029,6 +1029,10 @@ type Awaitable<T> = T | PromiseLike<T>
await doAsync(callback1, callback2)
})
```
::: warning
When using `assertions` with async concurrent tests, `expect` from the local [Test Context](/guide/test-context.md) must be used to ensure the right test is detected.
:::


## expect.hasAssertions

Expand Down
16 changes: 15 additions & 1 deletion docs/api/index.md
Expand Up @@ -149,7 +149,8 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t
test.todo.concurrent(/* ... */) // or test.concurrent.todo(/* ... */)
```

When using Snapshots with async concurrent tests, due to the limitation of JavaScript, you need to use the `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being detected.
When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context.md) to ensure the right test is detected.


```ts
test.concurrent('test 1', async ({ expect }) => {
Expand Down Expand Up @@ -567,6 +568,19 @@ You cannot use this syntax when using Vitest as [type checker](/guide/testing-ty
describe.todo.concurrent(/* ... */) // or describe.concurrent.todo(/* ... */)
```

When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context.md) to ensure the right test is detected.


```ts
describe.concurrent('suite', () => {
test('concurrent test 1', async ({ expect }) => {
expect(foo).toMatchSnapshot()
})
test('concurrent test 2', async ({ expect }) => {
expect(foo).toMatchSnapshot()
})
})
```
::: warning
You cannot use this syntax, when using Vitest as [type checker](/guide/testing-types).
:::
Expand Down
16 changes: 10 additions & 6 deletions docs/guide/features.md
Expand Up @@ -44,16 +44,16 @@ Learn more about [Test Filtering](./filtering.md).

## Running tests concurrently

Use `.concurrent` in consecutive tests to run them in parallel.
Use `.concurrent` in consecutive tests to run them in parallel.

```ts
import { describe, it } from 'vitest'

// The two tests marked with concurrent will be run in parallel
describe('suite', () => {
it('serial test', async () => { /* ... */ })
it.concurrent('concurrent test 1', async () => { /* ... */ })
it.concurrent('concurrent test 2', async () => { /* ... */ })
it.concurrent('concurrent test 1', async ({ expect }) => { /* ... */ })
it.concurrent('concurrent test 2', async ({ expect }) => { /* ... */ })
})
```

Expand All @@ -64,14 +64,18 @@ import { describe, it } from 'vitest'

// All tests within this suite will be run in parallel
describe.concurrent('suite', () => {
it('concurrent test 1', async () => { /* ... */ })
it('concurrent test 2', async () => { /* ... */ })
it.concurrent('concurrent test 3', async () => { /* ... */ })
it('concurrent test 1', async ({ expect }) => { /* ... */ })
it('concurrent test 2', async ({ expect }) => { /* ... */ })
it.concurrent('concurrent test 3', async ({ expect }) => { /* ... */ })
})
```

You can also use `.skip`, `.only`, and `.todo` with concurrent suites and tests. Read more in the [API Reference](/api/#test-concurrent).

::: warning
When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context.md) to ensure the right test is detected.
:::

## Snapshot

[Jest-compatible](https://jestjs.io/docs/snapshot-testing) snapshot support.
Expand Down
8 changes: 8 additions & 0 deletions docs/guide/snapshot.md
Expand Up @@ -33,6 +33,10 @@ exports['toUpperCase 1'] = '"FOOBAR"'

The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process. On subsequent test runs, Vitest will compare the rendered output with the previous snapshot. If they match, the test will pass. If they don't match, either the test runner found a bug in your code that should be fixed, or the implementation has changed and the snapshot needs to be updated.

::: warning
When using Snapshots with async concurrent tests, `expect` from the local [Test Context](/guide/test-context.md) must be used to ensure the right test is detected.
:::

## Inline Snapshots

Similarly, you can use the [`toMatchInlineSnapshot()`](/api/expect#tomatchinlinesnapshot) to store the snapshot inline within the test file.
Expand All @@ -59,6 +63,10 @@ it('toUpperCase', () => {

This allows you to see the expected output directly without jumping across different files.

::: warning
When using Snapshots with async concurrent tests, `expect` from the local [Test Context](/guide/test-context.md) must be used to ensure the right test is detected.
:::

## Updating Snapshots

When the received value doesn't match the snapshot, the test fails and shows you the difference between them. When the snapshot change is expected, you may want to update the snapshot from the current state.
Expand Down

0 comments on commit b67a5fb

Please sign in to comment.