Skip to content

Commit 708a18f

Browse files
committed
feat: test support for [email protected]
1 parent 886a575 commit 708a18f

File tree

6 files changed

+1069
-708
lines changed

6 files changed

+1069
-708
lines changed

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"prebuild": "yarn clean",
1717
"pretest:e2e": "yarn clean && yarn build",
1818
"test": "yarn test:unit",
19-
"test:unit": "tap src/**/*.spec.ts",
19+
"test:unit": "tap --ts src/**/*.spec.ts",
2020
"test:debug": "yarn test:unit --node-arg=--inspect-brk --timeout 600",
2121
"test:e2e": "node ./dist/test.js",
2222
"semantic-release": "semantic-release"
@@ -39,21 +39,22 @@
3939
"@types/node": "12",
4040
"@types/pino": "^6.0.0",
4141
"@types/sinon": "^9.0.10",
42-
"@types/tap": "^14.10.1",
42+
"@types/tap": "^15.0.7",
4343
"@typescript-eslint/eslint-plugin": "^4.11.0",
4444
"@typescript-eslint/parser": "^4.11.0",
4545
"eslint": "^7.16.0",
4646
"eslint-config-prettier": "^7.1.0",
4747
"eslint-plugin-jest": "^24.1.3",
4848
"eslint-plugin-prettier": "^3.3.0",
4949
"husky": "^4.3.6",
50-
"pino": "^6.0.0",
51-
"pino-pretty": "^4.0.0",
50+
"pino": "^8.0.0",
51+
"pino-pretty": "^9.1.0",
5252
"prettier": "^2.2.1",
5353
"rimraf": "^3.0.2",
5454
"semantic-release": "^17.3.1",
5555
"sinon": "^9.2.2",
56-
"tap": "^14.11.0",
56+
"tap": "^16.3.0",
57+
"ts-node": "^10.9.1",
5758
"typescript": "^4.1.3"
5859
},
5960
"peerDependencies": {

src/destination.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import { PinoLambdaOptions } from './types';
77
* Custom destination stream for pino
88
* @param options Options
99
*/
10-
export const pinoLambdaDestination = (options: PinoLambdaOptions = {}): Writable => {
10+
export const pinoLambdaDestination = (
11+
/* istanbul ignore next */
12+
options: PinoLambdaOptions = {},
13+
): Writable => {
1114
const writeable = new Writable({
1215
defaultEncoding: 'utf8',
1316
write(chunk, encoding, callback) {
1417
const storageProvider = options.storageProvider || GlobalContextStorageProvider;
15-
const formatter = options?.formatter || new CloudwatchLogFormatter();
18+
const formatter = options.formatter || new CloudwatchLogFormatter();
1619

1720
const data = JSON.parse(chunk);
1821
const lambdaContext = storageProvider.getContext() || {};
@@ -26,6 +29,7 @@ export const pinoLambdaDestination = (options: PinoLambdaOptions = {}): Writable
2629
// final entry must end with carriage return
2730
output += '\n';
2831

32+
/* istanbul ignore else */
2933
if (options.streamWriter) {
3034
options.streamWriter(output);
3135
} else {

src/request.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const CORRELATION_TRACE_ID = `${CORRELATION_HEADER}trace-id`;
1010
* Creates a function for tracing Lambda request context across logging calls
1111
* @param options The request options
1212
*/
13-
export const lambdaRequestTracker = (options: LambdaRequestTrackerOptions = {}) => (
14-
event: LambdaEvent,
15-
context: LambdaContext,
16-
): void => {
13+
export const lambdaRequestTracker = (
14+
/* istanbul ignore next */
15+
options: LambdaRequestTrackerOptions = {},
16+
) => (event: LambdaEvent, context: LambdaContext): void => {
1717
const ctx: ContextMap = {
1818
awsRequestId: context.awsRequestId,
1919
};
@@ -40,6 +40,7 @@ export const lambdaRequestTracker = (options: LambdaRequestTrackerOptions = {})
4040
}
4141

4242
// set the correlation id if not already set by upstream callers
43+
/* istanbul ignore next */
4344
if (!ctx[CORRELATION_ID]) {
4445
ctx[CORRELATION_ID] = context.awsRequestId;
4546
}
@@ -55,6 +56,7 @@ export const lambdaRequestTracker = (options: LambdaRequestTrackerOptions = {})
5556
}
5657

5758
const storageProvider = options.storageProvider || GlobalContextStorageProvider;
59+
/* istanbul ignore next */
5860
if (storageProvider) {
5961
storageProvider.setContext(ctx);
6062
}

src/tests/index.spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ tap.test('should log an error message with requestId', (t) => {
4949
t.end();
5050
});
5151

52+
tap.test('should log capitalize string levels', (t) => {
53+
const { log, output } = createLogger();
54+
55+
log.info({ level: 'debug' }, 'Simple message');
56+
t.matchSnapshot(output.buffer);
57+
t.end();
58+
});
59+
60+
tap.test('should not fail if level is an object', (t) => {
61+
const { log, output } = createLogger();
62+
63+
log.info({ level: {} }, 'Simple message');
64+
t.matchSnapshot(output.buffer);
65+
t.end();
66+
});
67+
68+
tap.test('should not fail if level number is invalid', (t) => {
69+
const { log, output } = createLogger();
70+
71+
log.info({ level: 98 }, 'Simple message');
72+
t.matchSnapshot(output.buffer);
73+
t.end();
74+
});
75+
5276
tap.test('should log correlation headers', (t) => {
5377
const { log, output, withRequest } = createLogger();
5478

@@ -61,6 +85,22 @@ tap.test('should log correlation headers', (t) => {
6185
t.end();
6286
});
6387

88+
tap.test('should set correlation if to trace id if present', (t) => {
89+
const { log, output, withRequest } = createLogger();
90+
91+
process.env['_X_AMZN_TRACE_ID'] = '168181818';
92+
93+
withRequest(
94+
{},
95+
{ awsRequestId: '98875' },
96+
);
97+
log.error('Message with trace id');
98+
t.matchSnapshot(output.buffer);
99+
100+
delete process.env['_X_AMZN_TRACE_ID'];
101+
t.end();
102+
});
103+
64104
tap.test('should log an error message with apiRequestId', (t) => {
65105
const { log, output, withRequest } = createLogger();
66106

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

+16
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,26 @@ exports[`src/tests/index.spec.ts TAP should log an info message with requestId >
4545
2016-12-01T06:00:00.000Z 12345 INFO Message with request ID {"awsRequestId":"12345","x-correlation-id":"12345","level":30,"time":1480572000000,"msg":"Message with request ID"}
4646
`
4747

48+
exports[`src/tests/index.spec.ts TAP should log capitalize string levels > must match snapshot 1`] = `
49+
2016-12-01T06:00:00.000Z 12345 DEBUG Simple message {"awsRequestId":"12345","x-correlation-id":"12345","level":"debug","time":1480572000000,"msg":"Simple message"}
50+
`
51+
4852
exports[`src/tests/index.spec.ts TAP should log correlation headers > must match snapshot 1`] = `
4953
2016-12-01T06:00:00.000Z 98875 ERROR Message with correlation ids {"awsRequestId":"98875","x-correlation-data":"abbb","x-correlation-service":"tyue","x-correlation-id":"98875","level":50,"time":1480572000000,"msg":"Message with correlation ids"}
5054
`
5155

56+
exports[`src/tests/index.spec.ts TAP should not fail if level is an object > must match snapshot 1`] = `
57+
2016-12-01T06:00:00.000Z 12345 [object Object] Simple message {"awsRequestId":"12345","x-correlation-id":"12345","level":{},"time":1480572000000,"msg":"Simple message"}
58+
`
59+
60+
exports[`src/tests/index.spec.ts TAP should not fail if level number is invalid > must match snapshot 1`] = `
61+
2016-12-01T06:00:00.000Z 12345 undefined Simple message {"awsRequestId":"12345","x-correlation-id":"12345","level":98,"time":1480572000000,"msg":"Simple message"}
62+
`
63+
5264
exports[`src/tests/index.spec.ts TAP should preserve mixins > must match snapshot 1`] = `
5365
2016-12-01T06:00:00.000Z 431234 INFO Message with mixin line 2 {"awsRequestId":"431234","x-correlation-id":"431234","level":30,"time":1480572000000,"line":2,"msg":"Message with mixin line 2"}
5466
`
67+
68+
exports[`src/tests/index.spec.ts TAP should set correlation if to trace id if present > must match snapshot 1`] = `
69+
2016-12-01T06:00:00.000Z 98875 ERROR Message with trace id {"awsRequestId":"98875","x-correlation-trace-id":"168181818","x-correlation-id":"98875","level":50,"time":1480572000000,"msg":"Message with trace id"}
70+
`

0 commit comments

Comments
 (0)