Skip to content

Commit

Permalink
afterEach basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 11, 2024
1 parent ad3aed5 commit 88df14c
Show file tree
Hide file tree
Showing 16 changed files with 561 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`Base behavior > Should produce correct validation result 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`Stateful async tests > Merges skipped validations from previous suite 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`Stateful behavior > Should merge skipped fields with previous values 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -87,6 +89,8 @@ exports[`Stateful behavior > Should merge skipped fields with previous values 1`

exports[`Stateful behavior > Should merge skipped fields with previous values 2`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 3,
"errors": [
Expand Down Expand Up @@ -185,6 +189,8 @@ exports[`Stateful behavior > Should merge skipped fields with previous values 2`

exports[`Stateful behavior > Should merge skipped fields with previous values 3`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 4,
"errors": [
Expand Down
9 changes: 9 additions & 0 deletions packages/vest/src/core/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
export type DoneCallback = (res: SuiteResult<TFieldName, TGroupName>) => void;
type FieldCallbacks = Record<string, DoneCallbacks>;
type DoneCallbacks = Array<DoneCallback>;
type AfterEachCallbaks = Array<DoneCallback>;
type FailuresCache = {
[Severity.ERRORS]: Record<TFieldName, TIsolate[]>;
[Severity.WARNINGS]: Record<TFieldName, TIsolate[]>;
Expand All @@ -33,6 +34,7 @@ export type PreAggCache = {
type StateExtra = {
doneCallbacks: TinyState<DoneCallbacks>;
fieldCallbacks: TinyState<FieldCallbacks>;
afterEachCallbacks: TinyState<AfterEachCallbaks>;
suiteName: Maybe<string>;
suiteId: string;
suiteResultCache: CacheApi<SuiteResult<TFieldName, TGroupName>>;
Expand All @@ -49,6 +51,7 @@ export function useCreateVestState({
VestReconciler: IRecociler;
}) {
const stateRef: StateExtra = {
afterEachCallbacks: tinyState.createTinyState<AfterEachCallbaks>(() => []),
doneCallbacks: tinyState.createTinyState<DoneCallbacks>(() => []),
fieldCallbacks: tinyState.createTinyState<FieldCallbacks>(() => ({})),
preAggCache,
Expand All @@ -72,6 +75,10 @@ export function useFieldCallbacks() {
return useX().fieldCallbacks();
}

export function useAfterEachCallbacks() {
return useX().afterEachCallbacks();
}

export function useSuiteName() {
return useX().suiteName;
}
Expand Down Expand Up @@ -107,9 +114,11 @@ export function useExpireSuiteResultCache() {
export function useResetCallbacks() {
const [, , resetDoneCallbacks] = useDoneCallbacks();
const [, , resetFieldCallbacks] = useFieldCallbacks();
const [, , resetAfterEachCallbacks] = useAfterEachCallbacks();

resetDoneCallbacks();
resetFieldCallbacks();
resetAfterEachCallbacks();
}

export function useResetSuite() {
Expand Down
7 changes: 6 additions & 1 deletion packages/vest/src/core/VestBus/VestBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { TestWalker } from 'TestWalker';
import { VestIsolate } from 'VestIsolate';
import { VestTest } from 'VestTest';
import { useOmitOptionalFields } from 'omitOptionalFields';
import { useRunDoneCallbacks, useRunFieldCallbacks } from 'runCallbacks';
import {
useRunAfterEachCallbacks,
useRunDoneCallbacks,
useRunFieldCallbacks,
} from 'runCallbacks';

// eslint-disable-next-line max-statements, max-lines-per-function
export function useInitVestBus() {
Expand Down Expand Up @@ -45,6 +49,7 @@ export function useInitVestBus() {
const { fieldName } = VestTest.getData(isolate);

useRunFieldCallbacks(fieldName);
useRunAfterEachCallbacks(fieldName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ exports[`useLoadSuite > Calling useLoadSuite should resume from loaded state 1`]
"key": null,
"keys": null,
"output": {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 4,
"errors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`test.memo > cache hit > Should produce correct initial result 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -75,6 +77,8 @@ exports[`test.memo > cache hit > Should produce correct initial result 1`] = `

exports[`test.memo > cache hit > sync > Should restore previous result on re-run 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ exports[`Test Vest's \`test\` function > test params > creates a test without a
"key": null,
"keys": null,
"output": {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 0,
"errors": [],
Expand Down Expand Up @@ -103,6 +105,8 @@ exports[`Test Vest's \`test\` function > test params > creates a test without a
"keyboardcat": [Circular],
},
"output": {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 0,
"errors": [],
Expand Down Expand Up @@ -174,6 +178,8 @@ exports[`Test Vest's \`test\` function > test params > creates a test without a
"key": null,
"keys": null,
"output": {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 0,
"errors": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`include > Field is excluded via \`skip\` > Should disregard \`include.when\` and avoid running the test 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -58,6 +60,8 @@ exports[`include > Field is excluded via \`skip\` > Should disregard \`include.w

exports[`include > Field is excluded via \`skip\` > Should disregard \`include\` and avoid running the test 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -114,6 +118,8 @@ exports[`include > Field is excluded via \`skip\` > Should disregard \`include\`

exports[`include > Test is excluded via \`skip.group\` > Should disregard \`include.when\` and avoid running the test 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -191,6 +197,8 @@ exports[`include > Test is excluded via \`skip.group\` > Should disregard \`incl

exports[`include > Test is excluded via \`skip.group\` > Should disregard \`include\` and avoid running the test 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -268,6 +276,8 @@ exports[`include > Test is excluded via \`skip.group\` > Should disregard \`incl

exports[`include > Test is excluded via \`skipWhen\` > Should disregard \`include.when\` and avoid running the matching tests 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -315,6 +325,8 @@ exports[`include > Test is excluded via \`skipWhen\` > Should disregard \`includ

exports[`include > Test is excluded via \`skipWhen\` > Should disregard \`include\` and avoid running the matching tests 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -362,6 +374,8 @@ exports[`include > Test is excluded via \`skipWhen\` > Should disregard \`includ

exports[`include > There is an \`onlyd\` field > \`include\` is run as-is without modifiers > Should run the included test along with the onlyd test 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -423,6 +437,8 @@ exports[`include > There is an \`onlyd\` field > \`include\` is run as-is withou

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a boolean > when \`false\` > Should skip run included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -488,6 +504,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a boolean > when \`true\` > Should run included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -558,6 +576,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a function > Callback evaluation > Should evaluate per test run 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -610,6 +630,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a function > when returning\`false\` > Should skip run included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -675,6 +697,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a function > when returning \`true\` > Should run included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -745,6 +769,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a string > \`when\` param is a name of a non-included field > Should avoid running the included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -810,6 +836,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a string > \`when\` param is a name of a skipped field > Should avoid running the included field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
Expand Down Expand Up @@ -875,6 +903,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > There is an \`onlyd\` field > include().when() > \`when\` param is a string > \`when\` param is a name of an onlyd field > Should run included field along with the onlyd field 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -945,6 +975,8 @@ exports[`include > There is an \`onlyd\` field > include().when() > \`when\` par

exports[`include > When no \`skip\` or \`only\` > include has no effect 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down Expand Up @@ -1006,6 +1038,8 @@ exports[`include > When no \`skip\` or \`only\` > include has no effect 1`] = `

exports[`include > When no \`skip\` or \`only\` > include().when has no effect 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 2,
"errors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`Top Level Focus > Top Level Skip > When passing false > Should run all fields 1`] = `
Promise {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"dump": [Function],
"errorCount": 3,
Expand Down Expand Up @@ -84,6 +86,8 @@ Promise {

exports[`Top Level Focus > Top Level Skip > When passing undefined > Should run all fields 1`] = `
Promise {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"dump": [Function],
"errorCount": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ exports[`staticSuite > dump > should output a dump of the suite 1`] = `
"key": null,
"keys": null,
"output": {
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 3,
"errors": [
Expand Down
15 changes: 14 additions & 1 deletion packages/vest/src/suite/runCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { isArray, callEach } from 'vest-utils';

import { useDoneCallbacks, useFieldCallbacks } from 'Runtime';
import {
useAfterEachCallbacks,
useDoneCallbacks,
useFieldCallbacks,
} from 'Runtime';
import { TFieldName } from 'SuiteResultTypes';
import { SuiteWalker } from 'SuiteWalker';

Expand All @@ -26,3 +30,12 @@ export function useRunDoneCallbacks() {
const [doneCallbacks] = useDoneCallbacks();
callEach(doneCallbacks);
}

export function useRunAfterEachCallbacks(fieldName: TFieldName) {
if (SuiteWalker.useHasRemainingWithTestNameMatching(fieldName)) {
return;
}

const [afterEachCallbacks] = useAfterEachCallbacks();
callEach(afterEachCallbacks);
}
4 changes: 3 additions & 1 deletion packages/vest/src/suiteResult/SuiteResultTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Maybe, Nullable } from 'vest-utils';
import { CB, Maybe, Nullable } from 'vest-utils';

import { Severity } from 'Severity';
import { SummaryFailure } from 'SummaryFailure';
Expand Down Expand Up @@ -55,6 +55,8 @@ export type SuiteRunResult<
F extends TFieldName,
G extends TGroupName,
> = SuiteResult<F, G> & {
after: Done<F, G>;
afterEach: CB;
done: Done<F, G>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`suite() > exposed methods > Should have all exposed methods 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 0,
"errors": [],
Expand Down
Loading

0 comments on commit 88df14c

Please sign in to comment.