Skip to content

Commit

Permalink
minor(vestjs-runtime): Add AbortController to all isolates
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 27, 2023
1 parent 5cfc7e8 commit 3007217
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 20 deletions.
2 changes: 0 additions & 2 deletions packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function IsolateTestBase() {
return {
severity: TestSeverity.Error,
status: IsolateTestStateMachine.initial(),
abortController: new AbortController(),
};
}

Expand All @@ -58,7 +57,6 @@ export type IsolateTestPayload<
severity: TestSeverity;
status: TestStatus;
asyncTest?: AsyncTest;
abortController: AbortController;
};

type CommonTestFields<
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/isolate/IsolateTest/VestTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Maybe, invariant, isPromise, optionalFunctionValue } from 'vest-utils';
import { IsolateSelectors, TIsolate } from 'vestjs-runtime';
import { IsolateMutator, IsolateSelectors, TIsolate } from 'vestjs-runtime';

import { ErrorStrings } from 'ErrorStrings';
import type { TIsolateTest } from 'IsolateTest';
Expand Down Expand Up @@ -161,7 +161,7 @@ export class VestTest {

static cancel(test: TIsolateTest): void {
VestTest.setStatus(test, TestStatus.CANCELED);
VestTest.getData(test).abortController.abort(TestStatus.CANCELED);
IsolateMutator.abort(test, TestStatus.CANCELED);
}

static omit(test: TIsolateTest): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ exports[`IsolateTest TestObject constructor 1`] = `
"$type": "UnitTest",
"children": [],
"data": {
"abortController": AbortController {},
"fieldName": "unicycle",
"message": "I am Root.",
"severity": "error",
Expand All @@ -25,7 +24,6 @@ exports[`IsolateTest testObject.warn Should mark the test as warning 1`] = `
"$type": "UnitTest",
"children": [],
"data": {
"abortController": AbortController {},
"fieldName": "unicycle",
"message": "I am Root.",
"severity": "warning",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Test Vest's \`test\` function test params creates a test without a key 1`] = `
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "field_name",
"message": "failure message",
"severity": "error",
Expand All @@ -17,6 +17,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a key
"output": undefined,
"parent": {
"$type": "Suite",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
[Circular],
Expand Down Expand Up @@ -72,10 +73,10 @@ exports[`Test Vest's \`test\` function test params creates a test without a key
exports[`Test Vest's \`test\` function test params creates a test without a message and with a key 1`] = `
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "field_name",
"severity": "error",
"testFn": [Function],
Expand All @@ -85,6 +86,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess
"output": undefined,
"parent": {
"$type": "Suite",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
[Circular],
Expand Down Expand Up @@ -142,10 +144,10 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess
exports[`Test Vest's \`test\` function test params creates a test without a message and without a key 1`] = `
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "field_name",
"severity": "error",
"testFn": [Function],
Expand All @@ -155,6 +157,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess
"output": undefined,
"parent": {
"$type": "Suite",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
[Circular],
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/__tests__/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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","abortController":{},"fieldName":"field_name","message":"failure message"},"S":"PASSING","k":"keyboardcat"}"`
`"{"$":"Test","D":{"severity":"error","fieldName":"field_name","message":"failure message"},"S":"PASSING","k":"keyboardcat"}"`
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/testLevelFlowControl/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function runSyncTest(testObject: TIsolateTest): TestResult {
return SuiteContext.run({ currentTest: testObject }, () => {
let result: TestResult;

const { message, testFn, abortController } = VestTest.getData(testObject);
const { message, testFn } = VestTest.getData(testObject);

try {
result = testFn({ signal: abortController.signal });
result = testFn({ signal: testObject.abortController.signal });
} catch (error) {
if (shouldUseErrorAsMessage(message, error)) {
VestTest.getData(testObject).message = error;
Expand Down
Original file line number Diff line number Diff line change
@@ -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},"S":"DONE"},{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_1","message":"field_1_message"},"S":"FAILED"},{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_2","message":"field_2_message"},"S":"SKIPPED"},{"children":[{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"},"S":"SKIPPED"},{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"},"S":"SKIPPED"},{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_4","groupName":"group_1","message":"field_4_message"},"S":"SKIPPED"}],"$":"Group","D":{},"S":"DONE"},{"children":[{"$":"Test","D":{"severity":"error","abortController":{},"fieldName":"field_5","message":"field_5_message"},"S":"SKIPPED"}],"$":"SkipWhen","D":{},"S":"DONE"}],"$":"Suite","D":{"optional":{}},"S":"DONE"}"`;
exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"{"children":[{"$":"Focused","D":{"focusMode":0,"match":["field_1"],"matchAll":false},"S":"DONE"},{"$":"Test","D":{"severity":"error","fieldName":"field_1","message":"field_1_message"},"S":"FAILED"},{"$":"Test","D":{"severity":"error","fieldName":"field_2","message":"field_2_message"},"S":"SKIPPED"},{"children":[{"$":"Test","D":{"severity":"error","fieldName":"field_3","groupName":"group_1","message":"field_3_message_1"},"S":"SKIPPED"},{"$":"Test","D":{"severity":"error","fieldName":"field_3","groupName":"group_1","message":"field_3_message_2"},"S":"SKIPPED"},{"$":"Test","D":{"severity":"error","fieldName":"field_4","groupName":"group_1","message":"field_4_message"},"S":"SKIPPED"}],"$":"Group","D":{},"S":"DONE"},{"children":[{"$":"Test","D":{"severity":"error","fieldName":"field_5","message":"field_5_message"},"S":"SKIPPED"}],"$":"SkipWhen","D":{},"S":"DONE"}],"$":"Suite","D":{"optional":{}},"S":"DONE"}"`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
exports[`staticSuite dump should output a dump of the suite 1`] = `
{
"$type": "Suite",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "t1",
"severity": "error",
"testFn": [Function],
Expand All @@ -23,10 +24,10 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
},
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "t2",
"severity": "error",
"testFn": [Function],
Expand All @@ -39,14 +40,15 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
},
{
"$type": "Group",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "t1",
"groupName": "g1",
"severity": "error",
Expand All @@ -60,10 +62,10 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
},
{
"$type": "Test",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {},
"fieldName": "t3",
"groupName": "g1",
"severity": "error",
Expand Down
2 changes: 2 additions & 0 deletions packages/vestjs-runtime/src/Isolate/Isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TIsolate<P extends IsolatePayload = IsolatePayload> = {
[IsolateKeys.Keys]: Nullable<Record<string, TIsolate>>;
[IsolateKeys.Data]: DataOnly<P>;
[IsolateKeys.Status]?: string;
[IsolateKeys.AbortController]: AbortController;
children: Nullable<TIsolate[]>;
key: IsolateKey;
output: any;
Expand Down Expand Up @@ -118,6 +119,7 @@ function baseIsolate(
const { allowReorder, status, ...data } = payload ?? {};
return {
[IsolateKeys.AllowReorder]: allowReorder,
[IsolateKeys.AbortController]: new AbortController(),
[IsolateKeys.Keys]: null,
[IsolateKeys.Parent]: null,
[IsolateKeys.Type]: type,
Expand Down
7 changes: 7 additions & 0 deletions packages/vestjs-runtime/src/Isolate/IsolateKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum IsolateKeys {
Data = 'data',
AllowReorder = 'allowReorder',
Status = 'status',
AbortController = 'abortController',
}

enum MinifiedKeys {
Expand Down Expand Up @@ -42,3 +43,9 @@ export const MinifiedToKey = Object.entries(KeyToMinified).reduce(
}),
{} as Record<string, IsolateKeys>
);

export const ExcludedFromDump = [
IsolateKeys.AbortController,
IsolateKeys.Parent,
IsolateKeys.Keys,
];
7 changes: 7 additions & 0 deletions packages/vestjs-runtime/src/Isolate/IsolateMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ export class IsolateMutator {
static setData(isolate: TIsolate, data: any): void {
isolate.data = data;
}

static abort(isolate: TIsolate, reason?: string): void {
if (isNullish(isolate.abortController)) {
return;
}
isolate.abortController.abort(reason);
}
}
21 changes: 20 additions & 1 deletion packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CB } from 'vest-utils';

import { TIsolate, Isolate } from 'Isolate';
import { StateRefType, useAvailableRoot } from 'VestRuntime';
import { VestRuntime } from 'vestjs-runtime';
import { IsolateMutator, VestRuntime } from 'vestjs-runtime';

enum IsolateType {
Isolate = 'Isolate',
Expand Down Expand Up @@ -95,6 +95,25 @@ describe('Isolate', () => {
});
});

describe('AbortController', () => {
it('Should create an AbortController', () => {
const isolate = withRunTime(() => {
return Isolate.create(IsolateType.Isolate, () => {});
});
expect(isolate.abortController).toBeDefined();
expect(isolate.abortController).toBeInstanceOf(AbortController);
});

it('Should abort the controller with the passed reason', () => {
const isolate = withRunTime(() => {
return Isolate.create(IsolateType.Isolate, () => {});
});
const spy = jest.spyOn(isolate.abortController, 'abort');
IsolateMutator.abort(isolate, 'foo');
expect(spy).toHaveBeenCalledWith('foo');
});
});

function withRunTime<T>(fn: CB<T>) {
return VestRuntime.Run(stateRef, () => {
return fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,36 @@ describe('IsolateMutator', () => {
});
});
});

describe('abort', () => {
it('Should abort the controller', () => {
const isolate = {
abortController: { abort: jest.fn() },
} as unknown as TIsolate;

expect(isolate.abortController.abort).not.toHaveBeenCalled();
IsolateMutator.abort(isolate);
expect(isolate.abortController.abort).toHaveBeenCalled();
});

describe('When the controller is nullish', () => {
it('Should no-op', () => {
const isolate = { abortController: null } as unknown as TIsolate;

expect(isolate.abortController).toBeNull();
IsolateMutator.abort(isolate);
expect(isolate.abortController).toBeNull();
});
});

it('Should abort the controller with the passed reason', () => {
const isolate = {
abortController: { abort: jest.fn() },
} as unknown as TIsolate;

expect(isolate.abortController.abort).not.toHaveBeenCalled();
IsolateMutator.abort(isolate, 'foo');
expect(isolate.abortController.abort).toHaveBeenCalledWith('foo');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
{
"$type": "URoot",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
{
"$type": "UChild_1",
"abortController": AbortController {},
"allowReorder": undefined,
"children": [
{
"$type": "UGrandChild_1",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {},
Expand All @@ -21,6 +24,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
},
{
"$type": "UGrandChild_2",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {},
Expand All @@ -31,6 +35,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
},
{
"$type": "UGrandChild_3",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('AsyncIsolate', () => {
expect(root).toMatchInlineSnapshot(`
{
"$type": "URoot",
"abortController": AbortController {},
"allowReorder": undefined,
"children": null,
"data": {},
Expand Down
9 changes: 7 additions & 2 deletions packages/vestjs-runtime/src/exports/IsolateSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
} from 'vest-utils';

import { TIsolate } from 'Isolate';
import { IsolateKeys, KeyToMinified, MinifiedToKey } from 'IsolateKeys';
import {
ExcludedFromDump,
IsolateKeys,
KeyToMinified,
MinifiedToKey,
} from 'IsolateKeys';
import { IsolateMutator } from 'IsolateMutator';

export class IsolateSerializer {
Expand Down Expand Up @@ -123,5 +128,5 @@ function transformIsolate(isolate: TIsolate): Record<string, any> {
}

function isKeyExcluededFromDump(key: string): boolean {
return [IsolateKeys.Parent, IsolateKeys.Keys].includes(key as IsolateKeys);
return ExcludedFromDump.includes(key as IsolateKeys);
}

2 comments on commit 3007217

@vercel
Copy link

@vercel vercel bot commented on 3007217 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app
vest-next-git-latest-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3007217 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest.vercel.app
vest-ealush.vercel.app
vestjs.dev
vest-git-latest-ealush.vercel.app
www.vestjs.dev

Please sign in to comment.