Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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