Raw
1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Write Integration Tests`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn about `medusaIntegrationTestRunner` from Medusa's Testing Framework and how to use it to write integration tests.
10
11 <Prerequisites
12 items={[
13 {
14 text: "Testing Tools Setup",
15 link: "/learn/debugging-and-testing/testing-tools"
16 }
17 ]}
18 />
19
20 ## medusaIntegrationTestRunner Utility
21
22 The `medusaIntegrationTestRunner` is from Medusa's Testing Framework and it's used to create integration tests in your Medusa project. It runs a full Medusa application, allowing you test API routes, workflows, or other customizations.
23
24 For example:
25
26 export const highlights = [
27 ["4", "api", "A set of utility methods used to send requests to the Medusa application."],
28 ["4", "getContainer", "A function to retrieve the Medusa container."]
29 ]
30
31 ```ts title="integration-tests/http/test.spec.ts" highlights={highlights}
32 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
33
34 medusaIntegrationTestRunner({
35 testSuite: ({ api, getContainer }) => {
36 // TODO write tests...
37 },
38 })
39
40 jest.setTimeout(60 * 1000)
41 ```
42
43 The `medusaIntegrationTestRunner` function accepts an object as a parameter. The object has a required property `testSuite`.
44
45 `testSuite`'s value is a function that defines the tests to run. The function accepts as a parameter an object that has the following properties:
46
47 - `api`: a set of utility methods used to send requests to the Medusa application. It has the following methods:
48 - `get`: Send a `GET` request to an API route.
49 - `post`: Send a `POST` request to an API route.
50 - `delete`: Send a `DELETE` request to an API route.
51 - `getContainer`: a function that retrieves the Medusa Container. Use the `getContainer().resolve` method to resolve resources from the Medusa Container.
52
53 The tests in the `testSuite` function are written using [Jest](https://jestjs.io/).
54
55 ### Jest Timeout
56
57 Since your tests connect to the database and perform actions that require more time than the typical tests, make sure to increase the timeout in your test:
58
59 ```ts title="integration-tests/http/test.spec.ts"
60 // in your test's file
61 jest.setTimeout(60 * 1000)
62 ```
63
64 ---
65
66 ### Run Tests
67
68 Run the following command to run your tests:
69
70 ```bash npm2yarn
71 npm run test:integration:http
72 ```
73
74 <Note title="Tip">
75
76 If you don't have a `test:integration:http` script in `package.json`, refer to the [Medusa Testing Tools chapter](../page.mdx#add-test-commands).
77
78 </Note>
79
80 This runs your Medusa application and runs the tests available under the `src/integrations/http` directory.
81
82 ---
83
84 ## Other Options and Inputs
85
86 Refer to [the Test Tooling Reference](!resources!/test-tools-reference/medusaIntegrationTestRunner) for other available parameter options and inputs of the `testSuite` function.
87
88 ---
89
90 ## Database Used in Tests
91
92 <Prerequisites
93 items={[
94 {
95 text: "A PostgreSQL server is running."
96 },
97 {
98 text: "The PostgreSQL user that the tests connect with can create databases."
99 }
100 ]}
101 />
102
103 The `medusaIntegrationTestRunner` function creates a database named `medusa-{random-id}-integration-{worker-id}`, where `{worker-id}` is the ID of the Jest worker running the test file. You can set a different name with the `dbName` property. Then, it drops that database after all the tests end.
104
105 The tests connect to the PostgreSQL server using the `DB_HOST`, `DB_PORT`, `DB_USERNAME`, and `DB_PASSWORD` environment variables, which default to `localhost`, `5432`, `postgres`, and an empty password respectively.
106
107 To manage that database, such as changing its name or perform operations on it in your tests, refer to [the Test Tooling Reference](!resources!/test-tools-reference/medusaIntegrationTestRunner).
108
109 ---
110
111 ## Example Integration Tests
112
113 The next chapters provide examples of writing integration tests for API routes and workflows.