Skip to content

[TRI-1127] Improvement: Add a @trigger.dev/testing package that allows for unit testing job runs #379

@ericallam

Description

@ericallam

Currently, we don't have a clean way of unit testing jobs, it would be nice to have a @trigger.dev/testing package that could be used to do test job runs with mocked io and make assertions:

const jobToTest = client.defineJob({
  id: "stripe-example-1",
  name: "Stripe Example 1",
  version: "0.1.0",
  trigger: eventTrigger({
    name: "stripe.example",
    schema: z.object({
      customerId: z.string(),
      source: z.string(),
    }),
  }),
  integrations: {
    stripe,
  },
  run: async (payload, io, ctx) => {
    await io.stripe.createCharge("create-charge", {
      amount: 100,
      currency: "usd",
      source: payload.source,
      customer: payload.customerId,
    });
  },
});

import { testJob } from "@trigger.dev/testing";

// Inside a test file
const testRun = testJob(jobToTest, {
  payload: {
    customerId: "cus_123",
    source: "src_123",
  },
});

expect(testRun).toHaveSucceeded();
// Test the stripe charge was called with the correct params
expect(testRun.io.stripe.createCharge).toHaveBeenCalledWith("create-charge", {
  amount: 100,
  currency: "usd",
  source: "src_123",
});

TRI-1127

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions