|
16 | 16 |
|
17 | 17 | /**
|
18 | 18 | * This script can be used with `mocha --require ...` to support skipping
|
19 |
| - * tests if the current version of Node.js is too old. For example, say |
20 |
| - * a package's unit tests cannot run with Node.js 14, but the CI unit tests |
21 |
| - * run all package test with that version. |
| 19 | + * tests if the current version of Node.js is too old or too new. For example, |
| 20 | + * say a package's unit tests cannot run with Node.js 14, but the CI unit tests |
| 21 | + * run all package tests with that version. |
22 | 22 | *
|
23 | 23 | * 1. Change this in "package.json":
|
24 | 24 | * "test": "nyc mocha ...",
|
25 |
| - * to this: |
26 |
| - * "test": "SKIP_TEST_IF_NODE_OLDER_THAN=18 nyc mocha --require '../../../scripts/skip-test-if.js' ... ", |
27 |
| - * where `SKIP_TEST_IF_NODE_OLDER_THAN` indicates the minimum Node.js major |
28 |
| - * version. |
| 25 | + * to one of these: |
| 26 | + * "test": "SKIP_TEST_IF_NODE_OLDER_THAN=18 nyc mocha --require '../../scripts/skip-test-if.js' ...", |
| 27 | + * "test": "SKIP_TEST_IF_NODE_NEWER_THAN=22 nyc mocha --require '../../scripts/skip-test-if.js' ...", |
| 28 | + * where `SKIP_TEST_IF_NODE_{OLDER|NEWER}_THAN` indicates a Node.js *major* |
| 29 | + * version number. |
29 | 30 | *
|
30 |
| - * 2. ".tav.yml" blocks should set SKIP_TEST_IF_DISABLE=true to |
| 31 | + * 2. ".tav.yml" blocks should set SKIP_TEST_IF_DISABLED=true to |
31 | 32 | * disable the skipping. Via this in each test block:
|
32 | 33 | * env:
|
33 |
| - * - SKIP_TEST_IF_DISABLE=true |
| 34 | + * - SKIP_TEST_IF_DISABLED=true |
34 | 35 | */
|
35 | 36 |
|
36 | 37 | function skipTestIf() {
|
37 |
| - if (process.env.SKIP_TEST_IF_DISABLE) { |
| 38 | + if (process.env.SKIP_TEST_IF_DISABLED) { |
38 | 39 | return;
|
39 | 40 | }
|
40 | 41 |
|
41 |
| - const minNodeMajor = process.env.SKIP_TEST_IF_NODE_OLDER_THAN ?? Number(process.env.SKIP_TEST_IF_NODE_OLDER_THAN); |
42 |
| - if (!minNodeMajor || isNaN(minNodeMajor)) { |
43 |
| - console.warn('skip-test-if warning: set a minimum Node.js major version via SKIP_TEST_IF_NODE_OLDER_THAN=<num>'); |
| 42 | + let minNodeMajor; |
| 43 | + if (process.env.SKIP_TEST_IF_NODE_OLDER_THAN) { |
| 44 | + minNodeMajor = Number(process.env.SKIP_TEST_IF_NODE_OLDER_THAN) |
| 45 | + if (isNaN(minNodeMajor) || !Number.isInteger(minNodeMajor)) { |
| 46 | + console.warn(`skip-test-if warning: ignoring invalid SKIP_TEST_IF_NODE_OLDER_THAN value: "${process.env.SKIP_TEST_IF_NODE_OLDER_THAN}"`); |
| 47 | + minNodeMajor = undefined; |
| 48 | + } |
| 49 | + } |
| 50 | + let maxNodeMajor; |
| 51 | + if (process.env.SKIP_TEST_IF_NODE_NEWER_THAN) { |
| 52 | + maxNodeMajor = Number(process.env.SKIP_TEST_IF_NODE_NEWER_THAN) |
| 53 | + if (isNaN(maxNodeMajor) || !Number.isInteger(maxNodeMajor)) { |
| 54 | + console.warn(`skip-test-if warning: ignoring invalid SKIP_TEST_IF_NODE_NEWER_THAN value: "${process.env.SKIP_TEST_IF_NODE_NEWER_THAN}"`); |
| 55 | + maxNodeMajor = undefined; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (minNodeMajor === undefined && maxNodeMajor === undefined) { |
| 60 | + console.warn('skip-test-if warning: skip-test-if.js was used, but no SKIP_TEST_IF_* envvars were set'); |
44 | 61 | return;
|
45 | 62 | }
|
46 | 63 |
|
47 | 64 | const nodeMajor = Number(process.versions.node.split('.')[0]);
|
48 |
| - if (nodeMajor < minNodeMajor) { |
| 65 | + if (minNodeMajor && nodeMajor < minNodeMajor) { |
49 | 66 | process.stderr.write(`skip-test-if: skipping tests on old Node.js (${nodeMajor} < ${minNodeMajor})\n`);
|
50 | 67 | // "Skip" tests by exiting the process. Mocha is all in one process.
|
51 | 68 | process.exit(0);
|
52 | 69 | }
|
| 70 | + if (maxNodeMajor && nodeMajor > maxNodeMajor) { |
| 71 | + process.stderr.write(`skip-test-if: skipping tests on too-new Node.js (${nodeMajor} > ${maxNodeMajor})\n`); |
| 72 | + // "Skip" tests by exiting the process. Mocha is all in one process. |
| 73 | + process.exit(0); |
| 74 | + } |
53 | 75 | }
|
54 | 76 |
|
55 | 77 | skipTestIf()
|
|
0 commit comments