From 539f55e794e8d7f3ca20b9ee5066be3984767951 Mon Sep 17 00:00:00 2001 From: Tom Thumb Date: Sun, 22 Jan 2023 09:26:28 -0500 Subject: [PATCH 1/6] docs: update Snapshot section to provide details on how to use with concurrent test close #2729 --- docs/guide/features.md | 12 ++++++------ docs/guide/snapshot.md | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/guide/features.md b/docs/guide/features.md index 9c48626fdb66..fc13b3c04524 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. Also, be sure to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being detected. ```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,9 +64,9 @@ 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 }) => { /* ... */ }) }) ``` diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index b4e1b3297bc5..5ac0039dfdff 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, you need to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being 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, you need to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being 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. From 5157318621ca0a6cdd1bfefbbcbec335fdba5491 Mon Sep 17 00:00:00 2001 From: Tom Thumb <74577069+asdfjkalsdfla@users.noreply.github.com> Date: Sat, 4 Feb 2023 12:18:49 -0500 Subject: [PATCH 2/6] Add ref to expect.assertions --- docs/api/index.md | 4 +++- docs/guide/features.md | 6 +++++- docs/guide/snapshot.md | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/api/index.md b/docs/api/index.md index 69d363a26f78..e1fb3e5e722c 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -149,13 +149,15 @@ 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 using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. ```ts test.concurrent('test 1', async ({ expect }) => { + expert.assertions(1) expect(foo).toMatchSnapshot() }) test.concurrent('test 2', async ({ expect }) => { + expert.assertions(1) expect(foo).toMatchSnapshot() }) ``` diff --git a/docs/guide/features.md b/docs/guide/features.md index fc13b3c04524..e10634723013 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. Also, be sure to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being detected. +Use `.concurrent` in consecutive tests to run them in parallel. ```ts import { describe, it } from 'vitest' @@ -72,6 +72,10 @@ describe.concurrent('suite', () => { You can also use `.skip`, `.only`, and `.todo` with concurrent suites and tests. Read more in the [API Reference](/api/#test-concurrent). +::: warning +When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. +::: + ## Snapshot [Jest-compatible](https://jestjs.io/docs/snapshot-testing) snapshot support. diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index 5ac0039dfdff..9e26975dddfb 100644 --- a/docs/guide/snapshot.md +++ b/docs/guide/snapshot.md @@ -34,7 +34,7 @@ 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, you need to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being detected. +When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. ::: ## Inline Snapshots @@ -64,7 +64,7 @@ it('toUpperCase', () => { This allows you to see the expected output directly without jumping across different files. ::: warning -When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) to ensure the right test is being detected. +When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. ::: ## Updating Snapshots From e74749467f4eb2aa817f922db0a3d5fc10a40657 Mon Sep 17 00:00:00 2001 From: Tom Thumb <74577069+asdfjkalsdfla@users.noreply.github.com> Date: Sun, 12 Feb 2023 08:27:49 -0500 Subject: [PATCH 3/6] Proper warning on assertions --- docs/api/expect.md | 4 ++++ docs/api/index.md | 5 ++--- docs/guide/features.md | 2 +- docs/guide/snapshot.md | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/api/expect.md b/docs/api/expect.md index c89f9c0d14af..995aee0cdad8 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 e1fb3e5e722c..b3c06bdaf4e7 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -149,15 +149,14 @@ 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, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. + 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 being detected. + ```ts test.concurrent('test 1', async ({ expect }) => { - expert.assertions(1) expect(foo).toMatchSnapshot() }) test.concurrent('test 2', async ({ expect }) => { - expert.assertions(1) expect(foo).toMatchSnapshot() }) ``` diff --git a/docs/guide/features.md b/docs/guide/features.md index e10634723013..a2387f838dde 100644 --- a/docs/guide/features.md +++ b/docs/guide/features.md @@ -73,7 +73,7 @@ describe.concurrent('suite', () => { You can also use `.skip`, `.only`, and `.todo` with concurrent suites and tests. Read more in the [API Reference](/api/#test-concurrent). ::: warning -When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. +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 diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index 9e26975dddfb..44eb86a3680e 100644 --- a/docs/guide/snapshot.md +++ b/docs/guide/snapshot.md @@ -34,7 +34,7 @@ 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, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. +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 @@ -64,7 +64,7 @@ it('toUpperCase', () => { This allows you to see the expected output directly without jumping across different files. ::: warning -When using Snapshots with async concurrent tests, you need to use `expect` from the [Test Context](/guide/test-context.md) and call `expect.assertions` to ensure the test will detect any failures. +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 From e7ff099cd1df7f11e6928f5c8a19d13bbe23b763 Mon Sep 17 00:00:00 2001 From: Tom Thumb <74577069+asdfjkalsdfla@users.noreply.github.com> Date: Sun, 12 Feb 2023 08:33:29 -0500 Subject: [PATCH 4/6] mention assertions in concurrent test section --- docs/api/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/index.md b/docs/api/index.md index b3c06bdaf4e7..5a26af04d3b2 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -149,7 +149,7 @@ 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, `expect` from the local [Test Context](/guide/test-context.md) must be used 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 From 81234574ad3ac389ea5b63031ada1d00b849542a Mon Sep 17 00:00:00 2001 From: Tom Thumb <74577069+asdfjkalsdfla@users.noreply.github.com> Date: Sun, 12 Feb 2023 08:38:52 -0500 Subject: [PATCH 5/6] add to describe concurrent --- docs/api/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/api/index.md b/docs/api/index.md index 5a26af04d3b2..9dd5179cfb64 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -568,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). ::: From ac3563eb91e2873926fa2d59a776f6fc199d39b2 Mon Sep 17 00:00:00 2001 From: Tom Thumb <74577069+asdfjkalsdfla@users.noreply.github.com> Date: Mon, 13 Feb 2023 19:51:30 -0500 Subject: [PATCH 6/6] Apply suggestions from code review remove extra spaces Co-authored-by: Anjorin Damilare --- docs/api/expect.md | 2 +- docs/guide/snapshot.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api/expect.md b/docs/api/expect.md index 995aee0cdad8..5f77e6d106de 100644 --- a/docs/api/expect.md +++ b/docs/api/expect.md @@ -1030,7 +1030,7 @@ type Awaitable = T | PromiseLike }) ``` ::: 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. + 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. ::: diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index 44eb86a3680e..29f16929daf8 100644 --- a/docs/guide/snapshot.md +++ b/docs/guide/snapshot.md @@ -34,7 +34,7 @@ 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. +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 @@ -64,7 +64,7 @@ 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. +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