Skip to content

Commit

Permalink
patch(vest): Imporve resume functionality by treating key as null/und…
Browse files Browse the repository at this point in the history
…efined
  • Loading branch information
ealush committed Feb 18, 2024
1 parent b733d78 commit 0f18c3b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export class IsolateTestReconciler extends IsolateReconciler {

static reconcile(
currentNode: TIsolateTest,
historyNode: TIsolateTest
historyNode: TIsolateTest,
): TIsolateTest {
const reconcilerOutput = usePickNode(historyNode, currentNode);

cancelOverriddenPendingTestOnTestReRun(
reconcilerOutput,
currentNode,
historyNode
historyNode,
);

return reconcilerOutput;
Expand All @@ -33,7 +33,7 @@ export class IsolateTestReconciler extends IsolateReconciler {

function usePickNode(
historyNode: TIsolateTest,
currentNode: TIsolateTest
currentNode: TIsolateTest,
): TIsolateTest {
const collisionResult = handleCollision(currentNode, historyNode);

Expand All @@ -42,7 +42,7 @@ function usePickNode(

function handleCollision(
newNode: TIsolateTest,
prevNode?: TIsolate
prevNode?: TIsolate,
): TIsolateTest {
if (IsolateInspector.usesKey(newNode)) {
return VestTest.cast(Reconciler.handleIsolateNodeWithKey(newNode));
Expand Down Expand Up @@ -78,7 +78,7 @@ function handleCollision(
function cancelOverriddenPendingTestOnTestReRun(
nextNode: TIsolate,
currentNode: TIsolate,
prevTestObject: TIsolateTest
prevTestObject: TIsolateTest,
) {
if (nextNode === currentNode && VestTest.is(currentNode)) {
cancelOverriddenPendingTest(prevTestObject, currentNode);
Expand All @@ -87,14 +87,14 @@ function cancelOverriddenPendingTestOnTestReRun(

function nodeReorderDetected(
newNode: TIsolateTest,
prevNode: Maybe<TIsolate>
prevNode: Maybe<TIsolate>,
): boolean {
return VestTest.is(prevNode) && !isSameProfileTest(prevNode, newNode);
}

function throwTestOrderError(
newNode: TIsolateTest,
prevNode: Maybe<TIsolate>
prevNode: Maybe<TIsolate>,
): void {
if (IsolateInspector.canReorder(newNode)) {
return;
Expand All @@ -106,6 +106,6 @@ function throwTestOrderError(
prevName: VestTest.is(prevNode)
? VestTest.getData(prevNode).fieldName
: undefined,
})
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import matchingFieldName from 'matchingFieldName';

export function isSameProfileTest(
testObject1: TIsolateTest,
testObject2: TIsolateTest
testObject2: TIsolateTest,
): boolean {
const { groupName: gn1 } = VestTest.getData(testObject1);
const { groupName: gn2, fieldName: fn2 } = VestTest.getData(testObject2);
return (
matchingFieldName(VestTest.getData(testObject1), fn2) &&
gn1 === gn2 &&
testObject1.key === testObject2.key
// Specifically using == here. The reason is that when serializing
// suite result, empty key gets removed, but it can also be null.
testObject1.key == testObject2.key
);
}
6 changes: 3 additions & 3 deletions packages/vest/src/core/test/helpers/matchingFieldName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import { WithFieldName } from 'TestTypes';

export function nonMatchingFieldName(
WithFieldName: WithFieldName<TFieldName>,
fieldName?: Maybe<TFieldName>
fieldName?: Maybe<TFieldName>,
): boolean {
return !!fieldName && !matchingFieldName(WithFieldName, fieldName);
}

export default function matchingFieldName(
WithFieldName: WithFieldName<TFieldName>,
fieldName?: Maybe<TFieldName>
fieldName?: Maybe<TFieldName>,
): boolean {
return !!(fieldName && WithFieldName.fieldName === fieldName);
}

export function matchesOrHasNoFieldName(
WithFieldName: WithFieldName<TFieldName>,
fieldName?: Maybe<TFieldName>
fieldName?: Maybe<TFieldName>,
): boolean {
if (fieldName) {
return matchingFieldName(WithFieldName, fieldName);
Expand Down
46 changes: 46 additions & 0 deletions packages/vest/src/exports/__tests__/SuiteSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,50 @@ describe('suite.resume', () => {
expect(suite2.hasWarnings()).toBe(false);
expect(suite2.get().tests.field_1).toBeUndefined();
});

describe('Running the suite after resuming', () => {
function cb(data: Record<string, any>, only?: string) {
vest.only(only);

vest.test('field_1', 'field_1_message', () => {
vest.enforce(data.field_1).isNotBlank();
});
vest.test('field_2', 'field_2_message', () => {
vest.enforce(data.field_2).isNotBlank();
});
}

it('Should continue with resumed state if matching', () => {
const suite = vest.create('suite_resume_test', cb);

suite({});

const serialized = SuiteSerializer.serialize(suite);

const suite2 = vest.create(cb);
SuiteSerializer.resume(suite2, serialized);
suite2({}, 'field_1');
expect(suite2.getError('field_1')).toBe('field_1_message');
expect(suite2.getError('field_2')).toBe('field_2_message');
});

describe('Sanity - suite should run as expected', () => {
it('Should have correct state after resuming', () => {
const suite = vest.create('suite_resume_test', cb);

suite({});

const serialized = SuiteSerializer.serialize(suite);

const suite2 = vest.create(cb);

SuiteSerializer.resume(suite2, serialized);

expect(suite2.getError('field_1')).toBe('field_1_message');
expect(suite2.getError('field_2')).toBe('field_2_message');

expect(suite2.getErrors()).toMatchSnapshot();
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SuiteSerializer Should produce a valid serialized dump 1`] = `"[{"0":"Suite","1":"9","2":{},"a":[{"0":"Focused","1":"9","2":{"focusMode":0,"match":["d"],"matchAll":false}},{"0":"3","1":"FAILED","2":{"4":"5","6":"d","7":"field_1_message"}},{"0":"3","1":"8","2":{"4":"5","6":"field_2","7":"field_2_message"}},{"0":"Group","1":"9","a":[{"0":"3","1":"8","2":{"4":"5","6":"e","7":"field_3_message_1","b":"c"}},{"0":"3","1":"8","2":{"4":"5","6":"e","7":"field_3_message_2","b":"c"}},{"0":"3","1":"8","2":{"4":"5","6":"field_4","7":"field_4_message","b":"c"}}]},{"0":"SkipWhen","1":"9","a":[{"0":"3","1":"8","2":{"4":"5","6":"field_5","7":"field_5_message"}}]}]},{"0":"$type","1":"status","2":"data","3":"Test","4":"severity","5":"error","6":"fieldName","7":"message","8":"SKIPPED","9":"DONE","a":"children","b":"groupName","c":"group_1","d":"field_1","e":"field_3"}]"`;

exports[`suite.resume Running the suite after resuming Sanity - suite should run as expected Should have correct state after resuming 1`] = `
{
"field_1": [
"field_1_message",
],
"field_2": [
"field_2_message",
],
}
`;

0 comments on commit 0f18c3b

Please sign in to comment.