7.2. Write Integration Tests

In this chapter, you'll learn about the medusaIntegrationTestRunner utility function used to write integration tests.

medusaIntegrationTestRunner Utility#

The medusaIntegrationTestRunner utility function is provided by the @medusajs/test-utils package 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:

integration-tests/http/test.spec.ts
1import { medusaIntegrationTestRunner } from "@medusajs/test-utils"2
3medusaIntegrationTestRunner({4  testSuite: ({ api, getContainer }) => {5    // TODO write tests...6  },7})

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 a GET request to an API route.
    • post: Send a POST request to an API route.
    • delete: Send a DELETE request to an API route.
  • getContainer: a function that retrieves the Medusa Container. Use the getContainer().resolve method to resolve resources from the Medusa Container.

The tests in the testSuite function are written using Jest.


Run Tests#

Run the following command to run your tests:

TipIf you don't have a 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 this reference in the Development Resources documentation 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 references in the Development Resources documentation.


Example Integration Tests#

The next chapters provide examples of writing integration tests for API routes and workflows.

Was this chapter helpful?
Edit this page