Skip to content

Commit

Permalink
patch(vestjs-runtime): use minifyObject for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 23, 2023
1 parent c51297a commit d664f5d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 411 deletions.
10 changes: 5 additions & 5 deletions packages/vest/src/core/isolate/IsolateSuite/IsolateSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type TIsolateSuite = TIsolate<{
}>;

export function IsolateSuite<Callback extends CB = CB>(
callback: Callback
callback: Callback,
): TIsolateSuite {
return Isolate.create(VestIsolateType.Suite, callback, {
optional: {},
Expand All @@ -21,7 +21,7 @@ export class SuiteOptionalFields {
static setOptionalField(
suite: TIsolateSuite,
fieldName: TFieldName,
setter: (current: OptionalFieldDeclaration) => OptionalFieldDeclaration
setter: (current: OptionalFieldDeclaration) => OptionalFieldDeclaration,
): void {
const current = suite.data.optional;
const currentField = current[fieldName];
Expand All @@ -33,12 +33,12 @@ export class SuiteOptionalFields {

static getOptionalField(
suite: TIsolateSuite,
fieldName: TFieldName
fieldName: TFieldName,
): OptionalFieldDeclaration {
return suite.data.optional[fieldName] ?? {};
return SuiteOptionalFields.getOptionalFields(suite)[fieldName] ?? {};
}

static getOptionalFields(suite: TIsolateSuite): OptionalFields {
return suite.data.optional;
return suite.data?.optional ?? {};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test Vest's \`test\` function test params creates a test with a message and with a key 1`] = `"[{"$type":"Test","data":{"severity":"error","fieldName":"field_name","message":"failure message"},"status":"PASSING","key":"keyboardcat"},{}]"`;

exports[`Test Vest's \`test\` function test params creates a test without a key 1`] = `
{
"$type": "Test",
Expand Down
34 changes: 16 additions & 18 deletions packages/vest/src/core/test/__tests__/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { faker } from '@faker-js/faker';
import { VestTest } from 'VestTest';
import { text } from 'vest-utils';
import { IsolateSerializer } from 'vestjs-runtime';

import { TestPromise } from '../../../testUtils/testPromise';

import { ErrorStrings } from 'ErrorStrings';
import { TIsolateTest } from 'IsolateTest';
import { VestTest } from 'VestTest';
import { enforce } from 'vest';
import * as vest from 'vest';

Expand All @@ -22,7 +22,7 @@ describe("Test Vest's `test` function", () => {
faker.lorem.sentence(),
() => {
vest.warn();
}
},
);
})();
expect(VestTest.warns(testObject)).toBe(true);
Expand All @@ -37,7 +37,7 @@ describe("Test Vest's `test` function", () => {
faker.lorem.sentence(),
() => {
throw new Error();
}
},
);
})();
expect(VestTest.isFailing(testObject)).toBe(true);
Expand All @@ -63,7 +63,7 @@ describe("Test Vest's `test` function", () => {
() => {
vest.warn();
failWithString();
}
},
);
})();

Expand Down Expand Up @@ -114,7 +114,7 @@ describe("Test Vest's `test` function", () => {
'I fail with a message',
]);
expect(res.tests['warning_field_without_message'].warnings).toEqual(
['I fail with a message']
['I fail with a message'],
);
});
});
Expand All @@ -132,7 +132,7 @@ describe("Test Vest's `test` function", () => {
new Promise((_, reject) => {
expect(VestTest.isFailing(testObject)).toBe(false);
setTimeout(reject, 300);
})
}),
);
expect(VestTest.isFailing(testObject)).toBe(false);
setTimeout(() => {
Expand Down Expand Up @@ -161,7 +161,7 @@ describe("Test Vest's `test` function", () => {
testObject = vest.test(
'field_name',
'failure message',
() => undefined
() => undefined,
);
})();
expect(testObject.data.fieldName).toBe('field_name');
Expand All @@ -186,15 +186,13 @@ describe("Test Vest's `test` function", () => {
'field_name',
'failure message',
() => undefined,
'keyboardcat'
'keyboardcat',
);
})();
expect(testObject.data.fieldName).toBe('field_name');
expect(testObject.key).toBe('keyboardcat');
expect(testObject.data.message).toBe('failure message');
expect(IsolateSerializer.serialize(testObject)).toMatchInlineSnapshot(
`"{"D":{"severity":"error","fieldName":"field_name","message":"failure message"},"$":"Test","S":"PASSING","ky":"keyboardcat"}"`
);
expect(IsolateSerializer.serialize(testObject)).toMatchSnapshot();
});

it('throws when field name is not a string', () => {
Expand All @@ -206,25 +204,25 @@ describe("Test Vest's `test` function", () => {
fn_name: 'test',
param: 'fieldName',
expected: 'string',
})
}),
);
// @ts-expect-error
expect(() => vest.test(null, 'error message', () => undefined)).toThrow(
text(ErrorStrings.INVALID_PARAM_PASSED_TO_FUNCTION, {
fn_name: 'test',
param: 'fieldName',
expected: 'string',
})
}),
);
expect(() =>
// @ts-expect-error
vest.test(null, 'error message', () => undefined, 'key')
vest.test(null, 'error message', () => undefined, 'key'),
).toThrow(
text(ErrorStrings.INVALID_PARAM_PASSED_TO_FUNCTION, {
fn_name: 'test',
param: 'fieldName',
expected: 'string',
})
}),
);
control();
})();
Expand All @@ -240,23 +238,23 @@ describe("Test Vest's `test` function", () => {
fn_name: 'test',
param: 'callback',
expected: 'function',
})
}),
);
// @ts-expect-error
expect(() => vest.test('x', 'msg', undefined)).toThrow(
text(ErrorStrings.INVALID_PARAM_PASSED_TO_FUNCTION, {
fn_name: 'test',
param: 'callback',
expected: 'function',
})
}),
);
// @ts-expect-error
expect(() => vest.test('x', 'msg', undefined, 'key')).toThrow(
text(ErrorStrings.INVALID_PARAM_PASSED_TO_FUNCTION, {
fn_name: 'test',
param: 'callback',
expected: 'function',
})
}),
);
control();
})();
Expand Down
71 changes: 4 additions & 67 deletions packages/vest/src/exports/SuiteSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,28 @@
import { assign } from 'vest-utils';
import { IsolateSerializer } from 'vestjs-runtime';

import { CommonStates } from 'CommonStateMachine';
import { TIsolateSuite } from 'IsolateSuite';
import { IsolateTestPayload } from 'IsolateTest';
import { TestStatus } from 'IsolateTestStateMachine';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { Suite } from 'SuiteTypes';
import { VestIsolateType } from 'VestIsolateType';
import { IsolateFocusedPayload } from 'focused';

export class SuiteSerializer {
static serialize(suite: Suite<TFieldName, TGroupName>) {
const dump = { ...suite.dump(), output: undefined };

return IsolateSerializer.serialize(dump, MiniMap);
return IsolateSerializer.serialize(dump);
}

static deserialize(
serialized: string | TIsolateSuite | Record<string, any>
serialized: string | TIsolateSuite | Record<string, any>,
): TIsolateSuite {
return IsolateSerializer.deserialize(serialized, MiniMap) as TIsolateSuite;
return IsolateSerializer.deserialize(serialized) as TIsolateSuite;
}

static resume(
suite: Suite<TFieldName, TGroupName>,
root: string | TIsolateSuite | Record<string, any>
root: string | TIsolateSuite | Record<string, any>,
): void {
const suiteRoot = SuiteSerializer.deserialize(root);

suite.resume(suiteRoot);
}
}

const testMiniMap: Record<keyof IsolateTestPayload, string> = {
asyncTest: '_at', // asyncTest is not serialized
fieldName: 'fN',
groupName: 'gN',
message: 'ms',
severity: 'sv',
status: 'st',
testFn: '_tf', // testFn is not serialized
};

const focusMiniMap: Record<keyof IsolateFocusedPayload, string> = {
focusMode: 'fM',
match: 'm',
matchAll: 'mA',
};

const MiniMap: MiniMap = {
keys: {
data: assign({}, testMiniMap, focusMiniMap),
},
values: {
status: {
CANCELED: 'C',
DONE: 'D',
FAILED: 'F',
INITIAL: 'I',
OMITTED: 'O',
PASSING: 'P',
PENDING: 'PE',
SKIPPED: 'S',
UNTESTED: 'U',
WARNING: 'W',
},
$type: {
Each: 'E',
Focused: 'F',
Group: 'G',
OmitWhen: 'OW',
SkipWhen: 'SW',
Suite: 'S',
Test: 'T',
},
},
};

type MiniMap = {
keys: {
data: Record<keyof IsolateTestPayload, string> &
Record<keyof IsolateFocusedPayload, string>;
};
values: {
status: Record<keyof typeof TestStatus, string> &
Record<keyof typeof CommonStates, string>;
$type: Record<keyof typeof VestIsolateType, string>;
};
};
45 changes: 0 additions & 45 deletions packages/vest/src/exports/__tests__/SuiteSerializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SuiteSerializer } from 'SuiteSerializer';
import { parse } from 'parser';
import * as vest from 'vest';

describe('SuiteSerializer', () => {
Expand Down Expand Up @@ -27,50 +26,6 @@ describe('SuiteSerializer', () => {
});
});

it('Should minify test payload', () => {
const suite = vest.create('suite_serialize_test', () => {
vest.test('field_1', 'field_1_message', () => false);
});

suite();
const serialized = SuiteSerializer.serialize(suite);

const parsed = JSON.parse(serialized);

expect(parsed['C'][0]['D']).toBeDefined();
expect(parsed['C'][0]['D']['fN']).toBe('field_1');
expect(parsed['C'][0]['D']['ms']).toBe('field_1_message');
expect(parsed['C'][0]['D']['sv']).toBe('error');
});

it('Should minify statuses', () => {
const suite = vest.create('suite_serialize_test', () => {
vest.test('field_1', 'field_1_message', () => false);
});

suite();
const serialized = SuiteSerializer.serialize(suite);

const parsed = JSON.parse(serialized);

expect(parsed['S']).toBe('D');
expect(parsed['C'][0]['S']).toBe('F');
});

it('Should minify isolate types', () => {
const suite = vest.create('suite_serialize_test', () => {
vest.test('field_1', 'field_1_message', () => false);
});

suite();
const serialized = SuiteSerializer.serialize(suite);

const parsed = JSON.parse(serialized);

expect(parsed['$']).toBe('S');
expect(parsed['C'][0]['$']).toBe('T');
});

describe('suite.resume', () => {
it('Should resume a suite from a serialized dump', () => {
const suite = vest.create(() => {
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`] = `"{"C":[{"D":{"fM":0,"m":["field_1"],"mA":false},"$":"F","S":"D"},{"D":{"sv":"error","fN":"field_1","ms":"field_1_message"},"$":"T","S":"F"},{"D":{"sv":"error","fN":"field_2","ms":"field_2_message"},"$":"T","S":"S"},{"C":[{"D":{"sv":"error","fN":"field_3","gN":"group_1","ms":"field_3_message_1"},"$":"T","S":"S"},{"D":{"sv":"error","fN":"field_3","gN":"group_1","ms":"field_3_message_2"},"$":"T","S":"S"},{"D":{"sv":"error","fN":"field_4","gN":"group_1","ms":"field_4_message"},"$":"T","S":"S"}],"$":"G","S":"D"},{"C":[{"D":{"sv":"error","fN":"field_5","ms":"field_5_message"},"$":"T","S":"S"}],"$":"SW","S":"D"}],"D":{"optional":{}},"$":"S","S":"D"}"`;
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"}]"`;
Loading

2 comments on commit d664f5d

@vercel
Copy link

@vercel vercel bot commented on d664f5d Dec 23, 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-git-latest-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d664f5d Dec 23, 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
www.vestjs.dev
vest-git-latest-ealush.vercel.app
vest-ealush.vercel.app
vestjs.dev

Please sign in to comment.