Skip to content

Commit 2582c43

Browse files
authored
types: group and skip types (#258)
1 parent 62966d9 commit 2582c43

File tree

1 file changed

+131
-12
lines changed

1 file changed

+131
-12
lines changed

packages/vest/vest.d.ts

+131-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare module 'vest' {
22
type TestCB = () => void | Promise<string | void> | false;
33
type DraftResult = Omit<IVestResult, 'done'>;
44
type DoneCB = (res: DraftResult) => void;
5+
type ExclusionArg = string | string[] | void;
56

67
interface VestTest {
78
failed: boolean;
@@ -14,7 +15,57 @@ declare module 'vest' {
1415
asyncTest?: Promise<string | void>;
1516
}
1617

17-
interface Test {
18+
interface ISkip {
19+
/**
20+
* Skips provided field from current run.
21+
* When undefined, the expression is ignored.
22+
* @param [fieldName] Field name or a list of fields to exclude
23+
*
24+
* @example
25+
*
26+
* vest.skip('username');
27+
* vest.skip(['username', 'password']);
28+
*/
29+
(fieldName?: ExclusionArg): void;
30+
/**
31+
* Skips provided group from current run.
32+
* When undefined, the expression is ignored.
33+
* @param [groupName] group name or a list of groups to exclude.
34+
*
35+
* @example
36+
*
37+
* vest.skip.group('username');
38+
* vest.skip.group(['username', 'password']);
39+
*/
40+
group(groupName?: ExclusionArg): void;
41+
}
42+
43+
interface IOnly {
44+
/**
45+
* Singles out a field name to be tested.
46+
* When fieldName is undefined, the expression is ignored.
47+
* @param [fieldName] Field name or a list of fields to include
48+
*
49+
* @example
50+
*
51+
* vest.only('username');
52+
* vest.only(['username', 'password']);
53+
*/
54+
(fieldName?: ExclusionArg): void;
55+
/**
56+
* Singles out a group name to be tested.
57+
* When groupName is undefined, the expression is ignored.
58+
* @param [groupName] Field name or a list of groups to include.
59+
*
60+
* @example
61+
*
62+
* vest.only.group('username');
63+
* vest.only.group(['username', 'password']);
64+
*/
65+
group(groupName?: ExclusionArg): void;
66+
}
67+
68+
interface ITest {
1869
/**
1970
* Runs a single test
2071
* @param fieldName The name of the field being validated
@@ -71,26 +122,80 @@ declare module 'vest' {
71122
memo(fieldName: string, testFn: TestCB, dependencies: any[]): VestTest;
72123
}
73124

125+
interface IEnforce {
126+
/**
127+
* Assertion function. Throws an error on failure.
128+
* @param value Value being enforced
129+
*/
130+
(value: any): IEnforceRules;
131+
132+
/**
133+
* Creates a new enforce instance with custom rules
134+
* @param rules Rules object to add onto enforce
135+
*
136+
* @example
137+
*
138+
* const customEnforce = enforce.extend({
139+
* isValidEmail: (email) => email.includes('@')
140+
* });
141+
*
142+
* customEnforce('notAnEmail') // throws an error
143+
*/
144+
extend<
145+
T extends { [key: string]: (value: any, ...args: any[]) => boolean }
146+
>(
147+
obj: T
148+
): (value: any) => IEnforceRules<T> & EnforceExtendMap<T>;
149+
}
150+
74151
interface Vest {
75-
test: Test;
152+
test: ITest;
153+
enforce: IEnforce;
154+
only: IOnly;
155+
skip: ISkip;
76156

77157
/**
78158
* Runs a stateless validation suite.
79159
* @param suiteName Unique suite name.
80160
* @param tests Suite body.
161+
*
162+
* const res = validate('form_name', () => {
163+
* // your tests go here
164+
* });
81165
*/
82166
validate(suiteName: string, tests: () => void): IVestResult;
83167

84168
/**
85169
* Runs a stateful validation suite.
86170
* @param suiteName Unique suite name.
87171
* @param tests Suite body.
172+
*
173+
* @example
174+
*
175+
* const validate = vest.create('form_name', (data = {}) => {
176+
* // your tests go here
177+
* });
178+
*
179+
* const res = validate({username: 'example'});
88180
*/
89181
create(
90182
suiteName: string,
91183
tests: (...args: any[]) => void
92184
): (...args: any[]) => IVestResult;
93185

186+
/**
187+
* Allows grouping tests so you can handle them together
188+
* @param groupName Group name.
189+
* @param testFn Group body.
190+
*
191+
* @example
192+
*
193+
* group('sign_up', () => {
194+
* // your tests go here
195+
* });
196+
*/
197+
group(groupName: string, testFn: () => void): void;
198+
94199
/**
95200
* Marks a test as warning.
96201
* Needs to run within a test body.
@@ -110,11 +215,6 @@ declare module 'vest' {
110215
*/
111216
draft(): DraftResult;
112217
VERSION: string;
113-
/**
114-
* Assertion function. Throws an error on failure.
115-
* @param value Value being enforced
116-
*/
117-
enforce(value: any): IEnforceRules;
118218
}
119219

120220
interface IVestResult {
@@ -282,14 +382,33 @@ declare module 'vest' {
282382
lengthNotEquals: (
283383
expected: number
284384
) => IEnforceRules<T> & EnforceExtendMap<T>;
285-
extend: <T extends { [key: string]: (...args: any[]) => boolean }>(
286-
obj: T
287-
) => IEnforceRules<T> & EnforceExtendMap<T>;
288385
}
289386

290-
const { test, validate, create, warn, draft, VERSION, enforce }: Vest;
387+
const {
388+
test,
389+
validate,
390+
create,
391+
warn,
392+
draft,
393+
VERSION,
394+
enforce,
395+
skip,
396+
only,
397+
group,
398+
}: Vest;
291399

292-
export { test, validate, create, warn, draft, VERSION, enforce };
400+
export {
401+
test,
402+
validate,
403+
create,
404+
warn,
405+
draft,
406+
VERSION,
407+
enforce,
408+
skip,
409+
only,
410+
group,
411+
};
293412

294413
const vest: Vest;
295414

0 commit comments

Comments
 (0)