Medusa Testing Tools

In this chapter, you'll learn about Medusa's testing tools and how to install and configure them.

@medusajs/test-utils Package#

Medusa provides a Testing Framework to create integration tests for your custom API routes, modules, or other Medusa customizations.

To use the Testing Framework, install @medusajs/test-utils as a devDependency:


Install and Configure Jest#

Writing tests with @medusajs/test-utils's tools requires installing and configuring Jest in your project.

Run the following command to install the required Jest dependencies:

Then, create the file jest.config.js with the following content:

jest.config.js
1const { loadEnv } = require("@medusajs/framework/utils")2loadEnv("test", process.cwd())3
4module.exports = {5  transform: {6    "^.+\\.[jt]s$": [7      "@swc/jest",8      {9        jsc: {10          parser: { syntax: "typescript", decorators: true },11        },12      },13    ],14  },15  testEnvironment: "node",16  moduleFileExtensions: ["js", "ts", "json"],17  modulePathIgnorePatterns: ["dist/"],18  setupFiles: ["./integration-tests/setup.js"],19}20
21if (process.env.TEST_TYPE === "integration:http") {22  module.exports.testMatch = ["**/integration-tests/http/*.spec.[jt]s"]23} else if (process.env.TEST_TYPE === "integration:modules") {24  module.exports.testMatch = ["**/src/modules/*/__tests__/**/*.[jt]s"]25} else if (process.env.TEST_TYPE === "unit") {26  module.exports.testMatch = ["**/src/**/__tests__/**/*.unit.spec.[jt]s"]27}

Next, create the integration-tests/setup.js file with the following content:

integration-tests/setup.js
1const { MetadataStorage } = require("@mikro-orm/core")2
3MetadataStorage.clear()

Add Test Commands#

Finally, add the following scripts to package.json:

package.json
1"scripts": {2  // ...3  "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",4  "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",5  "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"6},

You now have two commands:

  • test:integration:http to run integration tests (for example, for API routes and workflows) available under the integration-tests/http directory.
  • test:integration:modules to run integration tests for modules available in any __tests__ directory under src/modules.
  • test:unit to run unit tests in any __tests__ directory under the src directory.
NoteMedusa's Testing Framework works for integration tests only. You can write unit tests using Jest.

Test Tools and Writing Tests#

The next chapters explain how to use the testing tools provided by @medusajs/test-utils to write tests.

Was this chapter helpful?
Edit this page
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break