Skip to content

Commit

Permalink
patch(vest): use state property in test instead of state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 11, 2023
1 parent bca9421 commit 249c599
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/vest/src/core/isolate/IsolateTest/IsolateTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Maybe, seq } from 'vest-utils';
import { Isolate, IsolateKey, IsolateMutator } from 'vestjs-runtime';

import { createTestStateMachine } from 'IsolateTestStateMachine';
import { TestStatus } from 'IsolateTestStateMachine';
import { TestSeverity } from 'Severity';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { TestFn, AsyncTest } from 'TestTypes';
Expand Down Expand Up @@ -29,7 +29,7 @@ export class IsolateTest<
id = seq();
severity = TestSeverity.Error;
type = VestIsolateType.Test;
stateMachine = createTestStateMachine();
status: TestStatus = TestStatus.UNTESTED;

constructor({
fieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export enum TestAction {
RESET = 'RESET',
}

type TAction = TestAction | TestStatus;
export type TestStateMachineAction = TestAction | TestStatus;

export function createTestStateMachine() {
return StateMachine<TestStatus, TAction>(machine);
return StateMachine<TestStatus, TestStateMachineAction>(machine);
}

/* eslint-disable sort-keys */
const machine: TStateMachine<TestStatus, TAction> = {
const machine: TStateMachine<TestStatus, TestStateMachineAction> = {
initial: TestStatus.UNTESTED,
states: {
'*': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ export class VestTestInspector {
}

static statusEquals(test: IsolateTest, status: TestStatus): boolean {
return test.stateMachine.getState() === status;
return test.status === status;
}
}
32 changes: 24 additions & 8 deletions packages/vest/src/core/isolate/IsolateTest/VestTestMutator.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { IsolateTest } from 'IsolateTest';
import { TestAction, TestStatus } from 'IsolateTestStateMachine';
import {
TestAction,
TestStateMachineAction,
TestStatus,
createTestStateMachine,
} from 'IsolateTestStateMachine';
import { TestSeverity } from 'Severity';
import { VestTestInspector } from 'VestTestInspector';

const TestStateMachine = createTestStateMachine();

export class VestTestMutator {
static setPending(test: IsolateTest) {
test.stateMachine.transition(TestStatus.PENDING);
VestTestMutator.setStatus(test, TestStatus.PENDING);
}

static fail(test: IsolateTest): void {
test.stateMachine.transition(
VestTestMutator.setStatus(
test,
VestTestInspector.warns(test) ? TestStatus.WARNING : TestStatus.FAILED
);
}

static pass(test: IsolateTest): void {
test.stateMachine.transition(TestStatus.PASSING);
VestTestMutator.setStatus(test, TestStatus.PASSING);
}

static warn(test: IsolateTest): void {
Expand All @@ -33,18 +41,26 @@ export class VestTestMutator {
// is when we specifically skip a test with `skipWhen`, which is handled by the
// "force" boolean flag.
// I am not a fan of this flag, but it gets the job done.
test.stateMachine.transition(TestStatus.SKIPPED, force);
VestTestMutator.setStatus(test, TestStatus.SKIPPED, force);
}

static cancel(test: IsolateTest): void {
test.stateMachine.transition(TestStatus.CANCELED);
VestTestMutator.setStatus(test, TestStatus.CANCELED);
}

static omit(test: IsolateTest): void {
test.stateMachine.transition(TestStatus.OMITTED);
VestTestMutator.setStatus(test, TestStatus.OMITTED);
}

static reset(test: IsolateTest): void {
test.stateMachine.transition(TestAction.RESET);
VestTestMutator.setStatus(test, TestAction.RESET);
}

static setStatus(
test: IsolateTest,
status: TestStateMachineAction,
payload?: any
): void {
test.status = TestStateMachine.transitionFrom(test.status, status, payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ IsolateTest {
"message": "I am Root.",
"parent": null,
"severity": "error",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "UNTESTED",
"testFn": [MockFunction],
"type": "Test",
}
Expand All @@ -29,10 +26,7 @@ IsolateTest {
"message": "I am Root.",
"parent": null,
"severity": "warning",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "UNTESTED",
"testFn": [MockFunction],
"type": "Test",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ IsolateTest {
"type": "Suite",
},
"severity": "error",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "PASSING",
"testFn": [Function],
"type": "Test",
}
Expand Down Expand Up @@ -117,10 +114,7 @@ IsolateTest {
"type": "Suite",
},
"severity": "error",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "PASSING",
"testFn": [Function],
"type": "Test",
}
Expand Down Expand Up @@ -180,10 +174,7 @@ IsolateTest {
"type": "Suite",
},
"severity": "error",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "PASSING",
"testFn": [Function],
"type": "Test",
}
Expand Down Expand Up @@ -241,10 +232,7 @@ IsolateTest {
"type": "Suite",
},
"severity": "error",
"stateMachine": {
"getState": [Function],
"transition": [Function],
},
"status": "PASSING",
"testFn": [Function],
"type": "Test",
}
Expand Down

0 comments on commit 249c599

Please sign in to comment.