export const metadata = { title: `${pageNumber} Medusa Testing Tools`, } # {metadata.title} 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`: ```bash npm2yarn npm install --save-dev @medusajs/test-utils@latest ``` --- ## 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: ```bash npm2yarn npm install --save-dev jest @types/jest @swc/jest ``` Then, create the file `jest.config.js` with the following content: ```js title="jest.config.js" const { loadEnv } = require("@medusajs/framework/utils") loadEnv("test", process.cwd()) module.exports = { transform: { "^.+\\.[jt]s$": [ "@swc/jest", { jsc: { parser: { syntax: "typescript", decorators: true }, target: "es2021", }, }, ], }, testEnvironment: "node", moduleFileExtensions: ["js", "ts", "json"], modulePathIgnorePatterns: ["dist/"], setupFiles: ["./integration-tests/setup.js"], } if (process.env.TEST_TYPE === "integration:http") { module.exports.testMatch = ["**/integration-tests/http/*.spec.[jt]s"] } else if (process.env.TEST_TYPE === "integration:modules") { module.exports.testMatch = ["**/src/modules/*/__tests__/**/*.[jt]s"] } else if (process.env.TEST_TYPE === "unit") { module.exports.testMatch = ["**/src/**/__tests__/**/*.unit.spec.[jt]s"] } ``` Next, create the `integration-tests/setup.js` file with the following content: ```js title="integration-tests/setup.js" const { MetadataStorage } = require("@mikro-orm/core") MetadataStorage.clear() ``` --- ## Add Test Commands Finally, add the following scripts to `package.json`: ```json title="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=false --runInBand --forceExit", "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit" }, ``` 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. Medusa'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.