Skip to content

Commit

Permalink
minor(vest): staticSuite returns a promise (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush authored Jul 15, 2024
1 parent 543d2c8 commit 8f74204
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 32 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 as [Partial<T>])));
}
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';

Check warning on line 4 in packages/vest/src/exports/debounce.ts

View workflow job for this annotation

GitHub Actions / build (20)

There should be at least one empty line between import groups
import { registerReconciler } from 'vest';

Check warning on line 5 in packages/vest/src/exports/debounce.ts

View workflow job for this annotation

GitHub Actions / build (20)

`vest` import should occur before import of `vest-utils`

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';

Check warning on line 4 in packages/vest/src/exports/parser.ts

View workflow job for this annotation

GitHub Actions / build (20)

There should be at least one empty line between import groups
import { suiteSelectors } from 'vest';

Check warning on line 5 in packages/vest/src/exports/parser.ts

View workflow job for this annotation

GitHub Actions / build (20)

`vest` import should occur before import of `vest-utils`

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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Top Level Focus Top Level Skip When passing false Should run all fields 1`] = `
{
Promise {
"done": [Function],
"dump": [Function],
"errorCount": 3,
Expand Down Expand Up @@ -83,7 +83,7 @@ exports[`Top Level Focus Top Level Skip When passing false Should run all fields
`;

exports[`Top Level Focus Top Level Skip When passing undefined Should run all fields 1`] = `
{
Promise {
"done": [Function],
"dump": [Function],
"errorCount": 3,
Expand Down
32 changes: 32 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,34 @@ describe('runStatic', () => {
expect(suite2.hasErrors('t3')).toBe(false);
expect(suite2.hasErrors('t4')).toBe(false);
});

describe('runStatic (promise)', () => {
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);
const result = await res;
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;
expect(result).toHaveProperty('dump');
expect(result.dump()).toHaveProperty('$type', VestIsolateType.Suite);
});
});
});
31 changes: 20 additions & 11 deletions packages/vest/src/suite/createSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,18 @@ function staticSuite<

const result = suite(...args);

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

function withDump(o: any) {
return assign({ dump: suite.dump }, o);
}
},
{
...getTypedMethods<F, G>(),
Expand All @@ -178,8 +183,12 @@ export type StaticSuite<
export type StaticSuiteRunResult<
F extends TFieldName = string,
G extends TGroupName = string,
> = SuiteRunResult<F, G> & {
dump: CB<TIsolateSuite>;
} & TTypedMethods<F, G>;
> = Promise<SuiteWithDump<F, G>> &
WithDump<SuiteRunResult<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 8f74204

Please sign in to comment.