Skip to content

Instantly share code, notes, and snippets.

@fpirsch
Last active August 28, 2021 21:03
Show Gist options
  • Save fpirsch/464e1d424bb24b0b9fe9a404eee7a4d4 to your computer and use it in GitHub Desktop.
Save fpirsch/464e1d424bb24b0b9fe9a404eee7a4d4 to your computer and use it in GitHub Desktop.
The smallest test framework
const test = exports.test = (label, cb) => (test.$list || (test.$list = [])).push({ label, cb })
exports.beforeEach = cb => test.$beforeEach = cb
setImmediate(() => test.$list.forEach(async ({ label, cb }) => console.log(label, await cb(test.$beforeEach?.()) || '\u001B[32m✔\u001B[39m')))
test.skip = (label, cb) => console.log(label, '\u001B[91m✗ SKIPPED\u001B[39m')
test.todo = (label, cb) => console.log(label, '\u001B[91m✗ TODO\u001B[39m')
test.only = (label, cb) => (test.$list = [{ label, cb }]).push = () => {}
@fpirsch
Copy link
Author

fpirsch commented Aug 28, 2021

No bullshit test framework with

  • sync and async tests
  • beforeEach setup
  • .skip, .todo and .only modifiers

Recommended assertion library : https://nodejs.org/api/assert.html

Usage example:

const assert = require('assert')
const { beforeEach, test } = require('./smallest-test-framework.js')

test('should work', () => {
  assert.strictEqual(1, 1)
})

test('should work asynchronously', async () => {
  const result = await something()
  assert.deepStrictequal(result, { data: 42 })
})

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