Skip to content

Commit

Permalink
fix(core): tests should fail if has process.exit or unresolved Prom…
Browse files Browse the repository at this point in the history
…ises (#207)

* A test that exits early should fail

Fixes #206

Also fixes the case where a test calls process.exit(0) itself, as that's essentially the same thing.

* Apply suggestions from code review

* wrap exit listener; merge test files

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
  • Loading branch information
rictic and lukeed committed Jul 3, 2022
1 parent d764c08 commit 70b59e7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.js
Expand Up @@ -125,10 +125,12 @@ function setup(ctx, name = '') {
export const suite = (name = '', state = {}) => setup(context(state), name);
export const test = suite();

let isRunning = false;
export async function exec(bail) {
let timer = hrtime();
let done=0, total=0, skips=0, code=0;

isRunning = true;
for (let group of UVU_QUEUE) {
if (total) write('\n');

Expand All @@ -145,10 +147,17 @@ export async function exec(bail) {
}
}

isRunning = false;
write('\n Total: ' + total);
write((code ? kleur.red : kleur.green)('\n Passed: ' + done));
write('\n Skipped: ' + (skips ? kleur.yellow(skips) : skips));
write('\n Duration: ' + timer() + '\n\n');

if (isNode) process.exitCode = code;
}

if (isNode) process.on('exit', () => {
if (!isRunning) return; // okay to exit
process.exitCode = process.exitCode || 1;
console.error('Exiting early before testing is finished.');
});
17 changes: 17 additions & 0 deletions test/exit.fails.js
@@ -0,0 +1,17 @@
import { test } from 'uvu';

// TODO: test.fail() modifier (see #47)

// A test that calls process.exit must fail (even if it exits with 0).
// Otherwise all tests might not run, or even all assertions within test.
test('should fail if `process.exit` encountered', async () => {
process.exit(0);
});

// This promise will never resolve & the process will exit early.
// This must fail, otherwise all tests/assertions might not run.
test('should fail if Promise never resolves :: GC', async () => {
await new Promise(() => {});
});

test.run();
16 changes: 16 additions & 0 deletions test/index.js
Expand Up @@ -14,6 +14,22 @@ totalist(__dirname, (rel, abs) => {
let pid = spawnSync('node', HOOK.concat(abs));
let file = kleur.bold().underline(rel);

if (rel.endsWith('.fails.js')) {
try {
assert.notEqual(pid.status, 0, 'expected to fail');
assert.equal(pid.stderr.length > 0, true, 'run w/ stderr');
assert.equal(pid.stdout.length, 0, 'run w/o stdout');
console.log(PASS + file);
} catch (err) {
console.error(FAIL + file + ' :: "%s"', err.message);
if (pid.stdout.length) {
console.error(LEFT + '\n' + LEFT + pid.stdout.toString().replace(/(\r?\n)/g, '$1' + LEFT));
}
code = 1;
}
return;
}

try {
assert.equal(pid.status, 0, 'run w/o error code');
assert.equal(pid.stderr.length, 0, 'run w/o stderr');
Expand Down

0 comments on commit 70b59e7

Please sign in to comment.