Skip to content

Commit

Permalink
patch(vest): replace Cursor with IsolateCursor (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 13, 2022
1 parent d745cf2 commit 49096b1
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 127 deletions.
53 changes: 0 additions & 53 deletions packages/shared/src/cursor.ts

This file was deleted.

19 changes: 4 additions & 15 deletions packages/vest/src/core/ctx/ctx.ts
@@ -1,13 +1,13 @@
import assign from 'assign';
import { createContext } from 'context';
import { createCursor, Cursor } from 'cursor';
import { CB } from 'utilityTypes';

import { IsolateKeys, IsolateTypes } from 'IsolateTypes';
import { Isolate, IsolateTypes } from 'IsolateTypes';
import { Modes } from 'Modes';
import VestTest from 'VestTest';
import type { StateRef } from 'createStateRef';
import { SuiteSummary } from 'genTestsSummary';
import { generateIsolate } from 'isolate';

export default createContext<CTXType>((ctxRef, parentContext) =>
parentContext
Expand All @@ -20,26 +20,15 @@ export default createContext<CTXType>((ctxRef, parentContext) =>
groups: {},
},
inclusion: {},
isolate: {
type: IsolateTypes.DEFAULT,
keys: {
current: {},
prev: {},
},
},
isolate: generateIsolate(IsolateTypes.DEFAULT),
mode: [Modes.ALL],
testCursor: createCursor(),
},
ctxRef
)
);

type CTXType = {
isolate: {
type: IsolateTypes;
keys: IsolateKeys;
};
testCursor: Cursor;
isolate: Isolate;
stateRef?: StateRef;
exclusion: {
tests: Record<string, boolean>;
Expand Down
27 changes: 0 additions & 27 deletions packages/vest/src/core/ctx/testCursor.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/vest/src/core/isolate/IsolateTypes.ts
@@ -1,4 +1,5 @@
import VestTest from 'VestTest';
import { IsolateCursor } from 'isolateCursor';

export enum IsolateTypes {
DEFAULT,
Expand All @@ -13,3 +14,10 @@ export type IsolateKeys = {
current: Record<string, VestTest>;
prev: Record<string, VestTest>;
};

export type Isolate = {
type: IsolateTypes;
keys: IsolateKeys;
path: number[];
cursor: IsolateCursor;
};
69 changes: 54 additions & 15 deletions packages/vest/src/core/isolate/isolate.ts
Expand Up @@ -2,39 +2,78 @@ import invariant from 'invariant';
import isFunction from 'isFunction';
import * as nestedArray from 'nestedArray';

import { IsolateKeys, IsolateTypes } from 'IsolateTypes';
import { createIsolateCursor, IsolateCursor } from './isolateCursor';

import { Isolate, IsolateTypes } from 'IsolateTypes';
import VestTest from 'VestTest';
import ctx from 'ctx';
import { usePrevKeys } from 'key';
import { useSetTests } from 'stateHooks';
import * as testCursor from 'testCursor';

export function isolate(
{ type = IsolateTypes.DEFAULT }: { type?: IsolateTypes },
callback: () => VestTest[] | void
): VestTest[] | void {
invariant(isFunction(callback));

const keys: IsolateKeys = {
current: {},
prev: {},
};

const path = testCursor.usePath();
return ctx.run({ isolate: { type, keys } }, () => {
testCursor.addLevel();
// Generate a new Isolate layer, with its own cursor
const isolate = generateIsolate(type, useCurrentPath());

keys.prev = usePrevKeys();
const output = ctx.run({ isolate }, () => {
isolate.keys.prev = usePrevKeys();

useSetTests(tests => nestedArray.setValueAtPath(tests, path, []));
useSetTests(tests => nestedArray.setValueAtPath(tests, isolate.path, []));

const res = callback();
testCursor.removeLevel();
testCursor.moveForward();
return res;
});

// Move the parent cursor forward once we're done
useCursor().next();

return output;
}

/**
* @returns {Isolate} The current isolate layer
*/
export function useIsolate(): Isolate {
return ctx.useX().isolate;
}

export function shouldAllowReorder() {
/**
* @returns {boolean} Whether or not the current isolate allows tests to be reordered
*/
export function shouldAllowReorder(): boolean {
return ctx.useX().isolate.type === IsolateTypes.EACH;
}

/**
* @returns {number[]} The current cursor path of the isolate tree
*/
export function useCurrentPath(): number[] {
const isolate = useIsolate();
return isolate.path.concat(isolate.cursor.current());
}

/**
* @returns {IsolateCursor} The cursor object for the current isolate
*/
export function useCursor(): IsolateCursor {
return useIsolate().cursor;
}

export function generateIsolate(
type: IsolateTypes,
path: number[] = []
): Isolate {
return {
cursor: createIsolateCursor(),
keys: {
current: {},
prev: {},
},
path,
type,
};
}
29 changes: 29 additions & 0 deletions packages/vest/src/core/isolate/isolateCursor.ts
@@ -0,0 +1,29 @@
export function createIsolateCursor(): IsolateCursor {
const cursor = {
value: 0,
};

return {
current,
next,
};

/**
* @returns {number} The current value of the cursor
*/
function current(): number {
return cursor.value;
}

/**
* Moves the isolate cursor forward by 1
*/
function next(): void {
cursor.value++;
}
}

export type IsolateCursor = {
current: () => number;
next: () => void;
};
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/key.ts
Expand Up @@ -5,13 +5,13 @@ import { deferThrow } from 'throwError';

import VestTest from 'VestTest';
import ctx from 'ctx';
import { useCurrentPath } from 'isolate';
import { useTestObjects } from 'stateHooks';
import * as testCursor from 'testCursor';

export function usePrevKeys(): Record<string, VestTest> {
const [{ prev }] = useTestObjects();

return asArray(nestedArray.getCurrent(prev, testCursor.usePath())).reduce(
return asArray(nestedArray.getCurrent(prev, useCurrentPath())).reduce(
(prevKeys, testObject) => {
if (!(testObject instanceof VestTest)) {
return prevKeys;
Expand Down
13 changes: 8 additions & 5 deletions packages/vest/src/core/test/lib/registerPrevRunTest.ts
Expand Up @@ -3,28 +3,31 @@ import isPromise from 'isPromise';
import VestTest from 'VestTest';
import cancelOverriddenPendingTest from 'cancelOverriddenPendingTest';
import { isExcluded } from 'exclusive';
import { useCursor } from 'isolate';
import { shouldSkipBasedOnMode } from 'mode';
import { isOmitted } from 'omitWhen';
import registerTest from 'registerTest';
import runAsyncTest from 'runAsyncTest';
import { isExcludedIndividually } from 'skipWhen';
import * as testCursor from 'testCursor';
import { useTestAtCursor, useSetTestAtCursor } from 'useTestAtCursor';

// eslint-disable-next-line max-statements
export default function registerPrevRunTest(testObject: VestTest): VestTest {
const cursor = useCursor();

if (shouldSkipBasedOnMode(testObject)) {
testObject.skip();
useTestAtCursor(testObject);
testCursor.moveForward();

cursor.next();
return testObject;
}

const prevRunTest = useTestAtCursor(testObject);

if (isOmitted()) {
prevRunTest.omit();
testCursor.moveForward();
cursor.next();
return prevRunTest;
}

Expand All @@ -34,16 +37,16 @@ export default function registerPrevRunTest(testObject: VestTest): VestTest {
// This mostly means that we're probably giving
// up on this async test intentionally.
prevRunTest.skip(isExcludedIndividually());
testCursor.moveForward();
cursor.next();
return prevRunTest;
}
cancelOverriddenPendingTest(prevRunTest, testObject);

useSetTestAtCursor(testObject);
testCursor.moveForward();

registerTestObjectByTier(testObject);

cursor.next();
return testObject;
}

Expand Down
12 changes: 6 additions & 6 deletions packages/vest/src/core/test/lib/useTestAtCursor.ts
Expand Up @@ -7,10 +7,9 @@ import { deferThrow } from 'throwError';

import VestTest from 'VestTest';
import isSameProfileTest from 'isSameProfileTest';
import { shouldAllowReorder } from 'isolate';
import { shouldAllowReorder, useCurrentPath, useCursor } from 'isolate';
import { usePrevTestByKey, useRetainTestKey } from 'key';
import { useTestObjects, useSetTests } from 'stateHooks';
import * as testCursor from 'testCursor';

/**
* This module serves as the "collision detection" mechanism for Vest.
Expand Down Expand Up @@ -59,8 +58,9 @@ function removeAllNextTestsInIsolate() {
const [testObjects, setTestObjects] = useTestObjects();

const prevTests = testObjects.prev;
const current = nestedArray.getCurrent(prevTests, testCursor.usePath());
const cursorAt = testCursor.useCursorAt();
const current = nestedArray.getCurrent(prevTests, useCurrentPath());
const cursorAt = useCursor().current();

current.splice(cursorAt);
// We actually don't mind mutating the state directly (as can be seen above). There is no harm in it
// since we're only touching the "prev" state. The reason we still use the setter function is
Expand All @@ -72,15 +72,15 @@ function removeAllNextTestsInIsolate() {
}

export function useSetTestAtCursor(testObject: VestTest): void {
const cursorPath = testCursor.usePath();
const cursorPath = useCurrentPath();

useSetTests(tests =>
nestedArray.setValueAtPath(tests, cursorPath, testObject)
);
}

function useGetTestAtCursor(tests: NestedArray<VestTest>): VestTest {
const cursorPath = testCursor.usePath();
const cursorPath = useCurrentPath();

return nestedArray.valueAtPath(tests, cursorPath) as VestTest;
}
Expand Down

1 comment on commit 49096b1

@vercel
Copy link

@vercel vercel bot commented on 49096b1 May 13, 2022

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

Please sign in to comment.