Skip to content

Commit

Permalink
patch(vest): Use serializer replacer to omit unneeded properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 22, 2024
1 parent 1ea14a4 commit 64807d6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/vest/src/core/test/__tests__/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { faker } from '@faker-js/faker';
import { ErrorStrings } from 'ErrorStrings';
import { TIsolateTest } from 'IsolateTest';
import { VestTest } from 'VestTest';
import { text } from 'vest-utils';
import { IsolateSerializer } from 'vestjs-runtime';
import { describe, it, expect, vi } from 'vitest';

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 Down Expand Up @@ -193,7 +193,7 @@ describe("Test Vest's `test` function", () => {
expect(testObject.data.fieldName).toBe('field_name');
expect(testObject.key).toBe('keyboardcat');
expect(testObject.data.message).toBe('failure message');
expect(IsolateSerializer.serialize(testObject)).toMatchSnapshot();
expect(IsolateSerializer.serialize(testObject, v => v)).toMatchSnapshot();
});

it('throws when field name is not a string', () => {
Expand Down
34 changes: 32 additions & 2 deletions packages/vest/src/exports/SuiteSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CB } from 'vest-utils';
import { IsolateSerializer } from 'vestjs-runtime';
import { IsolateKeys } from 'vestjs-runtime/src/Isolate/IsolateKeys';

import { TIsolateSuite } from 'IsolateSuite';
import { TestStatus } from 'IsolateTestStateMachine';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { Suite } from 'SuiteTypes';

Expand All @@ -11,9 +13,9 @@ export type Dumpable = {

export class SuiteSerializer {
static serialize(suite: Dumpable) {
const dump = { ...suite.dump(), output: undefined };
const dump = { ...suite.dump() };

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

static deserialize(
Expand All @@ -31,3 +33,31 @@ export class SuiteSerializer {
suite.resume(suiteRoot);
}
}

function suiteSerializerReplacer(value: any, key: string) {
if (key === 'output') {
return undefined;
}

if (key === IsolateKeys.Status) {
if (AllowedStatuses.has(value)) {
return value;
}

return undefined;
}

if (DisallowedKeys.has(key)) {
return undefined;
}

return value;
}

const AllowedStatuses = new Set([
TestStatus.FAILED,
TestStatus.PASSING,
TestStatus.WARNING,
]);

const DisallowedKeys = new Set(['focusMode', 'match', 'matchAll', 'severity']);
3 changes: 1 addition & 2 deletions packages/vest/src/exports/__tests__/SuiteSerializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SuiteSerializer } from 'SuiteSerializer';
import { describe, it, expect } from 'vitest';

import { SuiteSerializer } from 'SuiteSerializer';
import * as vest from 'vest';

describe('SuiteSerializer', () => {
Expand Down Expand Up @@ -48,7 +48,6 @@ describe('suite.resume', () => {
});

suite();
suite.get();

const serialized = SuiteSerializer.serialize(suite);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

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[`SuiteSerializer > Should produce a valid serialized dump 1`] = `"[{"0":"Suite","1":{},"5":[{"0":"Focused","1":{}},{"0":"2","1":{"3":"field_1","4":"field_1_message"},"status":"FAILED"},{"0":"2","1":{"3":"field_2","4":"field_2_message"}},{"0":"Group","5":[{"0":"2","1":{"3":"8","4":"field_3_message_1","6":"7"}},{"0":"2","1":{"3":"8","4":"field_3_message_2","6":"7"}},{"0":"2","1":{"3":"field_4","4":"field_4_message","6":"7"}}]},{"0":"SkipWhen","5":[{"0":"2","1":{"3":"field_5","4":"field_5_message"}}]}]},{"0":"$type","1":"data","2":"Test","3":"fieldName","4":"message","5":"children","6":"groupName","7":"group_1","8":"field_3"}]"`;

exports[`suite.resume > Running the suite after resuming > Sanity - suite should run as expected > Should have correct state after resuming 1`] = `
{
Expand Down
12 changes: 10 additions & 2 deletions packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function addSummaryStats<F extends TFieldName, G extends TGroupName>(
summary.pendingCount++;
}

if (!VestTest.isNonActionable(testObject)) {
if (shouldCountTestRun(testObject)) {
summary.testCount++;
}

Expand Down Expand Up @@ -141,7 +141,9 @@ function appendTestObject(
}

// Increment the test count.
nextSummaryKey.testCount++;
if (shouldCountTestRun(testObject)) {
nextSummaryKey.testCount++;
}

return nextSummaryKey;

Expand All @@ -164,3 +166,9 @@ function baseTestStats() {
warnings: [],
});
}

function shouldCountTestRun<F extends TFieldName, G extends TGroupName>(
testObject: TIsolateTest<F, G>,
): boolean {
return VestTest.isTested(testObject) || VestTest.isPending(testObject);
}

0 comments on commit 64807d6

Please sign in to comment.