Skip to content

Instantly share code, notes, and snippets.

@vdsabev
Last active May 16, 2024 20:49
Show Gist options
  • Save vdsabev/7ba9449f4ee56bf7abc9ab6cf04cb601 to your computer and use it in GitHub Desktop.
Save vdsabev/7ba9449f4ee56bf7abc9ab6cf04cb601 to your computer and use it in GitHub Desktop.
Minimal Node.js test runner
import assert from 'node:assert/strict';
export default {
'tests for sum': {
'1 + 1 should be 2'() {
assert.equal(1 + 1, 2);
},
'2 + 2 should be 4'() {
assert.equal(2 + 2, 4);
},
},
'tests for multiplication': {
'1 * 1 should be 1'() {
assert.equal(1 * 1, 1);
},
'2 * 2 should be 4'() {
assert.equal(2 * 2, 4);
},
},
};
import fs from 'node:fs';
const [pattern] = process.argv.slice(2);
fs.glob(pattern, async (error, filenames) => {
if (error) throw error;
for (const filename of filenames) {
const suite = await import(`${process.cwd()}/${filename}`);
for (const [description, tests] of Object.entries(suite.default || suite)) {
console.log('');
console.log(description);
for (const [title, test] of Object.entries(tests)) {
try {
test();
console.log(`✅ ${title}`);
} catch (error) {
console.error(`❌ ${title}`);
console.error(error.message);
}
}
}
console.log('');
}
});
@vdsabev
Copy link
Author

vdsabev commented May 16, 2024

A minimal test runner for Node.js with support for test groups.
Copy the script and run with node test.js to get the test results:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment