Skip to content

Instantly share code, notes, and snippets.

@ronapelbaum
Last active July 23, 2018 08:27
Show Gist options
  • Save ronapelbaum/7efde4c94c9b8c80d34bdf1f3a03c974 to your computer and use it in GitHub Desktop.
Save ronapelbaum/7efde4c94c9b8c80d34bdf1f3a03c974 to your computer and use it in GitHub Desktop.
test.js - minimal nodejs test

test.js

This is a super simple test util

usage

  • test.equal(name, actual, expected)
  • test.type(name, param, type)
  • test.defined(name, param)

output

image

const app = () => {
const list = []
return {
add: (item) => list.push(list),
get: () => list.slice(0),
}
}
module.exports = app
const test = require('test')
const app = require('./app')
const myApp = app()
test.type('app should have add method', myApp.add, 'function')
test.type('app should have remove method', myApp.remove, 'function')
myApp.add('go to gym')
test.equal('app.get() should return a list with 1 item', myApp.get().length, 1)
const defaultColor = '\x1b[0m'
const color = color => {
return (...args) => console.log(color + args.join(' ') + defaultColor)
}
const red = color('\x1b[31m')
const green = color('\x1b[32m')
const blue = color('\x1b[34m')
module.exports = {
red,
green,
blue,
}
const assert = require('assert')
const log = require('./log')
let passed = 0
let failed = 0
const t = (name, cb) => {
try {
cb()
log.green('PASS', name)
passed++
} catch (e) {
log.red('FAIL', name, 'expected:', e.expected, 'actual:', e.actual)
failed++
}
}
const equal = (name, actual, expected) =>
t(name, () => assert.strictEqual(actual, expected))
const defined = (name, param) =>
t(name, () => assert.notStrictEqual(typeof param, 'undefined'))
const type = (name, param, type) =>
t(name, () => assert.strictEqual(typeof param, type))
process.on('exit', () => {
const total = passed + failed
log.blue('-'.repeat(total))
log.blue('Total:', total)
log.green('Passed:', passed)
log.red('Failed:', failed)
log.blue('-'.repeat(total))
})
module.exports = {
equal,
type,
deinfed,
}
@ronapelbaum
Copy link
Author

image

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