www/apps/book/app/learn/debugging-and-testing/testing-tools/page.mdx
export const metadata = {
title: ${pageNumber} Medusa Testing Tools,
}
In this chapter, you'll learn about Medusa's testing tools and how to install and configure them.
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:
npm install --save-dev @medusajs/test-utils@latest
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:
npm install --save-dev jest @types/jest @swc/jest
Then, create the file jest.config.js with the following content:
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:
As of Medusa v2.11.0, MikroORM dependencies are included in the @medusajs/framework package. If you're using an older version of Medusa, change the require statement to @mikro-orm/core.
const { MetadataStorage } = require("@medusajs/framework/mikro-orm/core")
MetadataStorage.clear()
Finally, add the following scripts to 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.
</Note>The next chapters explain how to use the testing tools provided by @medusajs/test-utils to write tests.