diff --git a/docs/api/expect.md b/docs/api/expect.md index c89f9c0d14af..5f77e6d106de 100644 --- a/docs/api/expect.md +++ b/docs/api/expect.md @@ -1029,6 +1029,10 @@ type Awaitable = T | PromiseLike 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 diff --git a/docs/api/index.md b/docs/api/index.md index 69d363a26f78..9dd5179cfb64 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -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 }) => { @@ -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). ::: diff --git a/docs/guide/features.md b/docs/guide/features.md index 9c48626fdb66..a2387f838dde 100644 --- a/docs/guide/features.md +++ b/docs/guide/features.md @@ -44,7 +44,7 @@ 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' @@ -52,8 +52,8 @@ 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 }) => { /* ... */ }) }) ``` @@ -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. diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index b4e1b3297bc5..29f16929daf8 100644 --- a/docs/guide/snapshot.md +++ b/docs/guide/snapshot.md @@ -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. @@ -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.