From 608822813d2b003e51a2e2adcedc1f21ec13a1cb Mon Sep 17 00:00:00 2001 From: Evyatar Date: Sun, 24 Sep 2023 20:51:50 +0200 Subject: [PATCH] Add abort reason and update docs --- .../vest/src/core/isolate/IsolateTest/VestTest.ts | 2 +- ...FunctionPayload.ts => testFunctionPayload.test.ts} | 11 +++++++++++ website/docs/writing_tests/async_tests.md | 10 ++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) rename packages/vest/src/core/test/__tests__/{testFunctionPayload.ts => testFunctionPayload.test.ts} (86%) diff --git a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts index 0dbae7305..ee65bac1e 100644 --- a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts +++ b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts @@ -142,7 +142,7 @@ export class VestTest { static cancel(test: TIsolateTest): void { VestTest.setStatus(test, TestStatus.CANCELED); - VestTest.getData(test).abortController.abort(); + VestTest.getData(test).abortController.abort(TestStatus.CANCELED); } static omit(test: TIsolateTest): void { diff --git a/packages/vest/src/core/test/__tests__/testFunctionPayload.ts b/packages/vest/src/core/test/__tests__/testFunctionPayload.test.ts similarity index 86% rename from packages/vest/src/core/test/__tests__/testFunctionPayload.ts rename to packages/vest/src/core/test/__tests__/testFunctionPayload.test.ts index 14ad168d1..e34248f8a 100644 --- a/packages/vest/src/core/test/__tests__/testFunctionPayload.ts +++ b/packages/vest/src/core/test/__tests__/testFunctionPayload.test.ts @@ -39,6 +39,17 @@ describe('Test Function Payload', () => { await expect(callPayload(testFn).signal.aborted).toBe(true); await expect(callPayload(testFn, 1, 0).signal.aborted).toBe(false); }); + + it('Should set the reason to `canceled`', async () => { + const testFn = jest.fn().mockResolvedValue(undefined); + const suite = vest.create(() => { + vest.test('field_1', testFn); + }); + suite(); + suite(); + + await expect(callPayload(testFn).signal.reason).toBe('CANCELED'); + }); }); describe('Multiple async tests', () => { diff --git a/website/docs/writing_tests/async_tests.md b/website/docs/writing_tests/async_tests.md index 4a16e745e..527c2e268 100644 --- a/website/docs/writing_tests/async_tests.md +++ b/website/docs/writing_tests/async_tests.md @@ -25,16 +25,18 @@ test('name', 'Already Taken', async () => { > Since 5.1.0 -Each Vest test is passed as an argument an `AbortSignal` object. Vest internally sets the AbortSignal `aborted` property to true when the test is canceled. +Each test function is passed an object with a `signal` property. This signal is an [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) which can be used to terminate your async operations once a test is canceled. -A test is canceled when running the same test again before its previous run has completed. +The AbortSignal has a boolean `aborted` property, by which you can determine whether the test was canceled or not. + +A test gets canceled when running the same test again before its previous run has completed. You can use the AbortSignal to stop the execution of your async test, or pass it to your fetch request. -[Read more on AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal). +[More on AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal). ```js -test('name', 'Already Taken', async signal => { +test('name', 'Already Taken', async ({ signal }) => { // ... }); ```