Skip to content

Commit

Permalink
fix(refactor): don't loop through levels to generate log export
Browse files Browse the repository at this point in the history
Instead use a test to make sure LEVELS an functions match
  • Loading branch information
wraithgar committed Apr 12, 2024
1 parent 8e90af0 commit 978fbf9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
40 changes: 23 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const log = {
LEVELS: [
'notice',
'error',
'warn',
'info',
'verbose',
'http',
'silly',
'pause',
'resume',
],
module.exports = {
log: {
LEVELS: [
'notice',
'error',
'warn',
'info',
'verbose',
'http',
'silly',
'pause',
'resume',
],
error: (...args) => process.emit('log', 'error', ...args),
notice: (...args) => process.emit('log', 'notice', ...args),
warn: (...args) => process.emit('log', 'warn', ...args),
info: (...args) => process.emit('log', 'info', ...args),
verbose: (...args) => process.emit('log', 'verbose', ...args),
http: (...args) => process.emit('log', 'http', ...args),
silly: (...args) => process.emit('log', 'silly', ...args),
pause: (...args) => process.emit('log', 'pause', ...args),
resume: (...args) => process.emit('log', 'resume', ...args),
},
}
for (const level of log.LEVELS) {
log[level] = (...args) => process.emit('log', level, ...args)
}

module.exports = { log }
2 changes: 1 addition & 1 deletion tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/index.js TAP > log levels 1`] = `
exports[`test/index.js TAP log > log levels 1`] = `
Array [
"notice",
"error",
Expand Down
36 changes: 25 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ const t = require('tap')
const { log } = require('../')
const { LEVELS } = log

t.matchSnapshot(LEVELS, 'log levels')
for (const level of LEVELS) {
t.test(level, t => {
t.match(log[level], Function)
process.once('log', (actual, ...args) => {
t.equal(actual, level, 'emitted log with expected level')
t.same(args, [1, 2, 3], 'got expected args')
t.end()
})
log[level](1, 2, 3)
t.test('log', t => {
t.matchSnapshot(LEVELS, 'log levels')
t.test('all log.LEVELS have a function in log', t => {
for (const level of LEVELS) {
t.test(level, t => {
t.match(log[level], Function)
process.once('log', (actual, ...args) => {
t.equal(actual, level, 'emitted log with expected level')
t.same(args, [1, 'two', [3], { 4: 4 }], 'got expected args')
t.end()
})
log[level](1, 'two', [3], { 4: 4 })
})
}
t.end()
})
}
t.test('all log functions are in log.LEVELS', t => {
t.plan(LEVELS.length)
for (const fn in log) {
if (fn !== 'LEVELS') {
t.ok(LEVELS.includes(fn), `log.${fn} is in LEVELS`)
}
}
})
t.end()
})

0 comments on commit 978fbf9

Please sign in to comment.