Skip to content

Commit

Permalink
Add explicit test for shared levels state
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Jan 16, 2017
1 parent ed0cd3c commit 7fcf153
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pino.js
Expand Up @@ -139,6 +139,9 @@ function pino (opts, stream) {
defineLevelsProperty(pino)

function Pino (opts, stream) {
// We define the levels property at construction so that state does
// not get shared between instances.
defineLevelsProperty(this)
this.stream = stream
this.serializers = opts.serializers
this.stringify = opts.stringify
Expand Down Expand Up @@ -170,7 +173,6 @@ function Pino (opts, stream) {
}

Pino.prototype = new EventEmitter()
defineLevelsProperty(Pino.prototype)

Pino.prototype.fatal = genLog(levels.fatal)
Pino.prototype.error = genLog(levels.error)
Expand Down
26 changes: 11 additions & 15 deletions test/addlevel.test.js
Expand Up @@ -2,12 +2,10 @@

var test = require('tap').test
var sink = require('./helper').sink
var pinoPath = require.resolve('../')
var pino = require('../')

test('can add a custom level via constructor', function (t) {
t.plan(2)
delete require.cache[pinoPath]
var pino = require(pinoPath)

var log = pino({level: 'foo', levelVal: 35}, sink(function (chunk, enc, cb) {
t.is(chunk.msg, 'bar')
Expand All @@ -20,8 +18,6 @@ test('can add a custom level via constructor', function (t) {

test('can add a custom level to a prior instance', function (t) {
t.plan(2)
delete require.cache[pinoPath]
var pino = require(pinoPath)

var log = pino(sink(function (chunk, enc, cb) {
t.is(chunk.msg, 'bar')
Expand All @@ -34,8 +30,6 @@ test('can add a custom level to a prior instance', function (t) {

test('custom levels encompass higher levels', function (t) {
t.plan(1)
delete require.cache[pinoPath]
var pino = require(pinoPath)

var log = pino({level: 'foo', levelVal: 35}, sink(function (chunk, enc, cb) {
t.is(chunk.msg, 'bar')
Expand All @@ -47,8 +41,6 @@ test('custom levels encompass higher levels', function (t) {

test('after the fact add level does not include lower levels', function (t) {
t.plan(1)
delete require.cache[pinoPath]
var pino = require(pinoPath)

var log = pino(sink(function (chunk, enc, cb) {
t.is(chunk.msg, 'bar')
Expand All @@ -63,8 +55,6 @@ test('after the fact add level does not include lower levels', function (t) {

test('children can be set to custom level', function (t) {
t.plan(2)
delete require.cache[pinoPath]
var pino = require(pinoPath)

var parent = pino({level: 'foo', levelVal: 35}, sink(function (chunk, enc, cb) {
t.is(chunk.msg, 'bar')
Expand All @@ -77,19 +67,25 @@ test('children can be set to custom level', function (t) {

test('rejects already known labels', function (t) {
t.plan(1)
delete require.cache[pinoPath]
var pino = require(pinoPath)
var log = pino({level: 'info', levelVal: 900})
t.is(log.levelVal, 30)
})

test('reject already known values', function (t) {
t.plan(1)
delete require.cache[pinoPath]
var pino = require(pinoPath)
try {
pino({level: 'foo', levelVal: 30})
} catch (e) {
t.is(e.message.indexOf('level value') > -1, true)
}
})

test('levels state is not shared between instances', function (t) {
t.plan(2)

var instance1 = pino({level: 'foo', levelVal: 35})
t.is(typeof instance1.foo, 'function')

var instance2 = pino()
t.is(instance2.hasOwnProperty('foo'), false)
})

0 comments on commit 7fcf153

Please sign in to comment.