Comparing version 1.0.2 to 1.1.0
#!/usr/bin/env node | ||
const { EOL } = require('os'); | ||
const { existsSync } = require('fs'); | ||
const { run } = require('.'); | ||
const args = process.argv.slice(2); | ||
const files = args.filter(file => file !== '--serial'); | ||
const files = args.filter(existsSync); | ||
const isSerial = args.some(arg => arg === '--serial'); | ||
const timeout = parseInt(/(?<=--timeout[ =])[0-9]+/.exec(args.join(' ')), 10) || 15000; | ||
run({ files, isSerial }) | ||
run({ files, isSerial, timeout }) | ||
.then(({ total, failed, passed }) => { | ||
@@ -12,0 +14,0 @@ const summary = []; |
66
index.js
@@ -6,37 +6,49 @@ const { types } = require('util'); | ||
let tests, only, results; | ||
let tests, only; | ||
let passed, failed, skipped; | ||
const createHandlers = title => [ | ||
() => { | ||
passed++; | ||
console.log(`✔ ${title}`); | ||
}, | ||
err => { | ||
failed++; | ||
console.log(`✖ ${title}`); | ||
console.error(err); | ||
} | ||
]; | ||
const start = ({ title, timeout, resolve }) => { | ||
let called; | ||
const end = err => { | ||
if (called) return; | ||
called = true; | ||
clearTimeout(timer); | ||
if (err instanceof Error) { | ||
failed++; | ||
console.log(`✖ ${title}`); | ||
console.error(err); | ||
} else { | ||
passed++; | ||
console.log(`✔ ${title}`); | ||
} | ||
resolve(); | ||
}; | ||
const timer = setTimeout(end, timeout, new Error(`Test "${title}" timed out after ${timeout}ms`)); | ||
return end; | ||
}; | ||
const execute = async ({ tests, isSerial }) => { | ||
const execute = async ({ tests, isSerial, timeout = 15000 }) => { | ||
const results = []; | ||
for (const index in tests) { | ||
const [title, testFn] = tests[index]; | ||
const [pass, fail] = createHandlers(title); | ||
try { | ||
const testResult = isSerial ? await testFn() : testFn(); | ||
results.push(testResult); | ||
if (isPromise(testResult)) { | ||
testResult.then(pass).catch(fail); | ||
} else { | ||
pass(); | ||
const test = new Promise(resolve => { | ||
const end = start({ title, timeout, resolve }); | ||
try { | ||
const testResult = testFn(); | ||
if (isPromise(testResult)) { | ||
testResult.then(() => end()).catch(end); | ||
} else { | ||
end(); | ||
} | ||
} catch (err) { | ||
end(err); | ||
} | ||
} catch (err) { | ||
fail(err); | ||
} | ||
}); | ||
results.push(isSerial ? await test : test); | ||
} | ||
return results; | ||
}; | ||
const run = async ({ files, isSerial }) => { | ||
[tests, results, only] = [[], [], []]; | ||
const run = async ({ files, isSerial, timeout }) => { | ||
[tests, only] = [[], [], []]; | ||
[passed, failed, skipped] = [0, 0, 0]; | ||
@@ -46,3 +58,3 @@ | ||
await execute({ tests: only.length ? only : tests, isSerial }); | ||
const results = await execute({ tests: only.length ? only : tests, isSerial, timeout }); | ||
@@ -49,0 +61,0 @@ await Promise.all(results).catch(err => {}); |
{ | ||
"name": "bron", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Tiny test runner", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,8 @@ # bron | ||
- Run tests in parallel (default), or serial | ||
- Timeouts (default: 15s) | ||
- Requires Node.js v8+ (Node.js v12 has better validations and error messages) | ||
[![Build Status](https://travis-ci.org/webpro/bron.svg?branch=master)](https://travis-ci.org/webpro/bron) | ||
[![npm version](https://badge.fury.io/js/bron.svg)](https://www.npmjs.com/package/bron) | ||
## Why? | ||
@@ -19,15 +23,14 @@ | ||
Turns out this isn't very hard to implement, bron is only <70 LOC. In case you need more from your test framework, I'm | ||
happy to recommend one of the more full fledged options: | ||
Turns out this isn't very hard to implement, all source code of bron combined is only <100 LOC. In case you need more | ||
from your test framework, I'm happy to recommend one of the more full fledged options: | ||
| Runner | Dependencies | Size | | ||
| -------------- | :----------: | ----: | | ||
| Bron (v1.0.0) | 0 | 3K | | ||
| Tape (v4.10.2) | 32 | 263K | | ||
| Mocha (v6.1.4) | 115 | 1.52M | | ||
| Ava (v2.0.0) | 453 | 3.95M | | ||
| Bron (v1.1.0) | 0 | 5K | | ||
| Tape (v4.11.0) | 32 | 265K | | ||
| Mocha (v6.2.0) | 116 | 1.53M | | ||
| Ava (v2.2.0) | 387 | 3.68M | | ||
## Not featuring... | ||
- Timeouts (TODO) | ||
- Extensive command-line options | ||
@@ -58,3 +61,3 @@ - TAP reporting | ||
``` | ||
bron <file> [--serial] | ||
bron <file> [--serial] [--timeout=500] | ||
``` | ||
@@ -183,1 +186,5 @@ | ||
You can use `.only` multiple times (each `.only` will run). | ||
## Timeout | ||
Add `--timeout=n` (with `n` in milliseconds) to change the default value for each test (`15000`). |
@@ -108,2 +108,24 @@ const { EOL } = require('os'); | ||
} | ||
logStub.reset(); | ||
errorStub.reset(); | ||
{ | ||
const { total, failed, passed } = await run({ files: ['test/timeout.js'], timeout: 100 }); | ||
const output = logStub.args.map(args => args[0]); | ||
assert.deepEqual(output, [ | ||
'✔ should not time out', | ||
'✔ should not time out (1ms)', | ||
'✔ should not time out (50ms)', | ||
'✖ should time out (150ms)', | ||
'✖ should time out (150ms)' | ||
]); | ||
assert.equal(total, 5); | ||
assert.equal(failed, 2); | ||
assert.equal(passed, 3); | ||
console.info(output.join(EOL)); | ||
} | ||
} catch (err) { | ||
@@ -110,0 +132,0 @@ console.warn(err); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15497
14
281
187
0
2