7.1. 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:
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 target: "es2021",12 },13 },14 ],15 },16 testEnvironment: "node",17 moduleFileExtensions: ["js", "ts", "json"],18 modulePathIgnorePatterns: ["dist/"],19 setupFiles: ["./integration-tests/setup.js"],20}21 22if (process.env.TEST_TYPE === "integration:http") {23 module.exports.testMatch = ["**/integration-tests/http/*.spec.[jt]s"]24} else if (process.env.TEST_TYPE === "integration:modules") {25 module.exports.testMatch = ["**/src/modules/*/__tests__/**/*.[jt]s"]26} else if (process.env.TEST_TYPE === "unit") {27 module.exports.testMatch = ["**/src/**/__tests__/**/*.unit.spec.[jt]s"]28}
Next, create the integration-tests/setup.js
file with the following content:
Add Test Commands#
Finally, add the following scripts to 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=false --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 theintegration-tests/http
directory.test:integration:modules
to run integration tests for modules available in any__tests__
directory undersrc/modules
.test:unit
to run unit tests in any__tests__
directory under thesrc
directory.
Test Tools and Writing Tests#
The next chapters explain how to use the testing tools provided by @medusajs/test-utils
to write tests.