Skip to content

Commit 9e96ca6

Browse files
authored
FEAT: get and reset as properties of createSuite result (#330)
1 parent c1846d1 commit 9e96ca6

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

packages/vest/src/core/createSuite/index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { OPERATION_MODE_STATEFUL } from '../../constants';
2+
import { get } from '../../hooks';
23
import runWithContext from '../../lib/runWithContext';
34
import validateSuiteParams from '../../lib/validateSuiteParams';
45
import Context from '../Context';
56
import produce from '../produce';
67
import { getSuite } from '../state';
78
import getSuiteState from '../state/getSuiteState';
89
import registerSuite from '../state/registerSuite';
10+
import reset from '../state/reset';
911
import mergeExcludedTests from '../test/lib/mergeExcludedTests';
1012

1113
/**
@@ -41,7 +43,7 @@ const createSuite = (name, tests) => {
4143
// returns validator function
4244
// and sets the function name
4345
// to the name of the suite
44-
return Object.defineProperty(
46+
return Object.defineProperties(
4547
(...args) => {
4648
const output = runWithContext(ctxRef, context => {
4749
registerSuite();
@@ -53,8 +55,17 @@ const createSuite = (name, tests) => {
5355
});
5456
return output;
5557
},
56-
'name',
57-
{ value: name }
58+
{
59+
name: {
60+
value: name,
61+
},
62+
get: {
63+
value: () => get(name),
64+
},
65+
reset: {
66+
value: () => reset(name),
67+
},
68+
}
5869
);
5970
};
6071

packages/vest/src/core/createSuite/spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import faker from 'faker';
22
import { noop } from 'lodash';
33
import mock from '../../../../../shared/testUtils/mock';
44
import resetState from '../../../testUtils/resetState';
5+
import { dummyTest } from '../../../testUtils/testDummy';
56
import { OPERATION_MODE_STATELESS } from '../../constants';
7+
import { get } from '../../hooks';
68
import runWithContext from '../../lib/runWithContext';
79
import { getSuite } from '../state';
810
import createSuite from '.';
@@ -94,6 +96,25 @@ describe('Test createSuite module', () => {
9496
expect(getSuite(suiteId)).toMatchSnapshot();
9597
});
9698

99+
it('Should be able to get the suite from the result of createSuite', () => {
100+
const testsCb = jest.fn();
101+
const suiteId = 'test_get_suite';
102+
expect(createSuite(suiteId, testsCb).get()).toBe(get(suiteId));
103+
});
104+
105+
it('Should be able to reset the suite from the result of createSuite', () => {
106+
const suiteId = 'test_reset_suite';
107+
const testSuite = createSuite(suiteId, () => {
108+
dummyTest.failing('f1', 'm1');
109+
});
110+
testSuite();
111+
expect(get(suiteId).hasErrors()).toBe(true);
112+
expect(get(suiteId).testCount).toBe(1);
113+
testSuite.reset();
114+
expect(get(suiteId).hasErrors()).toBe(false);
115+
expect(get(suiteId).testCount).toBe(0);
116+
});
117+
97118
it('Should return without calling tests callback', () => {
98119
const validate = runCreateSuite();
99120
expect(testsCb).not.toHaveBeenCalled();

packages/vest/src/vest.d.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ declare module 'vest' {
313313
memo(fieldName: string, testFn: TestCB, dependencies: any[]): VestTest;
314314
}
315315

316+
interface ICreateResult {
317+
(...args: any[]): IVestResult;
318+
get: Vest['get'];
319+
reset: Vest['reset'];
320+
}
321+
316322
interface Vest {
317323
test: ITest;
318324
enforce: IEnforce;
@@ -367,10 +373,7 @@ declare module 'vest' {
367373
*
368374
* const res = validate({username: 'example'});
369375
*/
370-
create(
371-
suiteName: string,
372-
tests: (...args: any[]) => void
373-
): (...args: any[]) => IVestResult;
376+
create(suiteName: string, tests: (...args: any[]) => void): ICreateResult;
374377

375378
/**
376379
* Allows grouping tests so you can handle them together

0 commit comments

Comments
 (0)