Skip to content

Commit bcba77c

Browse files
committed
chore: improve test coverage
1 parent efe6107 commit bcba77c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

factories/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class RequestFactory {
4141
allowMethodSpoofing: false,
4242
trustProxy: proxyAddr.compile('loopback'),
4343
subdomainOffset: 2,
44-
generateRequestId: true,
44+
generateRequestId: false,
4545
...this.#parameters.config,
4646
}
4747
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
],
185185
"exclude": [
186186
"tests/**",
187-
"test_factories/**",
187+
"factories/**",
188188
".yalc/**"
189189
]
190190
}

tests/exceptions/exception_handler.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,26 @@ test.group('Exception handler | report', () => {
384384

385385
assert.lengthOf(parsedLogs, 0)
386386
})
387+
388+
test('log request id in logs when request id exists', async ({ assert }) => {
389+
const logs: string[] = []
390+
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
391+
const exceptionHandler = new HttpExceptionHandler(logger)
392+
const ctx = new HttpContextFactory().create()
393+
394+
ctx.request.request.headers['x-request-id'] = '123'
395+
396+
const error = new Exception('Something went wrong', { status: 302 })
397+
await exceptionHandler.report(error, ctx)
398+
399+
const parsedLogs = logs.map((line) => {
400+
const logLine = JSON.parse(line)
401+
return { message: logLine.msg, level: logLine.level, requestId: logLine['x-request-id'] }
402+
})
403+
404+
assert.lengthOf(parsedLogs, 1)
405+
assert.equal(parsedLogs[0].message, 'Something went wrong')
406+
assert.equal(parsedLogs[0].requestId, '123')
407+
assert.equal(parsedLogs[0].level, 30)
408+
})
387409
})

tests/request.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,10 @@ test.group('Request', () => {
688688

689689
test('set x-request-id header when id method is called', async ({ assert }) => {
690690
const server = createServer((req, res) => {
691-
const request = new RequestFactory().merge({ req, res, encryption }).create()
691+
const request = new RequestFactory()
692+
.merge({ req, res, encryption, config: { generateRequestId: true } })
693+
.create()
694+
692695
res.writeHead(200, { 'content-type': 'application/json' })
693696
res.end(
694697
JSON.stringify({

0 commit comments

Comments
 (0)