1 | // Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
|
2 | // Node module: @loopback/testlab
|
3 | // This file is licensed under the MIT License.
|
4 | // License text available at https://opensource.org/licenses/MIT
|
5 |
|
6 | /**
|
7 | * A function defining a new test case or a test suite, e.g. `it` or `describe`.
|
8 | */
|
9 | export type TestDefinition<ARGS extends unknown[], RETVAL> = (
|
10 | name: string,
|
11 | ...args: ARGS
|
12 | ) => RETVAL;
|
13 |
|
14 | /**
|
15 | * Helper function for skipping tests when a certain condition is met.
|
16 | *
|
17 | * @example
|
18 | * ```ts
|
19 | * skipIf(
|
20 | * !features.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 |
|
49 | /**
|
50 | * Helper function for skipping tests on Travis CI.
|
51 | *
|
52 | * @example
|
53 | *
|
54 | * ```ts
|
55 | * skipOnTravis(it, 'does something when some condition', async () => {
|
56 | * // the test
|
57 | * });
|
58 | * ```
|
59 | *
|
60 | * @param verb - The function to invoke to define the test case or the test
|
61 | * suite, e.g. `it` or `describe`.
|
62 | * @param name - The test name (the first argument of `verb` function).
|
63 | * @param args - Additional arguments (framework specific), typically a function
|
64 | * implementing the test.
|
65 | */
|
66 | export function skipOnTravis<ARGS extends unknown[], RETVAL>(
|
67 | verb: TestDefinition<ARGS, RETVAL> & {skip: TestDefinition<ARGS, RETVAL>},
|
68 | name: string,
|
69 | ...args: ARGS
|
70 | ): RETVAL {
|
71 | if (process.env.TRAVIS) {
|
72 | return verb.skip(`[SKIPPED ON TRAVIS] ${name}`, ...args);
|
73 | } else {
|
74 | return verb(name, ...args);
|
75 | }
|
76 | }
|