7.2. Write Integration Tests
In this chapter, you'll learn about medusaIntegrationTestRunner from Medusa's Testing Framework and how to use it to write integration tests.
medusaIntegrationTestRunner Utility#
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.
For example:
The medusaIntegrationTestRunner function accepts an object as a parameter. The object has a required property testSuite.
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:
api: a set of utility methods used to send requests to the Medusa application. It has the following methods:get: Send aGETrequest to an API route.post: Send aPOSTrequest to an API route.delete: Send aDELETErequest to an API route.
getContainer: a function that retrieves the Medusa Container. Use thegetContainer().resolvemethod to resolve resources from the Medusa Container.
The tests in the testSuite function are written using Jest.
Jest Timeout#
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:
Run Tests#
Run the following command to run your tests:
test:integration script in package.json, refer to the Medusa Testing Tools chapter.This runs your Medusa application and runs the tests available under the src/integrations/http directory.
Other Options and Inputs#
Refer to the Test Tooling Reference for other available parameter options and inputs of the testSuite function.
Database Used in Tests#
The medusaIntegrationTestRunner function creates a database with a random name before running the tests. Then, it drops that database after all the tests end.
To manage that database, such as changing its name or perform operations on it in your tests, refer to the Test Tooling Reference.
Example Integration Tests#
The next chapters provide examples of writing integration tests for API routes and workflows.