Last active
October 5, 2022 21:03
-
-
Save branneman/71428ab3c4c90941e90485ac0b53b2b7 to your computer and use it in GitHub Desktop.
How I write High Quality Unit tests - https://youtu.be/naL7-XQ7T70
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
/** | |
* Returns a list of numbers from `min` (inclusive) to `max` (exclusive). | |
*/ | |
const range = (min, max, acc = []) => { | |
if (max < min) throw new Error('not supported!') | |
if (min >= max) return acc | |
return range(min + 1, max, acc.concat(min)) | |
} | |
/** | |
* Very minimal Unit-testing framework | |
*/ | |
const assert = require('assert') // Node.js standard module | |
const tests = [] | |
const test = (name, fn) => tests.push({ name, fn }) | |
const run = () => | |
tests.forEach((t) => { | |
try { | |
t.fn() | |
console.log(`\n✅ PASSED: ${t.name}`) | |
} catch (e) { | |
console.log(`\n❌ FAILED: ${t.name}`) | |
console.error(e.message) | |
} | |
}) | |
/** | |
* Unit tests | |
*/ | |
test('equal min-max works inclusive-exclusive', () => { | |
assert.deepEqual(range(0, 0), []) | |
}) | |
test('max is handled exclusively', () => { | |
assert.deepEqual(range(0, 1), [0]) | |
assert.deepEqual(range(0, 2), [0, 1]) | |
}) | |
test('handles starting at higher numbers', () => { | |
assert.deepEqual(range(5, 9), [5, 6, 7, 8]) | |
}) | |
test('supports negative numbers', () => { | |
assert.deepEqual(range(-5, -3), [-5, -4]) | |
assert.deepEqual(range(-5, 3), [-5, -4, -3, -2, -1, 0, 1, 2]) | |
}) | |
test('does not support decreasing numbers', () => { | |
assert.throws(() => range(8, 3)) | |
}) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment