Skip to content

Commit 6b65ab4

Browse files
dreamorosiam29d
andauthored
test(logger): migrate to vitest & custom matchers (aws-powertools#3131)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent e31279a commit 6b65ab4

16 files changed

+354
-239
lines changed

.github/workflows/reusable-run-linting-check-and-unit-tests.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ jobs:
4343
workspace: [
4444
"packages/batch",
4545
"packages/commons",
46-
"packages/jmespath",
46+
"packages/event-handler",
4747
"packages/idempotency",
48-
"packages/event-handler"
48+
"packages/jmespath",
49+
"packages/logger",
4950
]
5051
fail-fast: false
5152
steps:
@@ -88,15 +89,13 @@ jobs:
8889
nodeVersion: ${{ matrix.version }}
8990
- name: Run linting
9091
run: |
91-
npm run lint -w packages/logger \
92-
-w packages/tracer \
92+
npm run lint -w -w packages/tracer \
9393
-w packages/metrics \
9494
-w packages/parameters \
9595
-w packages/parser
9696
- name: Run unit tests
9797
run: |
98-
npm t -w packages/logger \
99-
-w packages/tracer \
98+
npm t -w packages/tracer \
10099
-w packages/metrics \
101100
-w packages/parameters \
102101
-w packages/parser

.husky/pre-push

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
npm t \
2-
-w packages/logger \
32
-w packages/metrics \
43
-w packages/tracer \
54
-w packages/parameters \

packages/logger/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"access": "public"
1111
},
1212
"scripts": {
13-
"test": "npm run test:unit",
14-
"test:unit": "jest --group=unit --detectOpenHandles --coverage --verbose",
15-
"jest": "jest --detectOpenHandles --verbose",
13+
"test": "vitest --run tests/unit",
14+
"test:unit": "vitest --run tests/unit",
15+
"test:unit:coverage": "vitest --run tests/unit --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'",
16+
"test:unit:types": "echo 'Not Implemented'",
1617
"test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e",
1718
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
1819
"test:e2e": "jest --group=e2e",

packages/logger/src/Logger.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,15 @@ class Logger extends Utility implements LoggerInterface {
418418
/**
419419
* @deprecated This method is deprecated and will be removed in the future major versions. Use {@link resetKeys()} instead.
420420
*/
421-
/* istanbul ignore next */
422-
public static injectLambdaContextAfterOrOnError(
421+
/* v8 ignore start */ public static injectLambdaContextAfterOrOnError(
423422
logger: Logger,
424423
_persistentAttributes: LogAttributes,
425424
options?: InjectLambdaContextOptions
426425
): void {
427426
if (options && (options.clearState || options?.resetKeys)) {
428427
logger.resetKeys();
429428
}
430-
}
429+
} /* v8 ignore stop */
431430

432431
/**
433432
* @deprecated - This method is deprecated and will be removed in the next major version.

packages/logger/tests/tsconfig.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"rootDir": "../",
4+
"rootDir": "../../",
55
"noEmit": true
66
},
7-
"include": ["../src/**/*", "./**/*"]
8-
}
7+
"include": [
8+
"../../testing/src/setupEnv.ts",
9+
"../src/**/*",
10+
"./**/*"
11+
]
12+
}

packages/logger/tests/unit/configFromEnv.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
/**
2-
* Test Logger EnvironmentVariablesService class
3-
*
4-
* @group unit/logger/config
5-
*/
6-
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
1+
import { beforeEach, describe, expect, it } from 'vitest';
72
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
83

94
describe('Class: EnvironmentVariablesService', () => {
105
const ENVIRONMENT_VARIABLES = process.env;
116

127
beforeEach(() => {
13-
jest.resetModules();
148
process.env = { ...ENVIRONMENT_VARIABLES };
159
});
1610

packages/logger/tests/unit/formatters.test.ts

+35-44
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
/**
2-
* Test Logger formatter
3-
*
4-
* @group unit/logger/logFormatter
5-
*/
61
import { AssertionError } from 'node:assert';
7-
import {
8-
afterAll,
9-
beforeEach,
10-
describe,
11-
expect,
12-
it,
13-
jest,
14-
} from '@jest/globals';
2+
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
153
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
164
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js';
175
import {
@@ -73,8 +61,6 @@ const unformattedAttributes: UnformattedAttributes = {
7361

7462
process.env.POWERTOOLS_DEV = 'true';
7563

76-
const logSpy = jest.spyOn(console, 'info');
77-
7864
const logger = new Logger();
7965

8066
const jsonReplacerFn: CustomJsonReplacerFn = (_: string, value: unknown) =>
@@ -120,14 +106,13 @@ describe('Formatters', () => {
120106
beforeEach(() => {
121107
process.env = { ...ENVIRONMENT_VARIABLES };
122108
const mockDate = new Date(1466424490000);
123-
jest.useFakeTimers().setSystemTime(mockDate);
124-
jest.resetAllMocks();
109+
vi.useFakeTimers().setSystemTime(mockDate);
110+
vi.resetAllMocks();
125111
unformattedAttributes.timestamp = mockDate;
126112
});
127113

128114
afterAll(() => {
129-
jest.useRealTimers();
130-
unformattedAttributes.timestamp = new Date();
115+
vi.useRealTimers();
131116
});
132117

133118
// #region base log keys
@@ -477,7 +462,7 @@ describe('Formatters', () => {
477462
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
478463
The positive value indicates that `America/New_York` is behind UTC.
479464
*/
480-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
465+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
481466

482467
// Act
483468
const timestamp = formatterWithEnv.formatTimestamp(new Date());
@@ -493,7 +478,7 @@ describe('Formatters', () => {
493478
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
494479
The positive value indicates that `America/New_York` is behind UTC.
495480
*/
496-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
481+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
497482

498483
// Act
499484
const timestamp = formatterWithEnv.formatTimestamp(new Date());
@@ -509,7 +494,7 @@ describe('Formatters', () => {
509494
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
510495
The positive value indicates that `America/New_York` is behind UTC.
511496
*/
512-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
497+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(240);
513498

514499
// Act
515500
const timestamp = formatterWithEnv.formatTimestamp(new Date());
@@ -521,12 +506,12 @@ describe('Formatters', () => {
521506
it('it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone', () => {
522507
// Prepare
523508
process.env.TZ = 'Asia/Dhaka';
524-
jest.useFakeTimers().setSystemTime(new Date('2016-06-20T12:08:10.910Z'));
509+
vi.setSystemTime(new Date('2016-06-20T12:08:10.910Z'));
525510
/*
526511
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
527512
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
528513
*/
529-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
514+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
530515
const formatter = new PowertoolsLogFormatter({
531516
envVarsService: new EnvironmentVariablesService(),
532517
});
@@ -542,12 +527,12 @@ describe('Formatters', () => {
542527
// Prepare
543528
process.env.TZ = 'Asia/Dhaka';
544529
const mockDate = new Date('2016-06-20T20:08:10.910Z');
545-
jest.useFakeTimers().setSystemTime(mockDate);
530+
vi.setSystemTime(mockDate);
546531
/*
547532
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
548533
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
549534
*/
550-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
535+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
551536
const formatter = new PowertoolsLogFormatter({
552537
envVarsService: new EnvironmentVariablesService(),
553538
});
@@ -566,7 +551,7 @@ describe('Formatters', () => {
566551
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
567552
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
568553
*/
569-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
554+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-360);
570555
const formatter = new PowertoolsLogFormatter();
571556

572557
// Act
@@ -579,7 +564,7 @@ describe('Formatters', () => {
579564
it('defaults to :UTC when the TZ env variable is set to :/etc/localtime', () => {
580565
// Prepare
581566
process.env.TZ = ':/etc/localtime';
582-
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0);
567+
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0);
583568
const formatter = new PowertoolsLogFormatter({
584569
envVarsService: new EnvironmentVariablesService(),
585570
});
@@ -676,8 +661,9 @@ describe('Formatters', () => {
676661
logger.info('foo', { circularObject });
677662

678663
// Assess
679-
expect(logSpy).toHaveBeenCalledTimes(1);
680-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
664+
expect(console.info).toHaveBeenCalledTimes(1);
665+
expect(console.info).toHaveLoggedNth(
666+
1,
681667
expect.objectContaining({
682668
level: 'INFO',
683669
message: 'foo',
@@ -696,8 +682,9 @@ describe('Formatters', () => {
696682
logger.info('foo', bigIntValue);
697683

698684
// Assess
699-
expect(logSpy).toHaveBeenCalledTimes(1);
700-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
685+
expect(console.info).toHaveBeenCalledTimes(1);
686+
expect(console.info).toHaveLoggedNth(
687+
1,
701688
expect.objectContaining({
702689
level: 'INFO',
703690
message: 'foo',
@@ -720,8 +707,9 @@ describe('Formatters', () => {
720707
logger.info('foo', values);
721708

722709
// Assess
723-
expect(logSpy).toHaveBeenCalledTimes(1);
724-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
710+
expect(console.info).toHaveBeenCalledTimes(1);
711+
expect(console.info).toHaveLoggedNth(
712+
1,
725713
expect.objectContaining({
726714
level: 'INFO',
727715
message: 'foo',
@@ -738,8 +726,9 @@ describe('Formatters', () => {
738726
loggerWithReplacer.info('foo', valueWithSet);
739727

740728
// Assess
741-
expect(logSpy).toHaveBeenCalledTimes(1);
742-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
729+
expect(console.info).toHaveBeenCalledTimes(1);
730+
expect(console.info).toHaveLoggedNth(
731+
1,
743732
expect.objectContaining({
744733
level: 'INFO',
745734
message: 'foo',
@@ -759,8 +748,9 @@ describe('Formatters', () => {
759748
loggerWithReplacer.info('foo', valueWithSetAndBigInt);
760749

761750
// Assess
762-
expect(logSpy).toHaveBeenCalledTimes(1);
763-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
751+
expect(console.info).toHaveBeenCalledTimes(1);
752+
expect(console.info).toHaveLoggedNth(
753+
1,
764754
expect.objectContaining({
765755
level: 'INFO',
766756
message: 'foo',
@@ -778,8 +768,9 @@ describe('Formatters', () => {
778768
childLogger.info('foo', { foo: new Set([1, 2]) });
779769

780770
// Assess
781-
expect(logSpy).toHaveBeenCalledTimes(1);
782-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual(
771+
expect(console.info).toHaveBeenCalledTimes(1);
772+
expect(console.info).toHaveLoggedNth(
773+
1,
783774
expect.objectContaining({
784775
level: 'INFO',
785776
message: 'foo',
@@ -795,8 +786,8 @@ describe('Formatters', () => {
795786
loggerWithCustomLogFormatter.info('foo');
796787

797788
// Assess
798-
expect(logSpy).toHaveBeenCalledTimes(1);
799-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual({
789+
expect(console.info).toHaveBeenCalledTimes(1);
790+
expect(console.info).toHaveLoggedNth(1, {
800791
logLevel: 12,
801792
message: 'foo',
802793
timestamp: expect.any(String),
@@ -811,8 +802,8 @@ describe('Formatters', () => {
811802
childLogger.info('foo');
812803

813804
// Assess
814-
expect(logSpy).toHaveBeenCalledTimes(1);
815-
expect(JSON.parse(logSpy.mock.calls[0][0])).toEqual({
805+
expect(console.info).toHaveBeenCalledTimes(1);
806+
expect(console.info).toHaveLoggedNth(1, {
816807
logLevel: 12,
817808
message: 'foo',
818809
timestamp: expect.any(String),

0 commit comments

Comments
 (0)