Skip to content

Commit bdc50a9

Browse files
committed
03/03: add explicit annotation step, use type vs interface
1 parent f904243 commit bdc50a9

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

exercises/03.best-practices/03.problem.network-mocking/README.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,17 @@ export const test = testBase.extend({
148148

149149
In this fixture, you are telling Vitest to start the worker (`await startWorker()` from `src/mocks/browser.js` you've prepared earlier), then expose it to the test (`await use(worker)`), and finally call `worker.stop()` after the test is done.
150150

151-
There's just a slight issue with this. By default, fixtures in Vitest are _lazy_, which means they will not be initialized unless they are referenced in a test. Here's what that means:
151+
🐨 To help Vitest understand the types of this `worker` fixture, annotate it explicitly by creating a new type called `TestContext`, describing the worker there, and providing it as a type argument to `test.extend()`:
152+
153+
```ts filename=test-extend.ts add=1-3 highlight=5
154+
type TestContext = {
155+
worker: typeof worker
156+
}
157+
158+
export const test = testBase.extend<TestContext>({
159+
```
160+
161+
There's just a slight issue with all of this. By default, fixtures in Vitest are _lazy_, which means they will not be initialized unless they are referenced in a test. Here's what that means:
152162
153163
```ts filename=some.test.ts highlight=3,7
154164
// MSW will not start here because this test does not

exercises/03.best-practices/03.solution.network-mocking/test-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test as testBase } from 'vitest'
22
import { startWorker, worker } from './src/mocks/browser.js'
33

4-
interface TestContext {
4+
type TestContext = {
55
worker: typeof worker
66
}
77

exercises/03.best-practices/04.problem.element-presence/test-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test as testBase } from 'vitest'
22
import { startWorker, worker } from './src/mocks/browser.js'
33

4-
interface TestContext {
4+
type TestContext = {
55
worker: typeof worker
66
}
77

exercises/03.best-practices/04.solution.element-presence/test-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test as testBase } from 'vitest'
22
import { startWorker, worker } from './src/mocks/browser.js'
33

4-
interface TestContext {
4+
type TestContext = {
55
worker: typeof worker
66
}
77

exercises/03.best-practices/05.problem.page-navigation/test-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test as testBase } from 'vitest'
22
import { startWorker, worker } from './src/mocks/browser.js'
33

4-
interface TestContext {
4+
type TestContext = {
55
worker: typeof worker
66
}
77

exercises/03.best-practices/05.solution.page-navigation/test-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test as testBase } from 'vitest'
22
import { startWorker, worker } from './src/mocks/browser.js'
33

4-
interface TestContext {
4+
type TestContext = {
55
worker: typeof worker
66
}
77

0 commit comments

Comments
 (0)