|
| 1 | +# @trigger.dev/test |
| 2 | + |
| 3 | +Testing utilities for trigger.dev tasks. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install --save-dev @trigger.dev/test |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Creating a mock task |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { mockTask } from "@trigger.dev/test"; |
| 17 | + |
| 18 | +const task = mockTask({ |
| 19 | + id: "test-task", |
| 20 | + output: { success: true }, |
| 21 | +}); |
| 22 | + |
| 23 | +// Use the mock task in your tests |
| 24 | +const result = await task.triggerAndWait({}).unwrap(); |
| 25 | +console.log(result); // { success: true } |
| 26 | +``` |
| 27 | + |
| 28 | +### Unit testing a task with mocked dependencies |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { mockTask, testTaskFunction } from "@trigger.dev/test"; |
| 32 | + |
| 33 | +// Create a mock for a dependent task |
| 34 | +const dependentTask = mockTask({ |
| 35 | + id: "dependent-task", |
| 36 | + output: { result: "mocked-result" }, |
| 37 | +}); |
| 38 | + |
| 39 | +// Test a task that uses the dependent task |
| 40 | +const result = await testTaskFunction(mainTask, { input: "test" }, {}, { |
| 41 | + mockDependencies: [dependentTask] |
| 42 | +}); |
| 43 | + |
| 44 | +// Verify the result |
| 45 | +expect(result).toEqual({ |
| 46 | + mainResult: "Processed: mocked-result", |
| 47 | + input: "test" |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +### Setting up a test environment with mocked tasks |
| 52 | + |
| 53 | +```typescript |
| 54 | +import { setupMockTaskEnvironment, mockTask } from "@trigger.dev/test"; |
| 55 | + |
| 56 | +// Set up the test environment |
| 57 | +const cleanup = setupMockTaskEnvironment((registry) => { |
| 58 | + // Register mock tasks |
| 59 | + registry.registerMockTask(mockTask({ |
| 60 | + id: "task-1", |
| 61 | + output: { result: "mocked-result-1" }, |
| 62 | + })); |
| 63 | + |
| 64 | + registry.registerMockTask(mockTask({ |
| 65 | + id: "task-2", |
| 66 | + output: { result: "mocked-result-2" }, |
| 67 | + })); |
| 68 | +}); |
| 69 | + |
| 70 | +// Run your tests... |
| 71 | + |
| 72 | +// Clean up |
| 73 | +cleanup(); |
| 74 | +``` |
| 75 | + |
| 76 | +### Verifying task triggers |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { mockTask, verifyTaskTriggered, getTaskTriggerCount } from "@trigger.dev/test"; |
| 80 | + |
| 81 | +const task = mockTask({ |
| 82 | + id: "test-task", |
| 83 | + output: { success: true }, |
| 84 | +}); |
| 85 | + |
| 86 | +// Trigger the task |
| 87 | +await task.trigger({ data: "test" }); |
| 88 | + |
| 89 | +// Verify the task was triggered |
| 90 | +console.log(verifyTaskTriggered(task)); // true |
| 91 | +console.log(verifyTaskTriggered(task, { data: "test" })); // true |
| 92 | +console.log(getTaskTriggerCount(task)); // 1 |
| 93 | +``` |
| 94 | + |
| 95 | +### Testing hooks |
| 96 | + |
| 97 | +```typescript |
| 98 | +import { mockTask, createMockHooks, verifyHookCalled } from "@trigger.dev/test"; |
| 99 | + |
| 100 | +const task = mockTask({ |
| 101 | + id: "test-task", |
| 102 | + output: { success: true }, |
| 103 | +}); |
| 104 | + |
| 105 | +const hooks = createMockHooks(task); |
| 106 | + |
| 107 | +// Call the hooks |
| 108 | +await hooks.onStart({ data: "test" }, {} as any); |
| 109 | +await hooks.onSuccess({ data: "test" }, { success: true }); |
| 110 | + |
| 111 | +// Verify the hooks were called |
| 112 | +console.log(verifyHookCalled(task, "onStartCalled")); // true |
| 113 | +console.log(verifyHookCalled(task, "onSuccessCalled")); // true |
| 114 | +``` |
| 115 | + |
| 116 | +## API Reference |
| 117 | + |
| 118 | +### Task Mocking |
| 119 | + |
| 120 | +- `mockTask(options)`: Creates a mock task that can be used for testing. |
| 121 | +- `isMockTask(task)`: Checks if a task is a mock task. |
| 122 | +- `createMockTaskForUnitTest(options)`: Creates a mock task with a custom run function. |
| 123 | +- `mockTaskDependencies(taskToTest, dependencies)`: Mocks dependencies for a specific task. |
| 124 | + |
| 125 | +### Task Testing |
| 126 | + |
| 127 | +- `executeMockTask(task, payload, options)`: Executes a task with mocked dependencies. |
| 128 | +- `testTaskFunction(task, payload, params, options)`: Tests a task's run function directly. |
| 129 | +- `setupMockTaskEnvironment(setupFn)`: Sets up a test environment with mocked tasks. |
| 130 | + |
| 131 | +### Trigger Verification |
| 132 | + |
| 133 | +- `verifyTaskTriggered(task, expectedPayload)`: Verifies that a task was triggered with the expected payload. |
| 134 | +- `getTaskTriggerCount(task)`: Gets the number of times a task was triggered. |
| 135 | +- `clearAllMockTriggers()`: Clears all mock triggers. |
| 136 | + |
| 137 | +### Hook Testing |
| 138 | + |
| 139 | +- `createMockHooks(task)`: Creates mock hooks for a task. |
| 140 | +- `verifyHookCalled(task, hookType)`: Verifies that a specific hook was called for a task. |
0 commit comments