Raw
1 export const metadata = {
2 title: `${pageNumber} Medusa Testing Tools`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you'll learn about Medusa's testing tools and how to install and configure them.
8
9 ## @medusajs/test-utils Package
10
11 Medusa provides a Testing Framework to create integration tests for your custom API routes, modules, or other Medusa customizations.
12
13 To use the Testing Framework, install `@medusajs/test-utils` as a `devDependency`:
14
15 ```bash npm2yarn
16 npm install --save-dev @medusajs/test-utils@latest
17 ```
18
19 ---
20
21 ## Install and Configure Jest
22
23 Writing tests with `@medusajs/test-utils`'s tools requires installing and configuring Jest in your project.
24
25 Run the following command to install the required Jest dependencies:
26
27 ```bash npm2yarn
28 npm install --save-dev jest @types/jest @swc/jest
29 ```
30
31 Then, create the file `jest.config.js` with the following content:
32
33 ```js title="jest.config.js"
34 const { loadEnv } = require("@medusajs/framework/utils")
35 loadEnv("test", process.cwd())
36
37 module.exports = {
38 transform: {
39 "^.+\\.[jt]s$": [
40 "@swc/jest",
41 {
42 jsc: {
43 parser: { syntax: "typescript", decorators: true },
44 target: "es2021",
45 },
46 },
47 ],
48 },
49 testEnvironment: "node",
50 moduleFileExtensions: ["js", "ts", "json"],
51 modulePathIgnorePatterns: ["dist/"],
52 setupFiles: ["./integration-tests/setup.js"],
53 }
54
55 if (process.env.TEST_TYPE === "integration:http") {
56 module.exports.testMatch = ["**/integration-tests/http/*.spec.[jt]s"]
57 } else if (process.env.TEST_TYPE === "integration:modules") {
58 module.exports.testMatch = ["**/src/modules/*/__tests__/**/*.[jt]s"]
59 } else if (process.env.TEST_TYPE === "unit") {
60 module.exports.testMatch = ["**/src/**/__tests__/**/*.unit.spec.[jt]s"]
61 }
62 ```
63
64 Next, create the `integration-tests/setup.js` file with the following content:
65
66 <Note>
67
68 As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/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`.
69
70 </Note>
71
72 ```js title="integration-tests/setup.js"
73 const { MetadataStorage } = require("@medusajs/framework/mikro-orm/core")
74
75 MetadataStorage.clear()
76 ```
77
78 ---
79
80 ## Add Test Commands
81
82 Finally, add the following scripts to `package.json`:
83
84 ```json title="package.json"
85 "scripts": {
86 // ...
87 "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
88 "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
89 "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
90 },
91 ```
92
93 You now have two commands:
94
95 - `test:integration:http` to run integration tests (for example, for API routes and workflows) available under the `integration-tests/http` directory.
96 - `test:integration:modules` to run integration tests for modules available in any `__tests__` directory under `src/modules`.
97 - `test:unit` to run unit tests in any `__tests__` directory under the `src` directory.
98
99 <Note>
100
101 Medusa's Testing Framework works for integration tests only. You can write unit tests using Jest.
102
103 </Note>
104
105 ---
106
107 ## Test Tools and Writing Tests
108
109 The next chapters explain how to use the testing tools provided by `@medusajs/test-utils` to write tests.