Skip to content

Commit

Permalink
fix(vest): support nested skipWhen when true->false
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Feb 8, 2022
1 parent 6d8cbcb commit ea76261
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`skipWhen Should pass result draft to the functional condition 1`] = `
Object {
"errorCount": 0,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 0,
"tests": Object {},
"warnCount": 0,
}
`;

exports[`skipWhen Should pass result draft to the functional condition 2`] = `
Object {
"errorCount": 1,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 1,
"tests": Object {
"f1": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
},
"warnCount": 0,
}
`;

exports[`skipWhen Should pass result draft to the functional condition 3`] = `
Object {
"errorCount": 2,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"isValid": [Function],
"suiteName": undefined,
"testCount": 2,
"tests": Object {
"f1": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
"f2": Object {
"errorCount": 1,
"errors": Array [
"msg",
],
"testCount": 1,
"warnCount": 0,
},
},
"warnCount": 0,
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vest from 'vest';
import { dummyTest } from '../../../../../testUtils/testDummy';

import { dummyTest } from '../../../testUtils/testDummy';
import * as vest from 'vest';

describe('skipWhen', () => {
let fn = jest.fn();
Expand Down Expand Up @@ -89,6 +89,69 @@ describe('skipWhen', () => {

expect(suite.get().tests.username.testCount).toBe(1);
});

describe('nested calls', () => {
let suite;

describe('skipped in non-skipped', () => {
beforeEach(() => {
suite = vest.create(() => {
vest.skipWhen(false, () => {
vest.test('outer', () => false);

vest.skipWhen(true, () => {
vest.test('outer', () => false);
});
});
});
suite();
});
it('Should run `outer` and skip `inner`', () => {
expect(suite.get().testCount).toBe(1);
expect(suite.get().hasErrors('outer')).toBe(true);
expect(suite.get().hasErrors('inner')).toBe(false);
});
});

describe('skipped in skipped', () => {
beforeEach(() => {
suite = vest.create(() => {
vest.skipWhen(true, () => {
vest.test('outer', () => false);

vest.skipWhen(true, () => {
vest.test('outer', () => false);
});
});
});
suite();
});
it('Should skip both `outer` and `inner`', () => {
expect(suite.get().testCount).toBe(0);
expect(suite.get().hasErrors('outer')).toBe(false);
expect(suite.get().hasErrors('inner')).toBe(false);
});
});
describe('non-skipped in skipped', () => {
beforeEach(() => {
suite = vest.create(() => {
vest.skipWhen(true, () => {
vest.test('outer', () => false);

vest.skipWhen(false, () => {
vest.test('outer', () => false);
});
});
});
suite();
});
it('Should skip both', () => {
expect(suite.get().testCount).toBe(0);
expect(suite.get().hasErrors('outer')).toBe(false);
expect(suite.get().hasErrors('inner')).toBe(false);
});
});
});
});

const suite = vest.create((skipTest: boolean) => {
Expand Down
14 changes: 10 additions & 4 deletions packages/vest/src/core/isolate/isolates/skipWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import optionalFunctionValue from 'optionalFunctionValue';

import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
import { isExcludedIndividually } from 'exclusive';
import { isolate } from 'isolate';
import { produceDraft, TDraftResult } from 'produceDraft';

Expand All @@ -21,10 +22,15 @@ export default function skipWhen(
isolate({ type: IsolateTypes.SKIP_WHEN }, () => {
ctx.run(
{
skipped: optionalFunctionValue(
conditional,
optionalFunctionValue(produceDraft)
),
skipped:
// Checking for nested conditional. If we're in a nested skipWhen,
// we should skip the test if the parent conditional is true.
isExcludedIndividually() ||
// Otherwise, we should skip the test if the conditional is true.
optionalFunctionValue(
conditional,
optionalFunctionValue(produceDraft)
),
},
() => callback()
);
Expand Down

0 comments on commit ea76261

Please sign in to comment.