Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update snapshot section to provide details on how to use with concurrent test #2733

Merged
merged 6 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/expect.md
Original file line number Diff line number Diff line change
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.
asdfjkalsdfla marked this conversation as resolved.
Show resolved Hide resolved
:::


## expect.hasAssertions

Expand Down
16 changes: 15 additions & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
asdfjkalsdfla marked this conversation as resolved.
Show resolved Hide resolved
:::

## 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.
asdfjkalsdfla marked this conversation as resolved.
Show resolved Hide resolved
:::

## 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