Skip to content

Commit

Permalink
patch(vest): Throw error when tests are called in the wrong order
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 032df1b commit aae250a
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 813 deletions.
2 changes: 1 addition & 1 deletion packages/n4s/src/runtime/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export default function rules() {
inside,
isArray,
isBetween,
isBoolean,
isBlank,
isBoolean,
isEmpty,
isEven,
isFalsy,
Expand Down
9 changes: 9 additions & 0 deletions packages/shared/src/throwError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ export default function throwError(
__DEV__ ? devMessage : defaultTo(productionMessage, devMessage)
);
}

export function throwErrorDeferred(
devMessage?: string,
productionMessage?: string
): void {
setTimeout(() => {
throwError(devMessage, productionMessage);
}, 0);
}
11 changes: 0 additions & 11 deletions packages/vest/src/core/state/stateHooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import createCache from 'cache';
import defaultTo from 'defaultTo';
import type { TStateHandlerReturn } from 'vast';

import VestTest from 'VestTest';
Expand Down Expand Up @@ -91,16 +90,6 @@ export function useOmittedFields(): Record<string, true> {
);
}

export function useTestAtCursor(initialValue: VestTest): VestTest {
const [cursorAt] = useCursorAt();
const [prevTestObjects] = usePrevTestObjects();

const nextTest = defaultTo(prevTestObjects[cursorAt], initialValue);
useSetTestAtCursor(nextTest);

return nextTest;
}

const incompleteCache = createCache();
export function useAllIncomplete(): VestTest[] {
const [testObjects] = useTestObjects();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import wait from 'wait';

import itWithContext from '../../../../../testUtils/itWithContext';
import itWithContext from '../../../../testUtils/itWithContext';

import VestTest from 'VestTest';
import { useAllIncomplete, useTestObjects } from 'stateHooks';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { dummyTest } from '../../../../testUtils/testDummy';

import { create, skipWhen } from 'vest';

describe('Merging of previous test runs', () => {
let suite;
let counter = 0;
let testContainer = [];

beforeEach(() => {
counter = 0;
testContainer = [];
});
describe('When test skipped in subsequent run', () => {
it('Should merge its result from previous runs', () => {
suite = create(() => {
skipWhen(counter === 1, () => {
testContainer.push([
dummyTest.failing('f1'),
dummyTest.failing('f2'),
dummyTest.passing('f3'),
dummyTest.failingWarning('f5'),
dummyTest.passingWarning('f6'),
]);
});
counter++;
});

const resA = suite();
const resB = suite();

const [testsA, testsB] = testContainer;

// This checks the the suite result is the same for both runs
expect(resA).isDeepCopyOf(resB);

// This checks that the test objects are the same for both runs
expect(testsA).toEqual(testsB);
});
});

describe('When test changes in subsequent run', () => {
it('Should update the result accordingly', () => {
suite = create(() => {
testContainer.push(
counter === 0 ? dummyTest.passing('f1') : dummyTest.failing('f1')
);

dummyTest.failing('f2');
counter++;
});

const resA = suite();

// Checking that the result is correct
expect(resA.isValid('f1')).toBe(true);
expect(resA.isValid('f2')).toBe(false);
const resB = suite();
// Checking that the result is correct
expect(resB.isValid('f1')).toBe(false);
expect(resA.isValid('f2')).toBe(false);

const [f1A, f1B] = testContainer;

// Checking that the result updated
expect(resA).not.isDeepCopyOf(resB);

// Checking that the test updated
expect(f1A).not.toBe(f1B);
});
});

describe('When tests are passed in a different order between tests', () => {
let throwErrorDeferred, vest;
beforeEach(() => {
throwErrorDeferred = jest.fn();
jest.resetModules();
jest.mock('throwError', () => ({
throwErrorDeferred,
default: jest.fn(),
}));
vest = require('vest');
});

afterEach(() => {
jest.resetAllMocks();
});

it('Should defer-throw an error', () => {
const { create, test } = vest;
suite = create(() => {
testContainer.push(
counter === 0 ? test('f1', jest.fn()) : test('f2', () => false)
);
counter++;
});

suite();
expect(throwErrorDeferred).not.toHaveBeenCalled();

suite();

expect(throwErrorDeferred).toHaveBeenCalledWith(
expect.stringContaining(
'Vest Critical Error: Tests called in different order than previous run.'
)
);
});
});
});
Loading

0 comments on commit aae250a

Please sign in to comment.