Skip to content

Commit 48f3703

Browse files
committed
feat(testlab): add skipIf helper
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
1 parent 89d6586 commit 48f3703

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

packages/testlab/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Table of contents:
4747
- [sinon](#sinon) - Mocks, stubs and more.
4848
- [shot](#shot) - HTTP Request/Response stubs.
4949
- [validateApiSpec](#validateapispec) - Open API Spec validator.
50+
- [skipIf](#skipif) - Skip tests when a condition is met.
5051
- [skipOnTravis](#skipontravis) - Skip tests on Travis env.
5152
- [createRestAppClient](#createrestappclient) - Create a supertest client
5253
connected to a running RestApplication.
@@ -82,6 +83,35 @@ by Shot in your unit tests:
8283
- Code modifying core HTTP Response, including full request/response handlers
8384
- Code parsing Express HTTP Request or modifying Express HTTP Response
8485

86+
### `skipIf`
87+
88+
Helper function for skipping tests when a certain condition is met. Use this
89+
helper together with `it` or `describe`.
90+
91+
```ts
92+
skipIf(someCondition, it, 'does something', async () => {
93+
// the test code
94+
});
95+
```
96+
97+
Unfortunately, type inference does not work well for `describe`, you have to
98+
help the compiler to figure out the correct types.
99+
100+
```ts
101+
skipIf<[(this: Suite) => void], void>(
102+
someCondition,
103+
describe,
104+
'some suite name',
105+
() => {
106+
// define the test cases
107+
},
108+
);
109+
```
110+
111+
Under the hood, `skipIf` invokes the provided test verb by default (e.g. `it`).
112+
When the provided condition was true, then it calls `.skip` instead (e.g.
113+
`it.skip`).
114+
85115
### `skipOnTravis`
86116

87117
Helper function for skipping tests on Travis environment. If you need to skip

packages/testlab/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export * from './http-server-config';
1010
export * from './request';
1111
export * from './shot';
1212
export * from './sinon';
13-
export * from './skip-travis';
13+
export * from './skip';
1414
export * from './test-sandbox';
1515
export * from './to-json';
1616
export * from './validate-api-spec';

packages/testlab/src/skip-travis.ts renamed to packages/testlab/src/skip.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ export type TestDefinition<ARGS extends unknown[], RETVAL> = (
1111
...args: ARGS
1212
) => RETVAL;
1313

14+
/**
15+
* Helper function for skipping tests when a certain condition is met.
16+
*
17+
* @example
18+
* ```ts
19+
* skipIf(
20+
* !connectorFeatures.freeFormProperties,
21+
* describe,
22+
* 'free-form properties (strict: false)',
23+
* () => {
24+
* // the tests
25+
* }
26+
* );
27+
* ```
28+
*
29+
* @param skip - Should the test case/suite be skipped?
30+
* @param verb - The function to invoke to define the test case or the test
31+
* suite, e.g. `it` or `describe`.
32+
* @param name - The test name (the first argument of `verb` function).
33+
* @param args - Additional arguments (framework specific), typically a function
34+
* implementing the test.
35+
*/
36+
export function skipIf<ARGS extends unknown[], RETVAL>(
37+
skip: boolean,
38+
verb: TestDefinition<ARGS, RETVAL> & {skip: TestDefinition<ARGS, RETVAL>},
39+
name: string,
40+
...args: ARGS
41+
): RETVAL {
42+
if (skip) {
43+
return verb.skip(`[SKIPPED] ${name}`, ...args);
44+
} else {
45+
return verb(name, ...args);
46+
}
47+
}
48+
1449
/**
1550
* Helper function for skipping tests on Travis CI.
1651
*

0 commit comments

Comments
 (0)