You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/book/app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx
+82-5Lines changed: 82 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,9 +19,26 @@ In this chapter, you'll learn about `moduleIntegrationTestRunner` from Medusa's
19
19
20
20
## moduleIntegrationTestRunner Utility
21
21
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.
23
23
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:
The `moduleIntegrationTestRunner` function accepts as a parameter an object with the following properties:
45
68
46
-
-`moduleName`: The name of the module.
69
+
-`moduleName`: The registration name of the module.
47
70
-`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.
48
71
-`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.
50
73
51
74
The `testSuite` function accepts as a parameter an object having the `service` property, which is an instance of the module's main service.
`moduleOptions` is an object of key-value pair options that your module's service receives in its constructor.
123
+
99
124
---
100
125
101
126
## Write Tests for Modules without Data Models
@@ -123,6 +148,58 @@ jest.setTimeout(60 * 1000)
123
148
124
149
---
125
150
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
+
exportconst 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."]
`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
+
126
203
### Other Options and Inputs
127
204
128
205
Refer to [the Test Tooling Reference](!resources!/test-tools-reference/moduleIntegrationTestRunner) for other available parameter options and inputs of the `testSuite` function.
0 commit comments