Skip to content

Commit

Permalink
test: add dummy test helper (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 27, 2020
1 parent 82a13cb commit ed58c37
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 23 deletions.
24 changes: 10 additions & 14 deletions packages/vest/src/core/produce/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import resetState from '../../../testUtils/resetState';
import runRegisterSuite from '../../../testUtils/runRegisterSuite';
import runSpec from '../../../testUtils/runSpec';
import suiteIdByName from '../../../testUtils/suiteIdByName';
import testDummy from '../../../testUtils/testDummy';
import getSuiteState from '../state/getSuiteState';
import hasRemainingTests from '../state/hasRemainingTests';
import patch from '../state/patch';
Expand Down Expand Up @@ -36,26 +37,21 @@ let suiteId, state, produced;
const KEPT_PROPERTIES = ['errorCount', 'warnCount', 'tests', 'name'];

runSpec(vest => {
const { test, create } = vest;
const { create } = vest;

const runCreateSuite = suiteName =>
create(suiteName, () => {
suiteId = suiteIdByName(suiteName);

vest.skip(SKIPPED_FIELD);
test('field_1', 'statement_string_1', () => false);
test(SKIPPED_FIELD, 'statement_string_2', () => {});
test('field_3', 'statement_string_3', () => {
vest.warn();
return false;
});
testDummy(vest).failing('field_1');
testDummy(vest).passing(SKIPPED_FIELD);
testDummy(vest).failingWarning('field_3');

test('field_4', 'statement_string_4', () => {
vest.warn();
});
testDummy(vest).passingWarning('field_4');

test('field_5', 'statement_string_5', () => false);
test('field_5', 'statement_string_6', () => false);
testDummy(vest).failing('field_5');
testDummy(vest).failing('field_5');
})();

describe('module: produce', () => {
Expand Down Expand Up @@ -323,8 +319,8 @@ runSpec(vest => {
const runCreateSuite = () =>
create(suiteName, () => {
suiteId = suiteIdByName(suiteName);
test('field_1', 'statement_string_1', () => Promise.reject());
test('sync_field_2', 'statement_string_2', () => {});
testDummy().failingAsync('field_1');
testDummy().passing('sync_field_2');
})();

describe('When field is async', () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/vest/src/core/state/reset/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getState, getSuites } from '..';
import resetState from '../../../../testUtils/resetState';
import runRegisterSuite from '../../../../testUtils/runRegisterSuite';
import runSpec from '../../../../testUtils/runSpec';
import testDummy from '../../../../testUtils/testDummy';
import vest from '../../../index';
import copy from '../../../lib/copy';
import getSuiteState from '../getSuiteState';
Expand All @@ -23,21 +24,16 @@ const spec = _vest => {

const createSuite = () => {
const vest = _vest;
const { test } = vest;
return vest.create(suiteId, skip => {
vest.skip(skip);

test('field_1', () =>
new Promise((res, reject) => setTimeout(reject, 250)));
testDummy(vest).failingAsync('field_1', { time: 250 });

test('field_2', () =>
new Promise((res, reject) => setTimeout(reject, 250)));
testDummy(vest).failingAsync('field_2', { time: 250 });

test('field_3', () =>
new Promise((res, reject) => setTimeout(reject, 250)));
testDummy(vest).failingAsync('field_3', { time: 250 });

test('field_4', () =>
new Promise((res, reject) => setTimeout(reject, 250)));
testDummy(vest).failingAsync('field_4', { time: 250 });
});
};

Expand Down
99 changes: 99 additions & 0 deletions packages/vest/testUtils/testDummy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable jest/no-export */
import faker from 'faker';
import vest from '../../src';

const testDummy = (vestRef = vest) => {
const { test } = vestRef;
const failing = (
name = faker.random.word(),
statement = faker.random.words()
) =>
test(name, statement, () => {
throw new Error();
});

const failingWarning = (
name = faker.random.word(),
statement = faker.random.words()
) =>
test(name, statement, () => {
vest.warn();
throw new Error();
});

const passing = (
name = faker.random.word(),
statement = faker.random.words()
) => test(name, statement, Function.prototype);

const passingWarning = (
name = faker.random.word(),
statement = faker.random.words()
) =>
test(name, statement, () => {
vest.warn();
});

const failingAsync = (
name = faker.random.word(),
{ statement, time = 0 } = {}
) =>
test(
name,
statement,
() =>
new Promise((resolve, reject) => {
setTimeout(reject, time);
})
);

const failingWarningAsync = (
name = faker.random.word(),
{ statement, time = 0 } = {}
) =>
test(name, statement, () => {
vest.warn();
return new Promise((resolve, reject) => {
setTimeout(reject, time);
});
});

const passingAsync = (
name = faker.random.word(),
{ statement, time = 0 } = {}
) =>
test(
name,
statement,
() =>
new Promise(resolve => {
setTimeout(resolve, time);
})
);

const passingWarningAsync = (
name = faker.random.word(),
{ statement, time = 0 } = {}
) =>
test(name, statement, () => {
vest.warn();
return new Promise(resolve => {
setTimeout(resolve, time);
});
});

return {
failing,
passing,
failingAsync,
passingAsync,
failingWarning,
failingWarningAsync,
passingWarning,
passingWarningAsync,
};
};

export default testDummy;

export const dummyTest = testDummy();

0 comments on commit ed58c37

Please sign in to comment.