Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ In this chapter, you'll learn about `moduleIntegrationTestRunner` from Medusa's

## moduleIntegrationTestRunner Utility

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

For example, assuming you have a `blog` module, create a test file at `src/modules/blog/__tests__/service.spec.ts`:
For example, consider a Blog Module with a `BlogModuleService` that has a `getMessage` method:

```ts title="src/modules/blog/service.ts"
import { MedusaService } from "@medusajs/framework/utils"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
Post,
}){
async getMessage(): Promise<string> {
return "Hello, World!"
}
}

export default BlogModuleService
```

To create an integration test for the module's service, create the file `src/modules/blog/__tests__/service.spec.ts` with the following content:

```ts title="src/modules/blog/__tests__/service.spec.ts"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
Expand All @@ -34,7 +51,13 @@ moduleIntegrationTestRunner<BlogModuleService>({
moduleModels: [Post],
resolve: "./src/modules/blog",
testSuite: ({ service }) => {
// TODO write tests
describe("BlogModuleService", () => {
it("says hello world", () => {
const message = service.getMessage()

expect(message).toEqual("Hello, World!")
})
})
},
})

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

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

- `moduleName`: The name of the module.
- `moduleName`: The registration name of the module.
- `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.
- `resolve`: The path to the module's directory.
- `testSuite`: A function that defines the tests to run.
- `testSuite`: A function that defines [Jest](https://jestjs.io/) tests to run.

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

Expand Down Expand Up @@ -96,6 +119,8 @@ moduleIntegrationTestRunner<BlogModuleService>({
})
```

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

---

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

---

## Inject Dependencies in Module Tests

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.

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.

To inject dependencies, pass the `injectedDependencies` property to the `moduleIntegrationTestRunner` function.

For example:

export const mockDependenciesHighlights = [
["11", "injectedDependencies", "Inject dependencies into the module's service."],
["12", "Modules.EVENT_BUS", "The registration name of the dependency."],
["12", "MockEventBusService", "The mock service to inject."]
]

```ts title="src/modules/blog/__tests__/service.spec.ts" highlights={mockDependenciesHighlights}
import { MockEventBusService, moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { BLOG_MODULE } from ".."
import BlogModuleService from "../service"
import Post from "../models/post"
import { Modules } from "@medusajs/framework/utils"

moduleIntegrationTestRunner<BlogModuleService>({
moduleName: BLOG_MODULE,
moduleModels: [Post],
resolve: "./src/modules/blog",
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService()
},
testSuite: ({ service }) => {
describe("BlogModuleService", () => {
it("says hello world", async () => {
const message = await service.getMessage()

expect(message).toEqual("Hello, World!")
})
})
},
})

jest.setTimeout(60 * 1000)
```

`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.

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.

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.

---

### Other Options and Inputs

Refer to [the Test Tooling Reference](!resources!/test-tools-reference/moduleIntegrationTestRunner) for other available parameter options and inputs of the `testSuite` function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Finally, add the following scripts to `package.json`:
"scripts": {
// ...
"test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
"test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
},
```
Expand Down
5 changes: 2 additions & 3 deletions www/apps/book/generated/edit-dates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ export const generatedEditDates = {
"app/learn/debugging-and-testing/testing-tools/integration-tests/api-routes/page.mdx": "2025-03-18T15:06:27.864Z",
"app/learn/debugging-and-testing/testing-tools/integration-tests/page.mdx": "2024-12-09T15:52:01.019Z",
"app/learn/debugging-and-testing/testing-tools/integration-tests/workflows/page.mdx": "2025-02-11T15:56:03.835Z",
"app/learn/debugging-and-testing/testing-tools/page.mdx": "2025-01-31T13:19:02.587Z",
"app/learn/debugging-and-testing/testing-tools/page.mdx": "2025-07-18T12:02:52.835Z",
"app/learn/debugging-and-testing/testing-tools/unit-tests/module-example/page.mdx": "2024-09-02T11:04:27.232Z",
"app/learn/debugging-and-testing/testing-tools/unit-tests/page.mdx": "2024-09-02T11:03:26.997Z",
"app/learn/fundamentals/modules/service-constraints/page.mdx": "2025-03-18T15:12:46.006Z",
"app/learn/fundamentals/api-routes/responses/page.mdx": "2024-10-21T13:30:21.367Z",
"app/learn/fundamentals/api-routes/validation/page.mdx": "2025-03-24T06:52:47.896Z",
"app/learn/fundamentals/api-routes/errors/page.mdx": "2025-06-19T16:09:08.563Z",
"app/learn/fundamentals/admin/constraints/page.mdx": "2024-10-21T13:30:21.366Z",
"app/learn/debugging-and-testing/testing-tools/modules-tests/module-example/page.mdx": "2025-03-18T15:07:22.640Z",
"app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2025-03-24T06:54:21.249Z",
"app/learn/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2025-07-18T12:24:02.385Z",
"app/learn/fundamentals/module-links/custom-columns/page.mdx": "2025-03-11T13:29:54.752Z",
"app/learn/fundamentals/module-links/directions/page.mdx": "2025-03-17T12:52:06.161Z",
"app/learn/fundamentals/module-links/page.mdx": "2025-04-17T08:50:17.036Z",
Expand Down
13 changes: 1 addition & 12 deletions www/apps/book/generated/sidebar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,18 +1117,7 @@ export const generatedSidebars = [
"type": "link",
"path": "/learn/debugging-and-testing/testing-tools/modules-tests",
"title": "Modules Tests",
"children": [
{
"loaded": true,
"isPathHref": true,
"type": "link",
"path": "/learn/debugging-and-testing/testing-tools/modules-tests/module-example",
"title": "Example",
"children": [],
"chapterTitle": "7.3.1. Example",
"number": "7.3.1."
}
],
"children": [],
"chapterTitle": "7.3. Modules Tests",
"number": "7.3."
},
Expand Down
Loading
Loading