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 beddb53
Showing 17 changed files with 562 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/vest-utils/src/exports/minifyObject.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { isObject } from 'valueIsObject';
// Basic key generator that generates keys like a, b, c, ... aa, ab, ac, ...
function genMinifiedKey() {
const chars =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*';
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*_';
let index = 0;

return function next(): string {
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

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

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

exports[`Stateful behavior > Should merge skipped fields with previous values 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
@@ -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": [
@@ -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": [
9 changes: 9 additions & 0 deletions packages/vest/src/core/Runtime.ts
Original file line number Diff line number Diff line change
@@ -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[]>;
@@ -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>>;
@@ -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,
@@ -72,6 +75,10 @@ export function useFieldCallbacks() {
return useX().fieldCallbacks();
}

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

export function useSuiteName() {
return useX().suiteName;
}
@@ -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() {
7 changes: 6 additions & 1 deletion packages/vest/src/core/VestBus/VestBus.ts
Original file line number Diff line number Diff line change
@@ -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() {
@@ -45,6 +49,7 @@ export function useInitVestBus() {
const { fieldName } = VestTest.getData(isolate);

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

Original file line number Diff line number Diff line change
@@ -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": [
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

exports[`test.memo > cache hit > Should produce correct initial result 1`] = `
{
"after": [Function],
"afterEach": [Function],
"done": [Function],
"errorCount": 1,
"errors": [
@@ -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": [
Original file line number Diff line number Diff line change
@@ -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": [],
@@ -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": [],
@@ -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": [],
Original file line number Diff line number Diff line change
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
@@ -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": [
Original file line number Diff line number Diff line change
@@ -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,
@@ -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,
Original file line number Diff line number Diff line change
@@ -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": [
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';

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

Loading

0 comments on commit beddb53

Please sign in to comment.