Skip to content

Commit 3221d9f

Browse files
committed
feat(testlab): add generic helper skipOnTravis
Introduce a generic helper `skipOnTravis` that supports arbitrary BDD verbs (`it`, `describe`, etc.) in a test-framework-agnostic way. Rework `itSkippedOnTravis` to leverage the new helper and deprecate it (in tsdocs only). Signed-off-by: Miroslav Bajtoš <[email protected]>
1 parent dfe71db commit 3221d9f

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

packages/http-server/src/__tests__/integration/http-server.integration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
givenHttpServerConfig,
99
httpGetAsync,
1010
httpsGetAsync,
11-
itSkippedOnTravis,
11+
skipOnTravis,
1212
supertest,
1313
} from '@loopback/testlab';
1414
import * as fs from 'fs';
@@ -22,7 +22,7 @@ describe('HttpServer (integration)', () => {
2222

2323
afterEach(stopServer);
2424

25-
itSkippedOnTravis('formats IPv6 url correctly', async () => {
25+
skipOnTravis(it, 'formats IPv6 url correctly', async () => {
2626
server = new HttpServer(dummyRequestHandler, {
2727
host: '::1',
2828
} as HttpOptions);
@@ -164,7 +164,7 @@ describe('HttpServer (integration)', () => {
164164
expect(response.statusCode).to.equal(200);
165165
});
166166

167-
itSkippedOnTravis('supports HTTP over IPv6', async () => {
167+
skipOnTravis(it, 'supports HTTP over IPv6', async () => {
168168
server = new HttpServer(dummyRequestHandler, {host: '::1'});
169169
await server.start();
170170
expect(getAddressFamily(server)).to.equal('IPv6');
@@ -190,7 +190,7 @@ describe('HttpServer (integration)', () => {
190190
expect(response.statusCode).to.equal(200);
191191
});
192192

193-
itSkippedOnTravis('handles IPv6 loopback address in HTTPS', async () => {
193+
skipOnTravis(it, 'handles IPv6 loopback address in HTTPS', async () => {
194194
const httpsServer: HttpServer = givenHttpsServer({
195195
host: '::1',
196196
});

packages/rest/src/__tests__/integration/rest.server.integration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
expect,
1111
givenHttpServerConfig,
1212
httpsGetAsync,
13-
itSkippedOnTravis,
13+
skipOnTravis,
1414
supertest,
1515
} from '@loopback/testlab';
1616
import * as fs from 'fs';
@@ -705,7 +705,7 @@ paths:
705705
await server.stop();
706706
});
707707

708-
itSkippedOnTravis('handles IPv6 loopback address in HTTPS', async () => {
708+
skipOnTravis(it, 'handles IPv6 loopback address in HTTPS', async () => {
709709
const keyPath = path.join(FIXTURES, 'key.pem');
710710
const certPath = path.join(FIXTURES, 'cert.pem');
711711
const server = await givenAServer({
@@ -726,7 +726,7 @@ paths:
726726
});
727727

728728
// https://github.com/strongloop/loopback-next/issues/1623
729-
itSkippedOnTravis('handles IPv6 address for API Explorer UI', async () => {
729+
skipOnTravis(it, 'handles IPv6 address for API Explorer UI', async () => {
730730
const keyPath = path.join(FIXTURES, 'key.pem');
731731
const certPath = path.join(FIXTURES, 'cert.pem');
732732
const server = await givenAServer({

packages/testlab/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +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-
- [itSkippedOnTravis](#itskippedontravis) - Skip tests on Travis env.
50+
- [skipOnTravis](#skipontravis) - Skip tests on Travis env.
5151
- [createRestAppClient](#createrestappclient) - Create a supertest client
5252
connected to a running RestApplication.
5353
- [givenHttpServerConfig](#givenhttpserverconfig) - Generate HTTP server config.
@@ -82,10 +82,21 @@ by Shot in your unit tests:
8282
- Code modifying core HTTP Response, including full request/response handlers
8383
- Code parsing Express HTTP Request or modifying Express HTTP Response
8484

85-
### `itSkippedOnTravis`
85+
### `skipOnTravis`
8686

8787
Helper function for skipping tests on Travis environment. If you need to skip
88-
testing on Travis for any reason, use this instead of Mocha's `it`.
88+
testing on Travis for any reason, use this helper together with `it` or
89+
`describe`.
90+
91+
```ts
92+
skipOnTravis(it, 'does something when some condition', async () => {
93+
// the test code
94+
});
95+
```
96+
97+
Under the hood, `skipOnTravis` invokes the provided test verb by default (e.g.
98+
`it`). When the helper detects Travis CI environment variables, then it calls
99+
`.skip` instead (e.g. `it.skip`).
89100

90101
### `createRestAppClient`
91102

packages/testlab/src/skip-travis.ts

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,47 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

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 on Travis CI.
16+
*
17+
* @example
18+
*
19+
* ```ts
20+
* skipOnTravis(it, 'does something when some condition', async () => {
21+
* // the test
22+
* });
23+
* ```
24+
*
25+
* @param verb - The function to invoke to define the test case or the test
26+
* suite, e.g. `it` or `describe`.
27+
* @param name - The test name (the first argument of `verb` function).
28+
* @param args - Additional arguments (framework specific), typically a function
29+
* implementing the test.
30+
*/
31+
export function skipOnTravis<ARGS extends unknown[], RETVAL>(
32+
verb: TestDefinition<ARGS, RETVAL> & {skip: TestDefinition<ARGS, RETVAL>},
33+
name: string,
34+
...args: ARGS
35+
): RETVAL {
36+
if (process.env.TRAVIS) {
37+
return verb.skip(`[SKIPPED ON TRAVIS] ${name}`, ...args);
38+
} else {
39+
return verb(name, ...args);
40+
}
41+
}
42+
43+
/*** LEGACY API FOR BACKWARDS COMPATIBILITY ***/
44+
45+
// TODO(semver-major) remove this code
46+
647
// Simplified test function type from Mocha
748
export interface TestFn {
849
(this: TestContext): PromiseLike<unknown>;
@@ -18,21 +59,19 @@ export interface TestContext {
1859
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1960
[index: string]: any;
2061
}
21-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22-
export type TestCallbackRetval = void | PromiseLike<any>;
2362

2463
/**
25-
* Helper function for skipping tests on Travis env
26-
* @param expectation
27-
* @param callback
64+
* Helper function for skipping tests on Travis env - legacy variant
65+
* supporting `it` only.
66+
*
67+
* @param expectation - The test name (the first argument of `it` function).
68+
* @param callback - The test function (the second argument of `it` function).
69+
*
70+
* @deprecated Use `skipOnTravis(it, name, fn)` instead.
2871
*/
2972
export function itSkippedOnTravis(
3073
expectation: string,
3174
callback?: TestFn,
3275
): void {
33-
if (process.env.TRAVIS) {
34-
it.skip(`[SKIPPED ON TRAVIS] ${expectation}`, callback);
35-
} else {
36-
it(expectation, callback);
37-
}
76+
skipOnTravis(it, expectation, callback);
3877
}

0 commit comments

Comments
 (0)