This is a super simple test util
test.equal(name, actual, expected)test.type(name, param, type)test.defined(name, param)
| 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, | |
| } |
