diff --git a/packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts b/packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts index 84fea2062..afb4fa2ab 100644 --- a/packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts +++ b/packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts @@ -47,6 +47,7 @@ export function IsolateTestBase() { return { severity: TestSeverity.Error, status: TestStatus.UNTESTED, + abortController: new AbortController(), }; } @@ -57,6 +58,7 @@ export type IsolateTestPayload< severity: TestSeverity; status: TestStatus; asyncTest?: AsyncTest; + abortController: AbortController; }; type CommonTestFields< diff --git a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts index a87725d86..0dbae7305 100644 --- a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts +++ b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts @@ -142,6 +142,7 @@ export class VestTest { static cancel(test: TIsolateTest): void { VestTest.setStatus(test, TestStatus.CANCELED); + VestTest.getData(test).abortController.abort(); } static omit(test: TIsolateTest): void { diff --git a/packages/vest/src/core/test/TestTypes.ts b/packages/vest/src/core/test/TestTypes.ts index becacb5f4..3c9c4c197 100644 --- a/packages/vest/src/core/test/TestTypes.ts +++ b/packages/vest/src/core/test/TestTypes.ts @@ -2,7 +2,7 @@ import { Maybe } from 'vest-utils'; import { TFieldName } from 'SuiteResultTypes'; -export type TestFn = () => TestResult; +export type TestFn = (abortSignal?: AbortSignal) => TestResult; export type AsyncTest = Promise; export type TestResult = Maybe; diff --git a/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap b/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap index f6af009a1..908366a58 100644 --- a/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap +++ b/packages/vest/src/core/test/__tests__/__snapshots__/IsolateTest.test.ts.snap @@ -5,6 +5,15 @@ exports[`IsolateTest TestObject constructor 1`] = ` "$type": "UnitTest", "children": [], "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "unicycle", "message": "I am Root.", "severity": "error", @@ -24,6 +33,15 @@ exports[`IsolateTest testObject.warn Should mark the test as warning 1`] = ` "$type": "UnitTest", "children": [], "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "unicycle", "message": "I am Root.", "severity": "warning", diff --git a/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap b/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap index 9fda6e6da..07db22bb4 100644 --- a/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap +++ b/packages/vest/src/core/test/__tests__/__snapshots__/test.test.ts.snap @@ -6,6 +6,15 @@ exports[`Test Vest's \`test\` function test params creates a test without a key "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "field_name", "message": "failure message", "severity": "error", @@ -73,6 +82,15 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "field_name", "severity": "error", "status": "PASSING", @@ -141,6 +159,15 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "field_name", "severity": "error", "status": "PASSING", diff --git a/packages/vest/src/core/test/__tests__/test.test.ts b/packages/vest/src/core/test/__tests__/test.test.ts index e75401367..a60229813 100644 --- a/packages/vest/src/core/test/__tests__/test.test.ts +++ b/packages/vest/src/core/test/__tests__/test.test.ts @@ -193,7 +193,7 @@ describe("Test Vest's `test` function", () => { expect(testObject.key).toBe('keyboardcat'); expect(testObject.data.message).toBe('failure message'); expect(IsolateSerializer.serialize(testObject)).toMatchInlineSnapshot( - `"{"$":"Test","D":{"severity":"error","status":"PASSING","fieldName":"field_name","message":"failure message"},"k":"keyboardcat"}"` + `"{"$":"Test","D":{"severity":"error","status":"PASSING","abortController":{},"fieldName":"field_name","message":"failure message"},"k":"keyboardcat"}"` ); }); diff --git a/packages/vest/src/core/test/testLevelFlowControl/runTest.ts b/packages/vest/src/core/test/testLevelFlowControl/runTest.ts index f9ea2870c..9a1f450e8 100644 --- a/packages/vest/src/core/test/testLevelFlowControl/runTest.ts +++ b/packages/vest/src/core/test/testLevelFlowControl/runTest.ts @@ -40,10 +40,10 @@ function runSyncTest(testObject: TIsolateTest): TestResult { return SuiteContext.run({ currentTest: testObject }, () => { let result: TestResult; - const { message, testFn } = VestTest.getData(testObject); + const { message, testFn, abortController } = VestTest.getData(testObject); try { - result = testFn(); + result = testFn(abortController.signal); } catch (error) { if (shouldUseErrorAsMessage(message, error)) { VestTest.getData(testObject).message = error; diff --git a/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap b/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap index 7b418ded9..fe12a2c5b 100644 --- a/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap +++ b/packages/vest/src/exports/__tests__/__snapshots__/SuiteSerializer.test.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"{"children":[{"$":"Focused","D":{"focusMode":0,"match":["field_1"],"matchAll":false}},{"$":"Test","D":{"severity":"error","status":"FAILED","fieldName":"field_1","message":"field_1_message"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","fieldName":"field_2","message":"field_2_message"}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","fieldName":"field_4","groupName":"group_1","message":"field_4_message"}}],"$":"Group","D":{}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","fieldName":"field_5","message":"field_5_message"}}],"$":"SkipWhen","D":{}}],"$":"Suite","D":{"optional":{}}}"`; +exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"{"children":[{"$":"Focused","D":{"focusMode":0,"match":["field_1"],"matchAll":false}},{"$":"Test","D":{"severity":"error","status":"FAILED","abortController":{},"fieldName":"field_1","message":"field_1_message"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_2","message":"field_2_message"}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"}},{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_4","groupName":"group_1","message":"field_4_message"}}],"$":"Group","D":{}},{"children":[{"$":"Test","D":{"severity":"error","status":"SKIPPED","abortController":{},"fieldName":"field_5","message":"field_5_message"}}],"$":"SkipWhen","D":{}}],"$":"Suite","D":{"optional":{}}}"`; diff --git a/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap b/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap index 5e2ada75e..c81bc15b1 100644 --- a/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap +++ b/packages/vest/src/suite/__tests__/__snapshots__/staticSuite.test.ts.snap @@ -10,6 +10,15 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "t1", "severity": "error", "status": "FAILED", @@ -25,6 +34,15 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "t2", "severity": "error", "status": "FAILED", @@ -44,6 +62,15 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "t1", "groupName": "g1", "severity": "error", @@ -60,6 +87,15 @@ exports[`staticSuite dump should output a dump of the suite 1`] = ` "allowReorder": undefined, "children": null, "data": { + "abortController": AbortController { + Symbol(signal): AbortSignal { + Symbol(kEvents): Map {}, + Symbol(events.maxEventTargetListeners): 10, + Symbol(events.maxEventTargetListenersWarned): false, + Symbol(kAborted): false, + Symbol(kReason): undefined, + }, + }, "fieldName": "t3", "groupName": "g1", "severity": "error",