Skip to content

Commit

Permalink
tests: listen for data event instead of custom stream approach
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed Jun 15, 2018
1 parent 1e27933 commit a605b56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
18 changes: 9 additions & 9 deletions test/add-level.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

const { test } = require('tap')
const { sink } = require('./helper')
const { sink, once } = require('./helper')
const pino = require('../')

test('can add a custom level via constructor', async ({is}) => {
const stream = sink()
const instance = pino({level: 'foo', levelVal: 35}, stream)
is(typeof instance.foo, 'function')
instance.foo('bar')
const { msg } = await stream.next
const { msg } = await once(stream, 'data')
is(msg, 'bar')
})

Expand All @@ -19,7 +19,7 @@ test('can add a custom level to a prior instance', async ({is}) => {
instance.addLevel('foo2', 35)
is(typeof instance.foo2, 'function')
instance.foo2('bar')
const { msg } = await stream.next
const { msg } = await once(stream, 'data')
is(msg, 'bar')
})

Expand All @@ -43,7 +43,7 @@ test('custom levels encompass higher levels', async ({is}) => {
const stream = sink()
const instance = pino({level: 'foo', levelVal: 35}, stream)
instance.warn('bar')
const { msg } = await stream.next
const { msg } = await once(stream, 'data')
is(msg, 'bar')
})

Expand All @@ -54,7 +54,7 @@ test('after the fact add level does not include lower levels', async ({is}) => {
instance.level = 'foo'
instance.info('nope')
instance.foo('bar')
const { msg } = await stream.next
const { msg } = await once(stream, 'data')
is(msg, 'bar')
})

Expand All @@ -65,7 +65,7 @@ test('after the fact add of a lower level does not include it', async ({is}) =>
instance.addLevel('foo', 15)
instance.info('bar')
instance.foo('nope')
const { msg } = await stream.next
const { msg } = await once(stream, 'data')
is(msg, 'bar')
})

Expand All @@ -74,7 +74,7 @@ test('children can be set to custom level', async ({is}) => {
const parent = pino({level: 'foo', levelVal: 35}, stream)
const child = parent.child({childMsg: 'yes'})
child.foo('bar')
const { msg, childMsg } = await stream.next
const { msg, childMsg } = await once(stream, 'data')
is(msg, 'bar')
is(childMsg, 'yes')
})
Expand All @@ -85,7 +85,7 @@ test('custom levels exists on children', async ({is}) => {
parent.addLevel('foo', 35)
const child = parent.child({childMsg: 'yes'})
child.foo('bar')
const { msg, childMsg } = await stream.next
const { msg, childMsg } = await once(stream, 'data')
is(msg, 'bar')
is(childMsg, 'yes')
})
Expand Down Expand Up @@ -114,7 +114,7 @@ test('level numbers are logged correctly after level change', async ({is}) => {
const instance = pino({level: 'foo', levelVal: 25}, stream)
instance.level = 'debug'
instance.foo('bar')
const { level } = await stream.next
const { level } = await once(stream, 'data')
is(level, 25)
})

Expand Down
46 changes: 23 additions & 23 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
const os = require('os')
const { test } = require('tap')
const { sink, check } = require('./helper')
const { sink, check, once } = require('./helper')
const proxyquire = require('proxyquire')
const pino = require('../')
const { version } = require('../package.json')
Expand All @@ -24,7 +24,7 @@ function levelTest (name, level) {
const instance = pino(stream)
instance.level = name
instance[name]('hello world')
check(is, await stream.next, level, 'hello world')
check(is, await once(stream, 'data'), level, 'hello world')
})

test(`passing objects at level ${name}`, async ({is}) => {
Expand All @@ -33,7 +33,7 @@ function levelTest (name, level) {
instance.level = name
instance[name]({ hello: 'world' })

const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
is(result.pid, pid)
is(result.hostname, hostname)
Expand All @@ -47,7 +47,7 @@ function levelTest (name, level) {
const instance = pino(stream)
instance.level = name
instance[name]({ hello: 'world' }, 'a string')
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -65,7 +65,7 @@ function levelTest (name, level) {
const instance = pino(stream)
instance.level = name
instance[name]('hello %d', 42)
const result = await stream.next
const result = await once(stream, 'data')
check(is, result, level, 'hello 42')
})

Expand All @@ -79,7 +79,7 @@ function levelTest (name, level) {
}, stream)
instance.level = name
instance[name]({ err })
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -101,7 +101,7 @@ function levelTest (name, level) {
instance.level = name
const child = instance.child({ hello: 'world' })
child[name]('hello world')
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand Down Expand Up @@ -131,7 +131,7 @@ test('serializers can return undefined to strip field', async ({is}) => {
}, stream)

instance.info({ test: 'sensitive info' })
const result = await stream.next
const result = await once(stream, 'data')
is('test' in result, false)
})

Expand Down Expand Up @@ -163,7 +163,7 @@ test('set the name', async ({is, same}) => {
name: 'hello'
}, stream)
instance.fatal('this is fatal')
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -184,7 +184,7 @@ test('set the messageKey', async ({is, same}) => {
messageKey
}, stream)
instance.info(message)
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -200,7 +200,7 @@ test('set undefined properties', async ({is, same}) => {
const stream = sink()
const instance = pino(stream)
instance.info({ hello: 'world', property: undefined })
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -216,7 +216,7 @@ test('prototype properties are not logged', async ({is}) => {
const stream = sink()
const instance = pino(stream)
instance.info(Object.create({hello: 'world'}))
const { hello } = await stream.next
const { hello } = await once(stream, 'data')
is(hello, undefined)
})

Expand All @@ -229,7 +229,7 @@ test('set the base', async ({is, same}) => {
}, stream)

instance.fatal('this is fatal')
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -246,7 +246,7 @@ test('set the base to null', async ({is, same}) => {
base: null
}, stream)
instance.fatal('this is fatal')
const result = await stream.next
const result = await once(stream, 'data')
is(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
delete result.time
same(result, {
Expand All @@ -269,7 +269,7 @@ test('correctly escapes msg strings with stray double quote at end', async ({sam
}, stream)

instance.fatal('this contains "')
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -287,7 +287,7 @@ test('correctly escape msg strings with unclosed double quote', async ({same}) =
name: 'hello'
}, stream)
instance.fatal('" this contains')
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -305,7 +305,7 @@ test('object and format string', async ({same}) => {
const instance = pino(stream)
instance.info({}, 'foo %s', 'bar')

const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -320,7 +320,7 @@ test('object and format string property', async ({same}) => {
const stream = sink()
const instance = pino(stream)
instance.info({ answer: 42 }, 'foo %s', 'bar')
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -338,7 +338,7 @@ test('correctly strip undefined when returned from toJSON', async ({is}) => {
test: 'this'
}, stream)
instance.fatal({test: {toJSON () { return undefined }}})
const result = await stream.next
const result = await once(stream, 'data')
is('test' in result, false)
})

Expand Down Expand Up @@ -366,7 +366,7 @@ test('normalize number to string', async ({same}) => {
const stream = sink()
const instance = pino(stream)
instance.info(1)
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -381,7 +381,7 @@ test('normalize number to string with an object', async ({same}) => {
const stream = sink()
const instance = pino(stream)
instance.info({ answer: 42 }, 1)
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -399,7 +399,7 @@ test('handles objects with null prototype', async ({same}) => {
const o = Object.create(null)
o.test = 'test'
instance.info(o)
const result = await stream.next
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
Expand All @@ -415,7 +415,7 @@ test('children with same names render in correct order', async ({is}) => {
const stream = sink()
const root = pino(stream)
root.child({a: 1}).child({a: 2}).info({a: 3})
const { a } = await stream.next
const { a } = await once(stream, 'data')
is(a, 3, 'last logged object takes precedence')
})

Expand Down
8 changes: 1 addition & 7 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ function once (emitter, name) {

function sink (func) {
const result = split(JSON.parse)
var extract
const next = () => new Promise((resolve) => { extract = resolve })
result.pipe(writer.obj(func || ((value, enc, cb) => {
result.next = next()
extract(value)
result.next.then(() => cb())
})))
if (func) result.pipe(writer.obj(func))
return result
}

Expand Down

0 comments on commit a605b56

Please sign in to comment.