Skip to content

Commit

Permalink
fix(levels): useLevelLabels permanently mutates initial cache (#684)
Browse files Browse the repository at this point in the history
* fix(levels): useLevelLabels permanently mutates initial cache

* simplify implimentation - always copy the instance state when recreating
  • Loading branch information
pgoldrbx authored and mcollina committed Jul 23, 2019
1 parent 560d8bd commit 10a5d1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function genLsCache (instance) {
? `{"${levelName}":"${instance.levels.labels[k]}"`
: flatstr(`{"${levelName}":` + Number(k))
return o
}, instance[lsCacheSym])
}, Object.assign({}, instance[lsCacheSym]))
return instance
}

Expand Down
15 changes: 15 additions & 0 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ test('produces labels when told to', async ({ is }) => {
instance.info('hello world')
})

test('resets levels from labels to numbers', async ({ is }) => {
const expected = [{
level: 30,
msg: 'hello world'
}]
pino({ useLevelLabels: true })
const instance = pino({ useLevelLabels: false }, sink((result, enc, cb) => {
const current = expected.shift()
check(is, result, current.level, current.msg)
cb()
}))

instance.info('hello world')
})

test('changes label naming when told to', async ({ is }) => {
const expected = [{
priority: 30,
Expand Down

0 comments on commit 10a5d1c

Please sign in to comment.