Skip to content

Commit

Permalink
dumpable static suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 17, 2024
1 parent 543d2c8 commit b7a7b96
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 31 deletions.
5 changes: 5 additions & 0 deletions packages/vest-utils/src/freezeAssign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assign from 'assign';

export function freezeAssign<T extends object>(...args: Partial<T>[]): T {
return Object.freeze(assign({}, ...args));
}
1 change: 1 addition & 0 deletions packages/vest-utils/src/vest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export * as tinyState from 'tinyState';
export { StringObject } from 'StringObject';
export { noop } from 'noop';
export * as Predicates from 'Predicates';
export { freezeAssign } from 'freezeAssign';

export type {
DropFirst,
Expand Down
1 change: 1 addition & 0 deletions packages/vest-utils/tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/vest/src/exports/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { registerReconciler } from 'vest';
import { CB, isPromise, Nullable } from 'vest-utils';
import { Isolate, TIsolate, IsolateSelectors } from 'vestjs-runtime';

import { TestFn, TestFnPayload } from 'TestTypes';
import { registerReconciler } from 'vest';

const isolateType = 'Debounce';

Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/exports/parser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { suiteSelectors } from 'vest';
import { hasOwnProperty, invariant, isNullish, isPositive } from 'vest-utils';

import { ErrorStrings } from 'ErrorStrings';
import { SuiteSummary, TFieldName, TGroupName } from 'SuiteResultTypes';
import { suiteSelectors } from 'vest';

export function parse<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>
summary: SuiteSummary<F, G>,
): ParsedVestObject<F> {
invariant(
summary && hasOwnProperty(summary, 'valid'),
ErrorStrings.PARSER_EXPECT_RESULT_OBJECT
ErrorStrings.PARSER_EXPECT_RESULT_OBJECT,
);

const sel = suiteSelectors(summary);
Expand Down
33 changes: 33 additions & 0 deletions packages/vest/src/suite/__tests__/staticSuite.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import wait from 'wait';

import { SuiteSerializer } from 'SuiteSerializer';
import { VestIsolateType } from 'VestIsolateType';
import * as vest from 'vest';
Expand Down Expand Up @@ -139,4 +141,35 @@ describe('runStatic', () => {
expect(suite2.hasErrors('t3')).toBe(false);
expect(suite2.hasErrors('t4')).toBe(false);
});

describe('runStatic.resolve', () => {
it("Should resolve with the suite's result", async () => {
const suite = vest.create(() => {
vest.test('t1', async () => {
await wait(100);
throw new Error();
});
});

const res = suite.runStatic();
expect(res.errorCount).toBe(0);
expect(res).toHaveProperty('resolve');
const result = await res.resolve();
expect(result.errorCount).toBe(1);
});

it("Should have a dump method on the resolved suite's result", async () => {
const suite = vest.create(() => {
vest.test('t1', async () => {
await wait(100);
throw new Error();
});
});

const res = suite.runStatic();
const result = await res.resolve();
expect(result).toHaveProperty('dump');
expect(result.dump()).toHaveProperty('$type', VestIsolateType.Suite);
});
});
});
40 changes: 28 additions & 12 deletions packages/vest/src/suite/createSuite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign, CB } from 'vest-utils';
import { assign, CB, freezeAssign } from 'vest-utils';
import { Bus, VestRuntime } from 'vestjs-runtime';

import { TTypedMethods, getTypedMethods } from './getTypedMethods';
Expand All @@ -9,6 +9,7 @@ import { useCreateVestState, useLoadSuite } from 'Runtime';
import { SuiteContext } from 'SuiteContext';
import {
SuiteName,
SuiteResult,
SuiteRunResult,
TFieldName,
TGroupName,
Expand Down Expand Up @@ -154,14 +155,22 @@ function staticSuite<

const result = suite(...args);

return Object.freeze(
assign(
{
dump: suite.dump,
},
result,
),
) as StaticSuiteRunResult<F, G>;
const resolve = new Promise<SuiteWithDump<F, G>>(resolve => {
result.done(res => {
resolve(withDump(res) as SuiteWithDump<F, G>);
});
});

return freezeAssign<StaticSuiteRunResult<F, G>>(
withDump({
resolve: () => resolve,
}),
result,
);

function withDump(o: any) {
return freezeAssign({ dump: suite.dump }, o);
}
},
{
...getTypedMethods<F, G>(),
Expand All @@ -178,8 +187,15 @@ export type StaticSuite<
export type StaticSuiteRunResult<
F extends TFieldName = string,
G extends TGroupName = string,
> = SuiteRunResult<F, G> & {
dump: CB<TIsolateSuite>;
} & TTypedMethods<F, G>;
> = WithDump<
SuiteRunResult<F, G> & {
resolve: () => Promise<SuiteWithDump<F, G>>;
} & TTypedMethods<F, G>
>;

type WithDump<T> = T & { dump: CB<TIsolateSuite> };
type SuiteWithDump<F extends TFieldName, G extends TGroupName> = WithDump<
SuiteResult<F, G>
>;

export { createSuite, staticSuite };
12 changes: 7 additions & 5 deletions packages/vest/src/suiteResult/suiteResult.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign } from 'vest-utils';
import { freezeAssign } from 'vest-utils';

import { useSuiteName, useSuiteResultCache } from 'Runtime';
import { SuiteResult, TFieldName, TGroupName } from 'SuiteResultTypes';
Expand All @@ -7,18 +7,20 @@ import { useProduceSuiteSummary } from 'useProduceSuiteSummary';

export function useCreateSuiteResult<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
>(): SuiteResult<F, G> {
return useSuiteResultCache<F, G>(() => {
// @vx-allow use-use
const summary = useProduceSuiteSummary<F, G>();

// @vx-allow use-use
const suiteName = useSuiteName();
return Object.freeze(
assign(summary, suiteSelectors<F, G>(summary), {
return freezeAssign<SuiteResult<F, G>>(
summary,
suiteSelectors<F, G>(summary),
{
suiteName,
})
},
) as SuiteResult<F, G>;
});
}
18 changes: 8 additions & 10 deletions packages/vest/src/suiteResult/suiteRunResult.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign } from 'vest-utils';
import { freezeAssign } from 'vest-utils';
import { VestRuntime } from 'vestjs-runtime';

import {
Expand All @@ -14,15 +14,13 @@ import { useCreateSuiteResult } from 'suiteResult';

export function useSuiteRunResult<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
>(): SuiteRunResult<F, G> {
return Object.freeze(
assign(
{
done: VestRuntime.persist(done),
},
useCreateSuiteResult<F, G>()
)
return freezeAssign<SuiteRunResult<F, G>>(
{
done: VestRuntime.persist(done) as Done<F, G>,
},
useCreateSuiteResult<F, G>(),
);
}

Expand All @@ -36,7 +34,7 @@ function done<F extends TFieldName, G extends TGroupName>(
): SuiteRunResult<F, G> {
const [callback, fieldName] = args.reverse() as [
(res: SuiteResult<F, G>) => void,
F
F,
];
const output = useSuiteRunResult<F, G>();
if (shouldSkipDoneRegistration<F, G>(callback, fieldName, output)) {
Expand Down

0 comments on commit b7a7b96

Please sign in to comment.