UNPKG

3.13 kBPlain TextView Raw
1// Copyright IBM Corp. 2018,2019. 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 */
9export 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 */
36export 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 */
66export 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}
77
78/*** LEGACY API FOR BACKWARDS COMPATIBILITY ***/
79
80// TODO(semver-major) remove this code
81
82// Simplified test function type from Mocha
83export interface TestFn {
84 (this: TestContext): PromiseLike<unknown>;
85 (this: TestContext, done: Function): void;
86}
87
88// Type of "this" object provided by Mocha to test functions
89export interface TestContext {
90 skip(): this;
91 timeout(ms: number | string): this;
92 retries(n: number): this;
93 slow(ms: number): this;
94 // eslint-disable-next-line @typescript-eslint/no-explicit-any
95 [index: string]: any;
96}
97
98/**
99 * Helper function for skipping tests on Travis env - legacy variant
100 * supporting `it` only.
101 *
102 * @param expectation - The test name (the first argument of `it` function).
103 * @param callback - The test function (the second argument of `it` function).
104 *
105 * @deprecated Use `skipOnTravis(it, name, fn)` instead.
106 */
107export function itSkippedOnTravis(
108 expectation: string,
109 callback?: TestFn,
110): void {
111 skipOnTravis(it, expectation, callback);
112}