Fix: Test package run isolation (#793)

* Fix link

* Move paths to compiler options

* Add failing test case

* Fix test isolation

* Add changeset
This commit is contained in:
nicktrn
2023-12-13 09:49:52 +00:00
committed by GitHub
parent 6d4676f204
commit fc31c6c07a
5 changed files with 76 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/testing": patch
---
Fix test isolation by restoring the original run function
+1 -1
View File
@@ -76,7 +76,7 @@ expect(testRun.output).toEqual({ bar: "baz" });
## More information
See the official [Trigger.dev Unit Testing Reference](https://github.com/triggerdotdev/trigger.dev/references/unit-testing/) for a working setup with Vitest.
See the official [Trigger.dev Unit Testing Reference](https://github.com/triggerdotdev/trigger.dev/tree/main/references/unit-testing/) for a working setup with Vitest.
## License
+16 -12
View File
@@ -192,18 +192,22 @@ export const createJobTester =
return run(payload, io, ctx);
};
const request = buildRequest("EXECUTE_JOB", client!.apiKey() ?? "", {
body: buildRequestBody(eventLog, job),
jobId: job.id,
});
const requestResult = await client!.handleRequest(request);
try {
const request = buildRequest("EXECUTE_JOB", client!.apiKey() ?? "", {
body: buildRequestBody(eventLog, job),
jobId: job.id,
});
const requestResult = await client!.handleRequest(request);
const { output, status, ...rest } = requestResult.body;
const { output, status, ...rest } = requestResult.body;
return {
output,
status,
tasks,
...rest,
};
return {
output,
status,
tasks,
...rest,
};
} finally {
job.options.run = run;
}
};
+47 -1
View File
@@ -1,7 +1,7 @@
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
import { Stripe } from "@trigger.dev/stripe";
import { toHaveSucceeded, createJobTester } from "../src";
import { expect, test, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { z } from "zod";
expect.extend({ toHaveSucceeded });
@@ -98,3 +98,49 @@ test("stripe integration", async () => {
// job run has expected output
expect(testRun.output).toEqual({ id: "charge_1234" });
});
describe("isolation test", () => {
const jobToTest = client.defineJob({
id: "isolation-test",
name: "isolation-test",
version: "0.1.0",
trigger: eventTrigger({
name: "isolation-test",
}),
run: async (payload, io, ctx) => {
const number = await io.runTask("get-number", async () => {
return 42;
});
return { number };
},
});
test("run 1", async () => {
const testRun = await testJob(jobToTest, {
tasks: {
"get-number": 1,
},
payload: 1
});
// job run was successful
expect(testRun).toHaveSucceeded();
// job run has expected output
expect(testRun.output.number).toEqual(1);
});
test("run 2", async () => {
const testRun = await testJob(jobToTest, {
tasks: {
"get-number": 2,
},
});
// job run was successful
expect(testRun).toHaveSucceeded();
// job run has expected output
expect(testRun.output.number).toEqual(2);
});
});
+7 -9
View File
@@ -4,18 +4,16 @@
"noEmit": true,
"lib": ["dom"],
"paths": {
"@/*": ["./src/*"],
"@trigger.dev/core": ["../../packages/core/src/index"],
"@trigger.dev/core/*": ["../../packages/core/src/*"],
"@trigger.dev/sdk": ["../../packages/trigger-sdk/src/index"],
"@trigger.dev/sdk/*": ["../../packages/trigger-sdk/src/*"],
"@trigger.dev/stripe": ["../../integrations/stripe/src/index"],
"@trigger.dev/stripe/*": ["../../integrations/stripe/src/*"],
"@trigger.dev/tsup/*": ["../../config-packages/tsup/src/*"],
"@trigger.dev/tsup": ["../../config-packages/tsup/src/index"]
}
},
"paths": {
"@/*": ["./src/*"],
"@trigger.dev/core": ["../../packages/core/src/index"],
"@trigger.dev/core/*": ["../../packages/core/src/*"],
"@trigger.dev/sdk": ["../../packages/trigger-sdk/src/index"],
"@trigger.dev/sdk/*": ["../../packages/trigger-sdk/src/*"],
"@trigger.dev/stripe": ["../../integrations/stripe/src/index"],
"@trigger.dev/stripe/*": ["../../integrations/stripe/src/*"]
},
"exclude": ["node_modules"]
}