Example: Write Integration Tests for Workflows

In this chapter, you'll learn how to write integration tests for workflows using medusaIntegrationTestRunner from Medusa's Testing Framwork.

Write Integration Test for Workflow#

Consider you have the following workflow defined at src/workflows/hello-world.ts:

src/workflows/hello-world.ts
1import {2  createWorkflow,3  createStep,4  StepResponse,5  WorkflowResponse,6} from "@medusajs/framework/workflows-sdk"7
8const step1 = createStep("step-1", () => {9  return new StepResponse("Hello, World!")10})11
12export const helloWorldWorkflow = createWorkflow(13  "hello-world-workflow",14  () => {15    const message = step1()16
17    return new WorkflowResponse(message)18  }19)

To write a test for this workflow, create the file integration-tests/http/workflow.spec.ts with the following content:

integration-tests/http/workflow.spec.ts
1import { medusaIntegrationTestRunner } from "@medusajs/test-utils"2import { helloWorldWorkflow } from "../../src/workflows/hello-world"3
4medusaIntegrationTestRunner({5  testSuite: ({ getContainer }) => {6    describe("Test hello-world workflow", () => {7      it("returns message", async () => {8        const { result } = await helloWorldWorkflow(getContainer())9          .run()10
11        expect(result).toEqual("Hello, World!")12      })13    })14  },15})16
17jest.setTimeout(60 * 1000)

You use the medusaIntegrationTestRunner to write an integration test for the workflow. The test pases if the workflow returns the string "Hello, World!".

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:

integration-tests/http/custom-routes.spec.ts
1// in your test's file2jest.setTimeout(60 * 1000)

Run Test#

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 integrations/http directory.


Test That a Workflow Throws an Error#

You might want to test that a workflow throws an error in certain cases. To test this:

  • Disable the throwOnError option when executing the workflow.
  • Use the returned errors property to check what errors were thrown.

For example, if you have a step that throws this error:

src/workflows/hello-world.ts
1import { MedusaError } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep("step-1", () => {5  throw new MedusaError(MedusaError.Types.NOT_FOUND, "Item doesn't exist")6})

You can write the following test to ensure that the workflow throws that error:

integration-tests/http/workflow.spec.ts
1import { medusaIntegrationTestRunner } from "@medusajs/test-utils"2import { helloWorldWorkflow } from "../../src/workflows/hello-world"3
4medusaIntegrationTestRunner({5  testSuite: ({ getContainer }) => {6    describe("Test hello-world workflow", () => {7      it("returns message", async () => {8        const { errors } = await helloWorldWorkflow(getContainer())9          .run({10            throwOnError: false11          })12
13        expect(errors.length).toBeGreaterThan(0)14        expect(errors[0].error.message).toBe("Item doesn't exist")15      })16    })17  },18})19
20jest.setTimeout(60 * 1000)

The errors property contains an array of errors thrown during the execution of the workflow. Each error item has an error object, being the error thrown.

If you threw a MedusaError, then you can check the error message in errors[0].error.message.

Was this chapter helpful?
Edit this page
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break