diff --git a/lib/index.js b/lib/index.js index 4929b22..5e8031d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,6 @@ +const META = Symbol('proc-log.meta') module.exports = { + META: META, output: { LEVELS: [ 'standard', diff --git a/test/index.js b/test/index.js index d4225eb..9921e59 100644 --- a/test/index.js +++ b/test/index.js @@ -2,9 +2,17 @@ const t = require('tap') const procLog = require('../') // This makes sure we are testing all known exported methods. -t.plan(4) +t.plan(5) for (const method in procLog) { + if (method === 'META') { + t.test(method, t => { + t.type(procLog[method], 'symbol') + t.end() + }) + continue + } + t.test(method, t => { const log = procLog[method] const { LEVELS, KEYS } = log diff --git a/test/meta.js b/test/meta.js new file mode 100644 index 0000000..2fda4b0 --- /dev/null +++ b/test/meta.js @@ -0,0 +1,42 @@ +const t = require('tap') +const { META, output } = require('../') + +// just to show how this would be implemented in consumers +const getMeta = (...args) => { + let meta = {} + const lastArg = args[args.length - 1] + if ( + lastArg && + typeof lastArg === 'object' && + Object.prototype.hasOwnProperty.call(lastArg, META) + ) { + meta = args.pop() + } + return [meta, ...args] +} + +// an example of wrapping a handler +const wrapMeta = (handler) => (level, ...args) => handler(level, ...getMeta(...args)) + +t.test('meta', t => { + process.once('output', (level, ...rawArgs) => { + const [meta, ...args] = getMeta(...rawArgs) + t.equal(level, 'standard') + t.ok(meta.force) + t.strictSame(args, ['arg1', 'arg2']) + t.end() + }) + + output.standard('arg1', 'arg2', { [META]: 0, force: true }) +}) + +t.test('wrap handler', t => { + process.once('output', wrapMeta((level, meta, ...args) => { + t.equal(level, 'standard') + t.ok(meta.force) + t.strictSame(args, ['arg1', { META: true }]) + t.end() + })) + + output.standard('arg1', { META: true }, { [META]: null, force: true }) +})