Skip to content

Commit bf009c5

Browse files
committed
added TEST_GAP_TIME
1 parent 853a2ff commit bf009c5

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

ADVANCED_USAGE.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ Output will look like this:
2727
#### LOG_SKIP
2828
You can suppress logging the information about which tests were skipped.
2929
This can be helpful when you are skipping hundreds of tests.
30-
```LOG_SKIP=false npm test```
30+
```LOG_SKIP=false npm test```
31+
32+
#### TEST_GAP_TIME
33+
You can pause in between tests. This helps if there are side effects
34+
that need time to complete. Gap time is in seconds.
35+
```TEST_GAP_TIME=3.5 npm test```

examples/test.gap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const test = require("../index.js");
2+
3+
// sleep 3 seconds between tests
4+
process.env.TEST_GAP_TIME = "3";
5+
6+
test("first", ({ eq }) => {});
7+
8+
test("second", ({ eq }) => {});

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ const MINUS = COLORS.PURPLE + "-" + COLORS.OFF;
1919
const queue = [];
2020
const complete = [];
2121

22+
const sleep = ms =>
23+
new Promise(resolve => {
24+
setTimeout(() => resolve(), ms);
25+
});
26+
27+
let ran = 0;
28+
2229
const run = async ({ name, cb, caller }) => {
2330
let savedActual, savedExpected;
2431
const eq = function (actual, expected) {
@@ -38,6 +45,8 @@ const run = async ({ name, cb, caller }) => {
3845
};
3946

4047
try {
48+
if (ran >= 1 && process.env.TEST_GAP_TIME) await sleep(Number(process.env.TEST_GAP_TIME) * 1000);
49+
4150
const start_time = performance.now();
4251
await Promise.resolve(cb({ eq }));
4352
if (caller !== complete[complete.length - 1]) {
@@ -109,6 +118,8 @@ const run = async ({ name, cb, caller }) => {
109118
process.exit(1);
110119
}
111120
}
121+
122+
ran++;
112123
};
113124

114125
const skip = name => {

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ t("node ./examples/test.name.js", "\x1B[33mskipped: skip\x1B[39m\n\x1B[32msucces
6161

6262
t("node ./examples/test.name-wildcard.js", "\x1B[33mskipped: skip\x1B[39m\n\x1B[32msuccess: only[this]\x1B[0m\n");
6363

64+
const start_test_gap = performance.now();
65+
t("node ./examples/test.gap.js", "\x1B[32msuccess: first\x1B[0m\n\x1B[32msuccess: second\x1B[0m\n");
66+
const end_test_gap = performance.now();
67+
const duration_test_gap = Math.round((end_test_gap - start_test_gap) / 1000);
68+
assert.strictEqual(duration_test_gap, 3);
69+
6470
const cmd2 = "node ./examples/test.only-one-arg.js";
6571
const log2 = $(cmd2).trim().replace(/\n/g, "");
6672
assert.strictEqual(log2.includes("you only supplied one argument"), true);

0 commit comments

Comments
 (0)