logatim
Version:
Isomorphic logger which implements log levels and ANSI styles
68 lines (55 loc) • 1.82 kB
JavaScript
const test = require('tap').test
const constants = require('../lib/constants')
const logatim = require('../index')
const CustomError = function CustomError (message) {
Error.captureStackTrace(this)
this.message = message
this.name = 'CustomError'
}
CustomError.prototype = Object.create(Error.prototype)
const dummies = {
customError: {
'message': 'Logatim!',
'name': 'CustomError'
},
simpleObject: {
'cool': 1337,
'loga': 'tim'
},
listObject: [1, 3, 3, 7],
deepObject: { 'go':
{'more':
[ 1, 2, {
'deeper': 'FTW',
'loga': ['t', 'i', 'm']
}
]}
},
simpleFunction: 'Valar morghulis\u001b[49m\u001b[0m',
composeFunction: '\u001b[1m[PREFIX 50] \u001b[34mValar dohaeris\u001b[49m\u001b[0m'
}
const getDummy = (key) => {
return `${JSON.stringify(dummies[key])}${constants.EOL}`
}
test('can print custom errors', function (t) {
t.plan(1)
try { throw new CustomError('Logatim!') } catch (ex) {
t.equal(logatim.raw(ex), getDummy('customError'), 'with all the variables')
}
})
test('can print objects', function (t) {
t.plan(3)
t.equal(logatim.raw(dummies.simpleObject), getDummy('simpleObject'), ' without leveling')
t.equal(logatim.raw(dummies.listObject), getDummy('listObject'), ' array-like')
t.equal(logatim.raw(dummies.deepObject), getDummy('deepObject'), ' with a deep nesting')
})
// use custom functions
test('can use functions as output', function (t) {
t.plan(2)
var simple = logatim.raw(function () { return 'Valar morghulis ' })
var compose = logatim
.bold(function () { return '[PREFIX ' + (13 + 37) + '] ' })
.blue.raw('Valar dohaeris')
t.equal(simple, dummies.simpleFunction, ' straight from the print function')
t.equal(compose, dummies.composeFunction, ' concatenating different chunks')
})