- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
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
:
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:
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:
Run Test#
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 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:
You can write the following test to ensure that the workflow throws that error:
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
.