6.2.2. Example: Write Integration Tests for Workflows

In this chapter, you'll learn how to write integration tests for workflows using the medusaIntegrationTestRunner utility function.

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.

Was this chapter helpful?
Edit this page