uci-utils-logger/test/logger.test.js

93 lines
2.1 KiB
JavaScript

import logger from '../src/logger'
import { expect } from 'chai'
import { it } from 'mocha'
import { stdout } from 'test-console'
//https://github.com/jamesshore/test-console
describe('JSON Logging Utility Testing ',async ()=> {
let log
// let captured = stdout.inspect().output
// let capture = stdout.inspect
// let restore = stdout.inspect().restore
//
class LogTest {
constructor(opts) {
log = logger({
name: 'test',
id: opts.id,
// file: 'example/example.js',
// class: 'LogTest',
// repo:'test repo'
package:'@uci-utils/test'
})
// console.log('env',process.env.UCI_ENV)
}
async logit(level,msg,props={}) {
Object.assign(props,{msg:msg})
log[level](props)
}
}
// TODO this is available from the logger as .levels
let levelName = {
10:'trace',
20:'debug',
30:'info',
40:'warn',
50:'error',
60:'fatal'
}
let logtest = {}
let inspect = {}
beforeEach( () => {
// console.log('starting console capture')
inspect = stdout.inspect()
})
afterEach( () => {
inspect.restore()
// console.log('console.restored')
})
it('check dev output to console' , async function () {
// setup
let msg='test'
let level = 'info'
let moreprops = {}
process.env.UCI_ENV='dev'
logtest = new LogTest({id:'someid'})
// create log to stdout
logtest.logit(level,msg,moreprops)
// test stdout response
let response = inspect.output[0]
expect(response).to.have.string(level.toUpperCase()) &&
expect(response).to.have.string(msg)
})
it('check json output' , async function () {
// setup
let msg='test'
let level = 'info'
let moreprops = {}
process.env.UCI_ENV='dev'
process.env.UCI_LOG_JSON=true
logtest = new LogTest({id:'someid'})
// create log to stdout
logtest.logit(level,msg,moreprops)
// test stdout response
let response = inspect.output[0]
response = JSON.parse(response)
expect(response.msg).to.equal(msg) &&
expect(levelName[response.level]).to.equal(level)
})
})