Skip to content

Commit 6474e4b

Browse files
authored
docs: add inject dependencies section in module tests + improvements (medusajs#12993)
1 parent fc29fc6 commit 6474e4b

File tree

8 files changed

+151
-173
lines changed

8 files changed

+151
-173
lines changed

www/apps/book/app/learn/debugging-and-testing/testing-tools/modules-tests/module-example/page.mdx

Lines changed: 0 additions & 83 deletions
This file was deleted.

www/apps/book/app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@ In this chapter, you'll learn about `moduleIntegrationTestRunner` from Medusa's
1919

2020
## moduleIntegrationTestRunner Utility
2121

22-
`moduleIntegrationTestRunner` creates integration tests for a module. The integration tests run on a test Medusa application with only the specified module enabled.
22+
`moduleIntegrationTestRunner` creates integration tests for a module's service. The integration tests run on a test Medusa application with only the specified module enabled.
2323

24-
For example, assuming you have a `blog` module, create a test file at `src/modules/blog/__tests__/service.spec.ts`:
24+
For example, consider a Blog Module with a `BlogModuleService` that has a `getMessage` method:
25+
26+
```ts title="src/modules/blog/service.ts"
27+
import { MedusaService } from "@medusajs/framework/utils"
28+
import Post from "./models/post"
29+
30+
class BlogModuleService extends MedusaService({
31+
Post,
32+
}){
33+
async getMessage(): Promise<string> {
34+
return "Hello, World!"
35+
}
36+
}
37+
38+
export default BlogModuleService
39+
```
40+
41+
To create an integration test for the module's service, create the file `src/modules/blog/__tests__/service.spec.ts` with the following content:
2542

2643
```ts title="src/modules/blog/__tests__/service.spec.ts"
2744
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
@@ -34,7 +51,13 @@ moduleIntegrationTestRunner<BlogModuleService>({
3451
moduleModels: [Post],
3552
resolve: "./src/modules/blog",
3653
testSuite: ({ service }) => {
37-
// TODO write tests
54+
describe("BlogModuleService", () => {
55+
it("says hello world", () => {
56+
const message = service.getMessage()
57+
58+
expect(message).toEqual("Hello, World!")
59+
})
60+
})
3861
},
3962
})
4063

@@ -43,10 +66,10 @@ jest.setTimeout(60 * 1000)
4366

4467
The `moduleIntegrationTestRunner` function accepts as a parameter an object with the following properties:
4568

46-
- `moduleName`: The name of the module.
69+
- `moduleName`: The registration name of the module.
4770
- `moduleModels`: An array of models in the module. Refer to [this section](#write-tests-for-modules-without-data-models) if your module doesn't have data models.
4871
- `resolve`: The path to the module's directory.
49-
- `testSuite`: A function that defines the tests to run.
72+
- `testSuite`: A function that defines [Jest](https://jestjs.io/) tests to run.
5073

5174
The `testSuite` function accepts as a parameter an object having the `service` property, which is an instance of the module's main service.
5275

@@ -96,6 +119,8 @@ moduleIntegrationTestRunner<BlogModuleService>({
96119
})
97120
```
98121

122+
`moduleOptions` is an object of key-value pair options that your module's service receives in its constructor.
123+
99124
---
100125

101126
## Write Tests for Modules without Data Models
@@ -123,6 +148,58 @@ jest.setTimeout(60 * 1000)
123148

124149
---
125150

151+
## Inject Dependencies in Module Tests
152+
153+
Some modules have injected dependencies, such as the [Event Module's service](!resources!/infrastructure-modules/event). When writing tests for those modules, you need to inject the dependencies that the module's service requires to avoid errors.
154+
155+
You can inject dependencies as mock dependencies that simulate the behavior of the original service. This way you avoid unexpected behavior or results, such as sending real events or making real API calls.
156+
157+
To inject dependencies, pass the `injectedDependencies` property to the `moduleIntegrationTestRunner` function.
158+
159+
For example:
160+
161+
export const mockDependenciesHighlights = [
162+
["11", "injectedDependencies", "Inject dependencies into the module's service."],
163+
["12", "Modules.EVENT_BUS", "The registration name of the dependency."],
164+
["12", "MockEventBusService", "The mock service to inject."]
165+
]
166+
167+
```ts title="src/modules/blog/__tests__/service.spec.ts" highlights={mockDependenciesHighlights}
168+
import { MockEventBusService, moduleIntegrationTestRunner } from "@medusajs/test-utils"
169+
import { BLOG_MODULE } from ".."
170+
import BlogModuleService from "../service"
171+
import Post from "../models/post"
172+
import { Modules } from "@medusajs/framework/utils"
173+
174+
moduleIntegrationTestRunner<BlogModuleService>({
175+
moduleName: BLOG_MODULE,
176+
moduleModels: [Post],
177+
resolve: "./src/modules/blog",
178+
injectedDependencies: {
179+
[Modules.EVENT_BUS]: new MockEventBusService()
180+
},
181+
testSuite: ({ service }) => {
182+
describe("BlogModuleService", () => {
183+
it("says hello world", async () => {
184+
const message = await service.getMessage()
185+
186+
expect(message).toEqual("Hello, World!")
187+
})
188+
})
189+
},
190+
})
191+
192+
jest.setTimeout(60 * 1000)
193+
```
194+
195+
`injectedDependencies`'s value is an object whose keys are registration names of the dependencies you want to inject, and the values are the mock services.
196+
197+
In this example, you inject a mock Event Module service into the `BlogModuleService`. Medusa exposes a `MockEventBusService` class that you can use to mock the Event Module's service.
198+
199+
For other modules, you can create a mock service that implements the same interface as the original service. Make sure to use the same registration name as the original service when injecting it.
200+
201+
---
202+
126203
### Other Options and Inputs
127204

128205
Refer to [the Test Tooling Reference](!resources!/test-tools-reference/moduleIntegrationTestRunner) for other available parameter options and inputs of the `testSuite` function.

www/apps/book/app/learn/debugging-and-testing/testing-tools/page.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Finally, add the following scripts to `package.json`:
7878
"scripts": {
7979
// ...
8080
"test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
81-
"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
81+
"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
8282
"test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
8383
},
8484
```

www/apps/book/generated/edit-dates.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ export const generatedEditDates = {
5353
"app/learn/debugging-and-testing/testing-tools/integration-tests/api-routes/page.mdx": "2025-03-18T15:06:27.864Z",
5454
"app/learn/debugging-and-testing/testing-tools/integration-tests/page.mdx": "2024-12-09T15:52:01.019Z",
5555
"app/learn/debugging-and-testing/testing-tools/integration-tests/workflows/page.mdx": "2025-02-11T15:56:03.835Z",
56-
"app/learn/debugging-and-testing/testing-tools/page.mdx": "2025-01-31T13:19:02.587Z",
56+
"app/learn/debugging-and-testing/testing-tools/page.mdx": "2025-07-18T12:02:52.835Z",
5757
"app/learn/debugging-and-testing/testing-tools/unit-tests/module-example/page.mdx": "2024-09-02T11:04:27.232Z",
5858
"app/learn/debugging-and-testing/testing-tools/unit-tests/page.mdx": "2024-09-02T11:03:26.997Z",
5959
"app/learn/fundamentals/modules/service-constraints/page.mdx": "2025-03-18T15:12:46.006Z",
6060
"app/learn/fundamentals/api-routes/responses/page.mdx": "2024-10-21T13:30:21.367Z",
6161
"app/learn/fundamentals/api-routes/validation/page.mdx": "2025-03-24T06:52:47.896Z",
6262
"app/learn/fundamentals/api-routes/errors/page.mdx": "2025-06-19T16:09:08.563Z",
6363
"app/learn/fundamentals/admin/constraints/page.mdx": "2024-10-21T13:30:21.366Z",
64-
"app/learn/debugging-and-testing/testing-tools/modules-tests/module-example/page.mdx": "2025-03-18T15:07:22.640Z",
65-
"app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2025-03-24T06:54:21.249Z",
64+
"app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2025-07-18T12:24:02.385Z",
6665
"app/learn/fundamentals/module-links/custom-columns/page.mdx": "2025-03-11T13:29:54.752Z",
6766
"app/learn/fundamentals/module-links/directions/page.mdx": "2025-03-17T12:52:06.161Z",
6867
"app/learn/fundamentals/module-links/page.mdx": "2025-04-17T08:50:17.036Z",

www/apps/book/generated/sidebar.mjs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,18 +1117,7 @@ export const generatedSidebars = [
11171117
"type": "link",
11181118
"path": "/learn/debugging-and-testing/testing-tools/modules-tests",
11191119
"title": "Modules Tests",
1120-
"children": [
1121-
{
1122-
"loaded": true,
1123-
"isPathHref": true,
1124-
"type": "link",
1125-
"path": "/learn/debugging-and-testing/testing-tools/modules-tests/module-example",
1126-
"title": "Example",
1127-
"children": [],
1128-
"chapterTitle": "7.3.1. Example",
1129-
"number": "7.3.1."
1130-
}
1131-
],
1120+
"children": [],
11321121
"chapterTitle": "7.3. Modules Tests",
11331122
"number": "7.3."
11341123
},

0 commit comments

Comments
 (0)