Last active
May 16, 2024 20:49
-
-
Save vdsabev/7ba9449f4ee56bf7abc9ab6cf04cb601 to your computer and use it in GitHub Desktop.
Minimal Node.js test runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: