Skip to content

Commit ab5d8e0

Browse files
wip
1 parent 1830406 commit ab5d8e0

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

pino-test.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const DEFAULT_WAIT_FOR_TIMEOUT = 1000
1515
*
1616
* @returns A stream.
1717
*/
18-
function sink ({ destroyOnError = false, emitErrorEvent = false } = {}) {
18+
function sink({ destroyOnError = false, emitErrorEvent = false } = {}) {
1919
const stream = split((data) => {
2020
try {
2121
return JSON.parse(data)
@@ -37,7 +37,7 @@ function sink ({ destroyOnError = false, emitErrorEvent = false } = {}) {
3737
*
3838
* @throws If the expected value is not equal to the chunk value.
3939
*/
40-
function check (chunk, expectedOrCallback, assert) {
40+
function check(chunk, expectedOrCallback, assert) {
4141
const { time, pid, hostname, ...chunkCopy } = chunk
4242

4343
nodeAssert.strictEqual(new Date(time) <= new Date(), true, 'time is greater than Date.now()')
@@ -77,7 +77,7 @@ function check (chunk, expectedOrCallback, assert) {
7777
* assert.strictEqual(log.msg, 'hello world 1')
7878
* })
7979
*/
80-
async function once (stream, expectedOrCallback, assert = nodeAssert.deepStrictEqual) {
80+
async function once(stream, expectedOrCallback, assert = nodeAssert.deepStrictEqual) {
8181
return new Promise((resolve, reject) => {
8282
const dataHandler = (data) => {
8383
stream.removeListener('error', reject)
@@ -118,7 +118,7 @@ async function once (stream, expectedOrCallback, assert = nodeAssert.deepStrictE
118118
* ]
119119
* await pinoTest.consecutive(stream, expecteds)
120120
*/
121-
async function consecutive (stream, expectedOrCallbacks, assert = nodeAssert.deepStrictEqual) {
121+
async function consecutive(stream, expectedOrCallbacks, assert = nodeAssert.deepStrictEqual) {
122122
let i = 0
123123
for await (const chunk of stream) {
124124
check(chunk, expectedOrCallbacks[i], assert)
@@ -136,9 +136,11 @@ async function consecutive (stream, expectedOrCallbacks, assert = nodeAssert.dee
136136
*
137137
* @param {import('node:stream').Transform} stream The stream to be tested.
138138
* @param {Array<object | Function>} expectedsOrCallbacks The array of expected values to be tested or callback functions.
139-
* @param {Function} [assert=nodeAssert.deepStrictEqual] The assert function to be used when the expectedOrCallback parameter is an object.
140-
* @param {Number} [timeout=1000] The time in milliseconds to wait for the expected value.
141-
*
139+
* @param {Function} [assert=nodeAssert.partialDeepStrictEqual] The assert function to be used when the expectedOrCallback parameter is an object.
140+
* @param {Object} [options] The options to be used.
141+
* @param {Number} [options.timeout=1000] The time in milliseconds to wait for the expected value.
142+
* @param {Number} [options.maxMessages=100] The maximum number of messages to wait for.
143+
* @param {Boolean} [options.debug=false] If true, the stream will be logged to the console.
142144
* @returns A promise that resolves when the expected value is equal to the stream value.
143145
* @throws If the expected value is not equal to the stream value.
144146
* @throws If the callback function throws an error.
@@ -154,8 +156,37 @@ async function consecutive (stream, expectedOrCallbacks, assert = nodeAssert.dee
154156
*
155157
* await pinoTest.waitFor(stream, { msg: 'server started', level: 30 })
156158
*/
157-
async function waitFor (stream, expectedOrCallbacks, assert = nodeAssert.deepStrictEqual, timeout = DEFAULT_WAIT_FOR_TIMEOUT) {
158-
// TODO implementation
159+
async function waitFor(stream, expectedOrCallbacks, assert = nodeAssert.partialDeepStrictEqual, { maxMessages = DEFAULT_MAX_MESSAGES, timeout = DEFAULT_WAIT_FOR_TIMEOUT, debug = false } = {}) {
160+
return new Promise((resolve, reject) => {
161+
let count = 0
162+
const fn = (message) => {
163+
if (debug) {
164+
console.log('waitFor received', message)
165+
}
166+
167+
if (check(message, expectedOrCallbacks, assert)) {
168+
stream.off('data', fn)
169+
stream.off('error', reject)
170+
resolve()
171+
}
172+
count++
173+
if (count > maxMessages) {
174+
stream.off('data', fn)
175+
stream.off('error', reject)
176+
const identifier = typeof expectedOrCallbacks !== 'function' ? expectedOrCallbacks.msg : '[function]'
177+
reject(new Error(`Max message count reached on waitFor: ${identifier}`))
178+
}
179+
}
180+
stream.on('data', fn)
181+
stream.on('error', reject)
182+
183+
setTimeout(() => {
184+
stream.off('data', fn)
185+
stream.off('error', reject)
186+
const identifier = typeof expectedOrCallbacks !== 'function' ? expectedOrCallbacks.msg : '[function]'
187+
reject(new Error(`Timeout on waitFor: ${identifier}`))
188+
}, timeout)
189+
})
159190
}
160191

161-
module.exports = { sink, once, consecutive }
192+
module.exports = { sink, once, consecutive, waitFor }

test/pino-test.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,18 @@ describe('consecutive', () => {
399399
)
400400
})
401401
})
402+
403+
describe('waitFor', () => {
404+
test('should wait for the expected log message', async () => {
405+
const stream = pinoTest.sink()
406+
const instance = pino(stream)
407+
408+
setTimeout(() => {
409+
instance.info('hello world')
410+
}, 10)
411+
412+
const expected = { msg: 'hello world', level: 30 }
413+
414+
await pinoTest.waitFor(stream, expected)
415+
})
416+
})

0 commit comments

Comments
 (0)