Skip to content

Instantly share code, notes, and snippets.

@shannonmoeller
Created December 3, 2018 23:19
Show Gist options
  • Save shannonmoeller/026bf1dc615307cd5d325d10a1635b71 to your computer and use it in GitHub Desktop.
Save shannonmoeller/026bf1dc615307cd5d325d10a1635b71 to your computer and use it in GitHub Desktop.
const tests = new Set();
const t = {
total: 0,
passed: 0,
failed: 0,
comment(message) {
console.log(`# ${message}`);
},
ok(value, message = 'should be ok', ...rest) {
const output = `ok ${++t.total} ${message}`;
if (value) {
++t.passed;
console.log(output);
} else {
++t.failed;
console.log(`not ${output}`);
console.assert(value, ...rest);
}
},
equal(actual, expected, message = 'should be equal') {
t.ok(actual === expected, message, { actual, expected });
},
deepEqual(actual, expected, message = 'should be deeply equal') {
const aa = JSON.stringify(actual);
const bb = JSON.stringify(expected);
t.ok(aa === bb, message, { actual, expected });
},
};
export function test(description, fn) {
tests.add([fn && description, fn || description]);
}
export function title(value) {
test(`\n# ${value}\n# `, () => null);
}
setTimeout(async () => {
console.log('TAP version 13');
for (const [description, fn] of tests) {
if (description) {
t.comment(description);
}
await fn(t);
}
t.comment(`pass: ${t.passed}`);
t.comment(`fail: ${t.failed}`);
console.log(`1..${t.total}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment