Skip to content

Commit 6283d7a

Browse files
committed
test: improve test coverage
1 parent aa69c6d commit 6283d7a

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/tests/index.spec.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import tap from 'tap';
22
import sinon from 'sinon';
3-
import pino, { PinoLambdaLogger } from '../index';
3+
import pino, { PinoLambdaLogger, ExtendedPinoOptions } from '../index';
44

55
sinon.useFakeTimers(Date.UTC(2016, 11, 1, 6, 0, 0, 0));
66

@@ -38,6 +38,18 @@ tap.test('should log an error message with requestId', (t) => {
3838
t.end();
3939
});
4040

41+
tap.test('should log correlation headers', (t) => {
42+
const [log, output] = createLogger();
43+
44+
log.withRequest(
45+
{ headers: { 'x-correlation-data': 'abbb', 'x-correlation-service': 'tyue' } },
46+
{ awsRequestId: '98875' },
47+
);
48+
log.error('Message with correlation ids');
49+
t.matchSnapshot(output.buffer);
50+
t.end();
51+
});
52+
4153
tap.test('should log an error message with apiRequestId', (t) => {
4254
const [log, output] = createLogger();
4355

@@ -57,16 +69,46 @@ tap.test('should add tags with a child logger', (t) => {
5769
t.end();
5870
});
5971

72+
tap.test('should preserve mixins', (t) => {
73+
let n = 0;
74+
const [log, output] = createLogger({
75+
mixin () {
76+
return { line: ++n }
77+
}
78+
});
79+
80+
log.withRequest({}, { awsRequestId: '431234' });
81+
log.info('Message with mixin line 1');
82+
log.info('Message with mixin line 2');
83+
t.matchSnapshot(output.buffer);
84+
t.end();
85+
});
86+
87+
tap.test('should capture xray trace IDs', (t) => {
88+
process.env._X_AMZN_TRACE_ID = 'x-1-2e2323r1234r4';
89+
90+
const [log, output] = createLogger();
91+
92+
log.withRequest({}, { awsRequestId: '431234' });
93+
log.info('Message with trace ID');
94+
t.matchSnapshot(output.buffer);
95+
96+
process.env._X_AMZN_TRACE_ID = undefined;
97+
t.end();
98+
});
99+
100+
60101
/**
61102
* Creates a test logger and output buffer for assertions
62103
* Returns the logger and the buffer
63104
*/
64-
function createLogger(): [PinoLambdaLogger, { buffer: string }] {
105+
function createLogger(options?: ExtendedPinoOptions): [PinoLambdaLogger, { buffer: string }] {
65106
const output = {
66107
buffer: 'undefined',
67108
};
68109

69110
const logger = pino({
111+
...options,
70112
streamWriter: (str: string | Uint8Array): boolean => {
71113
output.buffer = (str as string).trim();
72114
return true;

tap-snapshots/src-tests-index.spec.ts-TAP.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ exports[`src/tests/index.spec.ts TAP should add tags with a child logger > must
99
2016-12-01T06:00:00.000Z 9048989 INFO Message with userId {"level":30,"time":1480572000000,"userId":12,"awsRequestId":"9048989","x-correlation-id":"9048989","msg":"Message with userId"}
1010
`
1111

12+
exports[`src/tests/index.spec.ts TAP should capture xray trace IDs > must match snapshot 1`] = `
13+
2016-12-01T06:00:00.000Z 431234 INFO Message with trace ID {"level":30,"time":1480572000000,"awsRequestId":"431234","x-correlation-trace-id":"x-1-2e2323r1234r4","x-correlation-id":"431234","msg":"Message with trace ID"}
14+
`
15+
1216
exports[`src/tests/index.spec.ts TAP should log a simple info message > must match snapshot 1`] = `
1317
2016-12-01T06:00:00.000Z INFO Simple message {"level":30,"time":1480572000000,"msg":"Simple message"}
1418
`
@@ -28,3 +32,11 @@ exports[`src/tests/index.spec.ts TAP should log an info message with data > must
2832
exports[`src/tests/index.spec.ts TAP should log an info message with requestId > must match snapshot 1`] = `
2933
2016-12-01T06:00:00.000Z 12345 INFO Message with request ID {"level":30,"time":1480572000000,"awsRequestId":"12345","x-correlation-id":"12345","msg":"Message with request ID"}
3034
`
35+
36+
exports[`src/tests/index.spec.ts TAP should log correlation headers > must match snapshot 1`] = `
37+
2016-12-01T06:00:00.000Z 98875 ERROR Message with correlation ids {"level":50,"time":1480572000000,"awsRequestId":"98875","x-correlation-data":"abbb","x-correlation-service":"tyue","x-correlation-id":"98875","msg":"Message with correlation ids"}
38+
`
39+
40+
exports[`src/tests/index.spec.ts TAP should preserve mixins > must match snapshot 1`] = `
41+
2016-12-01T06:00:00.000Z 431234 INFO Message with mixin line 2 {"level":30,"time":1480572000000,"line":2,"awsRequestId":"431234","x-correlation-id":"431234","msg":"Message with mixin line 2"}
42+
`

0 commit comments

Comments
 (0)