Documentation

7.3.1. Example: Integration Tests for a Module

In this chapter, find an example of writing an integration test for a module using the moduleIntegrationTestRunner utility function.

Write Integration Test for Module#

Consider a hello module with a HelloModuleService that has a getMessage method:

src/modules/hello/service.ts
1import { MedusaService } from "@medusajs/utils"2import MyCustom from "./models/my-custom"3
4class HelloModuleService extends MedusaService({5  MyCustom,6}){7  getMessage(): string {8    return "Hello, World!"9  }10}11
12export default HelloModuleService

To create an integration test for the method, create the file src/modules/hello/__tests__/service.spec.ts with the following content:

src/modules/hello/__tests__/service.spec.ts
1import { moduleIntegrationTestRunner } from "medusa-test-utils"2import { HELLO_MODULE } from ".."3import HelloModuleService from "../service"4import MyCustom from "../models/my-custom"5
6moduleIntegrationTestRunner<HelloModuleService>({7  moduleName: HELLO_MODULE,8  moduleModels: [MyCustom],9  resolve: "./modules/hello",10  testSuite: ({ service }) => {11    describe("HelloModuleService", () => {12      it("says hello world", () => {13        const message = service.getMessage()14
15        expect(message).toEqual("Hello, World!")16      })17    })18  },19})

You use the moduleIntegrationTestRunner function to add tests for the hello module. You have one test that passes if the getMessage method returns the "Hello, World!" string.


Run Test#

Run the following command to run your module integration tests:

TipIf you don't have a test: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.

Was this chapter helpful?
Edit this page