Wiki source code of Mock Dependencies in a TypeScript Unit Test
Last modified by Vincent Massol on 2026/11/17 19:57
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | Replace the dependencies of the code under test with typed mocks in a Vitest unit test, as recommended for the [[Front-End Test>>doc:documentation.xs.dev.front-end.test.WebHome]] packages. | ||
| 2 | |||
| 3 | 1. Add the library to the package {{code language="bash"}}pnpm add -D vitest-mock-extended{{/code}} | ||
| 4 | 1. Import ##mock## in the test {{code language="typescript"}}import { mock } from "vitest-mock-extended";{{/code}} | ||
| 5 | 1. Create a mock of the dependency, with an optional partial implementation((( | ||
| 6 | {{code language="typescript"}} | ||
| 7 | import { EntityType } from "@xwiki/platform-model-api"; | ||
| 8 | import type { DocumentReference } from "@xwiki/platform-model-api"; | ||
| 9 | |||
| 10 | // The mock is typed MockProxy<DocumentReference> & DocumentReference: no cast is needed. | ||
| 11 | const sourceDocument = mock<DocumentReference>({ | ||
| 12 | type: EntityType.DOCUMENT, | ||
| 13 | name: "Source", | ||
| 14 | }); | ||
| 15 | {{/code}} | ||
| 16 | ))) | ||
| 17 | 1. Stub a return value for specific arguments with ##calledWith## on the method((( | ||
| 18 | {{code language="typescript"}} | ||
| 19 | import type { DocumentService } from "@xwiki/platform-document-api"; | ||
| 20 | import type { Container } from "inversify"; | ||
| 21 | |||
| 22 | const documentService = mock<DocumentService>(); | ||
| 23 | const container = mock<Container>(); | ||
| 24 | |||
| 25 | container.get.calledWith("DocumentService").mockReturnValue(documentService); | ||
| 26 | {{/code}} | ||
| 27 | ))) | ||
| 28 | 1. Throw on a service id that has no stub with ##fallbackMockImplementation## on the ##Container## mock((( | ||
| 29 | {{code language="typescript"}} | ||
| 30 | const container = mock<Container>( | ||
| 31 | {}, | ||
| 32 | { | ||
| 33 | fallbackMockImplementation: (id: unknown) => { | ||
| 34 | throw new Error(`Unexpected service: ${String(id)}`); | ||
| 35 | }, | ||
| 36 | }, | ||
| 37 | ); | ||
| 38 | {{/code}} | ||
| 39 | ))) | ||
| 40 | 1. Declare a mock shared by the tests of a suite as ##MockProxy##, create it in ##beforeEach## and reset it with ##mockReset##((( | ||
| 41 | {{code language="typescript"}} | ||
| 42 | import { beforeEach } from "vitest"; | ||
| 43 | import { mock, mockReset } from "vitest-mock-extended"; | ||
| 44 | import type { MockProxy } from "vitest-mock-extended"; | ||
| 45 | |||
| 46 | let documentService: MockProxy<DocumentService>; | ||
| 47 | |||
| 48 | beforeEach(() => { | ||
| 49 | documentService = mock<DocumentService>(); | ||
| 50 | }); | ||
| 51 | |||
| 52 | // A mock created once for the whole file is reset instead. | ||
| 53 | const serializer = mock<RemoteURLSerializer>(); | ||
| 54 | beforeEach(() => { | ||
| 55 | mockReset(serializer); | ||
| 56 | }); | ||
| 57 | {{/code}} | ||
| 58 | ))) | ||
| 59 | 1. Stub ##get## of a provider to return a mock when the code under test calls ##provider.get().method()##((( | ||
| 60 | {{code language="typescript"}} | ||
| 61 | import type { | ||
| 62 | RemoteURLSerializer, | ||
| 63 | RemoteURLSerializerProvider, | ||
| 64 | } from "@xwiki/platform-model-remote-url-api"; | ||
| 65 | |||
| 66 | const serializer = mock<RemoteURLSerializer>(); | ||
| 67 | serializer.serialize.mockReturnValue("/download/my/page/cat.png"); | ||
| 68 | const provider = mock<RemoteURLSerializerProvider>(); | ||
| 69 | provider.get.mockReturnValue(serializer); | ||
| 70 | {{/code}} | ||
| 71 | ))) | ||
| 72 | 1. Run the tests {{code language="bash"}}pnpm vitest run{{/code}} The tests pass. A lookup of a service without a stub fails at once, at the line of the lookup, with the message below.((( | ||
| 73 | {{code language="none"}} | ||
| 74 | Error: Unexpected service: DocumentService | ||
| 75 | {{/code}} | ||
| 76 | ))) |