7.3. Write Tests for Modules
In this chapter, you'll learn about moduleIntegrationTestRunner from Medusa's Testing Framework and how to use it to write integration tests for a module's main service.
moduleIntegrationTestRunner Utility#
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, consider a Blog Module with a BlogModuleService that has a getMessage method:
To create an integration test for the module's service, create the file src/modules/blog/__tests__/service.spec.ts with the following content:
1import { moduleIntegrationTestRunner } from "@medusajs/test-utils"2import { BLOG_MODULE } from ".."3import BlogModuleService from "../service"4import Post from "../models/post"5 6moduleIntegrationTestRunner<BlogModuleService>({7 moduleName: BLOG_MODULE,8 moduleModels: [Post],9 resolve: "./src/modules/blog",10 testSuite: ({ service }) => {11 describe("BlogModuleService", () => {12 it("says hello world", () => {13 const message = service.getMessage()14 15 expect(message).toEqual("Hello, World!")16 })17 })18 },19})20 21jest.setTimeout(60 * 1000)
The moduleIntegrationTestRunner function accepts as a parameter an object with the following properties:
moduleName: The registration name of the module.moduleModels: An array of models in the module. Refer to this section if your module doesn't have data models.resolve: The path to the module's directory.testSuite: A function that defines Jest 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.
moduleIntegrationTestRunner function is used as the type of the service property.The tests in the testSuite function are written using Jest.
Run Tests#
Run the following command to run your module integration tests:
test:integration:modules script in package.json, refer to the Medusa Testing Tools chapter.This runs your Medusa application and runs the tests available in any __tests__ directory under the src/modules directory.
Pass Module Options#
If your module accepts options, you can set them using the moduleOptions property of the moduleIntegrationTestRunner's parameter.
For example:
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#
If your module doesn't have a data model, pass a dummy model in the moduleModels property.
For example:
1import { moduleIntegrationTestRunner } from "@medusajs/test-utils"2import BlogModuleService from "../service"3import { model } from "@medusajs/framework/utils"4 5const DummyModel = model.define("dummy_model", {6 id: model.id().primaryKey(),7})8 9moduleIntegrationTestRunner<BlogModuleService>({10 moduleModels: [DummyModel],11 // ...12})13 14jest.setTimeout(60 * 1000)
Inject Dependencies in Module Tests#
Some modules have injected dependencies, such as the Event Module's service. 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:
1import { MockEventBusService, moduleIntegrationTestRunner } from "@medusajs/test-utils"2import { BLOG_MODULE } from ".."3import BlogModuleService from "../service"4import Post from "../models/post"5import { Modules } from "@medusajs/framework/utils"6 7moduleIntegrationTestRunner<BlogModuleService>({8 moduleName: BLOG_MODULE,9 moduleModels: [Post],10 resolve: "./src/modules/blog",11 injectedDependencies: {12 [Modules.EVENT_BUS]: new MockEventBusService(),13 },14 testSuite: ({ service }) => {15 describe("BlogModuleService", () => {16 it("says hello world", async () => {17 const message = await service.getMessage()18 19 expect(message).toEqual("Hello, World!")20 })21 })22 },23})24 25jest.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 for other available parameter options and inputs of the testSuite function.
Database Used in Tests#
The moduleIntegrationTestRunner function creates a database with a random name before running the tests. Then, it drops that database after all the tests end.
To manage that database, such as changing its name or perform operations on it in your tests, refer to the Test Tooling Reference.