Skip to content

Commit e7a8c8c

Browse files
authored
test(commons): migrate to vitest (#3060)
1 parent 5db27bd commit e7a8c8c

16 files changed

+126
-290
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
strategy:
4141
matrix:
4242
version: [18, 20]
43-
workspace: ["packages/batch"]
43+
workspace: ["packages/batch", "packages/commons"]
4444
fail-fast: false
4545
steps:
4646
- name: Checkout code
@@ -57,7 +57,9 @@ jobs:
5757
- name: Linting
5858
run: npm run lint -w ${{ matrix.workspace }}
5959
- name: Unit tests
60-
run: npm run test:unit:coverage -w ${{ matrix.workspace }}
60+
run: |
61+
npm run test:unit:coverage -w ${{ matrix.workspace }}
62+
npm run test:unit:types -w ${{ matrix.workspace }}
6163
run-linting-check-and-unit-tests-on-utilities:
6264
runs-on: ubuntu-latest
6365
env:
@@ -92,8 +94,7 @@ jobs:
9294
-w packages/event-handler
9395
- name: Run unit tests
9496
run: |
95-
npm t -w packages/commons \
96-
-w packages/logger \
97+
npm t -w packages/logger \
9798
-w packages/tracer \
9899
-w packages/metrics \
99100
-w packages/parameters \

.husky/pre-push

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

packages/batch/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"scripts": {
1313
"test": "vitest --run",
1414
"test:unit": "vitest --run",
15-
"test:unit:coverage": "vitest --run --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'",
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": "echo 'Not Implemented'",
1718
"test:e2e:nodejs20x": "echo 'Not Implemented'",
1819
"test:e2e": "echo 'Not Implemented'",
19-
"watch": "jest --watch",
2020
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2121
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2222
"build": "npm run build:esm & npm run build:cjs",

packages/commons/jest.config.cjs

-29
This file was deleted.

packages/commons/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
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",
14+
"test:unit": "vitest --run",
15+
"test:unit:coverage": "vitest --run tests/unit --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'",
16+
"test:unit:types": "vitest --run tests/types --typecheck",
1617
"test:e2e": "echo 'Not Applicable'",
17-
"watch": "jest --watch",
1818
"generateVersionFile": "echo \"// this file is auto generated, do not modify\nexport const PT_VERSION = '$(jq -r '.version' package.json)';\" > src/version.ts",
1919
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2020
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Callback, Context } from 'aws-lambda';
2+
import { describe, expectTypeOf, it } from 'vitest';
3+
import type { LambdaInterface } from '../../src/types/index.js';
4+
5+
describe('Type: LambdaInterface', () => {
6+
it('works with a sync handler', () => {
7+
// Prepare
8+
class Lambda implements LambdaInterface {
9+
public handler(_event: unknown, context: Context, _callback: Callback) {
10+
context.getRemainingTimeInMillis();
11+
_callback(null, 'Hello World');
12+
}
13+
}
14+
15+
// Act
16+
const lambda = new Lambda();
17+
18+
// Assess
19+
expectTypeOf(lambda).toBeObject();
20+
expectTypeOf(lambda).toHaveProperty('handler');
21+
expectTypeOf(lambda.handler).toBeFunction();
22+
});
23+
24+
it('works with an async handler', async () => {
25+
// Prepare
26+
class Lambda implements LambdaInterface {
27+
public async handler(_event: unknown, context: Context) {
28+
context.getRemainingTimeInMillis();
29+
return 'Hello World';
30+
}
31+
}
32+
33+
// Act
34+
const lambda = new Lambda();
35+
36+
// Assess
37+
expectTypeOf(lambda).toBeObject();
38+
expectTypeOf(lambda).toHaveProperty('handler');
39+
expectTypeOf(lambda.handler).toBeFunction();
40+
});
41+
});

packages/commons/tests/unit/EnvironmentVariablesService.test.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
/**
2-
* Test EnvironmentVariablesService class
3-
*
4-
* @group unit/commons/environmentService
5-
*/
1+
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
62
import { EnvironmentVariablesService } from '../../src/index.js';
73

84
describe('Class: EnvironmentVariablesService', () => {
95
const ENVIRONMENT_VARIABLES = process.env;
106

117
beforeEach(() => {
12-
jest.resetModules();
8+
vi.resetModules();
139
process.env = { ...ENVIRONMENT_VARIABLES };
1410
});
1511

@@ -18,7 +14,7 @@ describe('Class: EnvironmentVariablesService', () => {
1814
});
1915

2016
describe('Method: get', () => {
21-
test('When the variable IS present, it returns the value of a runtime variable', () => {
17+
it('returns the value of a runtime variable', () => {
2218
// Prepare
2319
process.env.CUSTOM_VARIABLE = 'my custom value';
2420
const service = new EnvironmentVariablesService();
@@ -30,7 +26,7 @@ describe('Class: EnvironmentVariablesService', () => {
3026
expect(value).toEqual('my custom value');
3127
});
3228

33-
test('When the variable IS NOT present, it returns an empty string', () => {
29+
it('returns an empty string when the env variable is not present', () => {
3430
// Prepare
3531
process.env.CUSTOM_VARIABLE = undefined;
3632
const service = new EnvironmentVariablesService();
@@ -44,7 +40,7 @@ describe('Class: EnvironmentVariablesService', () => {
4440
});
4541

4642
describe('Method: getServiceName', () => {
47-
test('It returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => {
43+
it('returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => {
4844
// Prepare
4945
process.env.POWERTOOLS_SERVICE_NAME = 'shopping-cart-api';
5046
const service = new EnvironmentVariablesService();
@@ -58,7 +54,7 @@ describe('Class: EnvironmentVariablesService', () => {
5854
});
5955

6056
describe('Method: getXrayTraceId', () => {
61-
test('It returns the value of the environment variable _X_AMZN_TRACE_ID', () => {
57+
it('returns the value of the environment variable _X_AMZN_TRACE_ID', () => {
6258
// Prepare
6359
process.env._X_AMZN_TRACE_ID = 'abcd123456789';
6460
const service = new EnvironmentVariablesService();
@@ -69,7 +65,7 @@ describe('Class: EnvironmentVariablesService', () => {
6965
// Assess
7066
expect(value).toEqual('abcd123456789');
7167
});
72-
test('It returns the value of the Root X-Ray segment ID properly formatted', () => {
68+
it('returns the value of the Root X-Ray segment ID properly formatted', () => {
7369
// Prepare
7470
process.env._X_AMZN_TRACE_ID =
7571
'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
@@ -82,7 +78,7 @@ describe('Class: EnvironmentVariablesService', () => {
8278
expect(value).toEqual('1-5759e988-bd862e3fe1be46a994272793');
8379
});
8480

85-
test('It returns the value of the Root X-Ray segment ID properly formatted', () => {
81+
it('returns the value of the Root X-Ray segment ID properly formatted', () => {
8682
// Prepare
8783
process.env._X_AMZN_TRACE_ID = undefined;
8884
const service = new EnvironmentVariablesService();
@@ -96,7 +92,7 @@ describe('Class: EnvironmentVariablesService', () => {
9692
});
9793

9894
describe('Method: getXrayTraceSampled', () => {
99-
test('It returns true if the Sampled flag is set in the _X_AMZN_TRACE_ID environment variable', () => {
95+
it('returns true if the Sampled flag is set in the _X_AMZN_TRACE_ID environment variable', () => {
10096
// Prepare
10197
process.env._X_AMZN_TRACE_ID =
10298
'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
@@ -109,7 +105,7 @@ describe('Class: EnvironmentVariablesService', () => {
109105
expect(value).toEqual(true);
110106
});
111107

112-
test('It returns false if the Sampled flag is not set in the _X_AMZN_TRACE_ID environment variable', () => {
108+
it('returns false if the Sampled flag is not set in the _X_AMZN_TRACE_ID environment variable', () => {
113109
// Prepare
114110
process.env._X_AMZN_TRACE_ID =
115111
'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047';
@@ -122,7 +118,7 @@ describe('Class: EnvironmentVariablesService', () => {
122118
expect(value).toEqual(false);
123119
});
124120

125-
it('It returns false when no _X_AMZN_TRACE_ID environment variable is present', () => {
121+
it('returns false when no _X_AMZN_TRACE_ID environment variable is present', () => {
126122
// Prepare
127123
process.env._X_AMZN_TRACE_ID = undefined;
128124
const service = new EnvironmentVariablesService();
@@ -150,8 +146,8 @@ describe('Class: EnvironmentVariablesService', () => {
150146
['0', false],
151147
];
152148

153-
test.each(valuesToTest)(
154-
'it takes string "%s" and returns %s',
149+
it.each(valuesToTest)(
150+
'takes string "%s" and returns %s',
155151
(input, output) => {
156152
// Prepare
157153
const service = new EnvironmentVariablesService();
@@ -164,7 +160,7 @@ describe('Class: EnvironmentVariablesService', () => {
164160
});
165161

166162
describe('Method: isDevMode', () => {
167-
test('it returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
163+
it('returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
168164
// Prepare
169165
process.env.POWERTOOLS_DEV = 'true';
170166
const service = new EnvironmentVariablesService();
@@ -176,7 +172,7 @@ describe('Class: EnvironmentVariablesService', () => {
176172
expect(value).toEqual(true);
177173
});
178174

179-
test('it returns false if the environment variable POWERTOOLS_DEV is "false"', () => {
175+
it('returns false if the environment variable POWERTOOLS_DEV is "false"', () => {
180176
// Prepare
181177
process.env.POWERTOOLS_DEV = 'false';
182178
const service = new EnvironmentVariablesService();
@@ -188,7 +184,7 @@ describe('Class: EnvironmentVariablesService', () => {
188184
expect(value).toEqual(false);
189185
});
190186

191-
test('it returns false if the environment variable POWERTOOLS_DEV is NOT set', () => {
187+
it('returns false if the environment variable POWERTOOLS_DEV is NOT set', () => {
192188
// Prepare
193189
process.env.POWERTOOLS_DEV = 'somethingsilly';
194190
const service = new EnvironmentVariablesService();
@@ -200,7 +196,7 @@ describe('Class: EnvironmentVariablesService', () => {
200196
expect(value).toEqual(false);
201197
});
202198

203-
test('it returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => {
199+
it('returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => {
204200
// Prepare
205201
process.env.POWERTOOLS_DEV = 'somethingsilly';
206202
const service = new EnvironmentVariablesService();

packages/commons/tests/unit/LRUCache.test.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
/**
2-
* Test LRUCache class
3-
*
4-
* @group unit/commons/lru-cache
5-
*/
1+
import { describe, expect, it } from 'vitest';
62
import { LRUCache } from '../../src/LRUCache.js';
73

84
describe('Class: LRUMap', () => {
95
describe('Method: add', () => {
10-
test('when called it adds items to the cache', () => {
6+
it('adds items to the cache', () => {
117
// Prepare
128
const cache = new LRUCache();
139

@@ -21,7 +17,7 @@ describe('Class: LRUMap', () => {
2117
expect(cache.get('b')).toBe(2);
2218
});
2319

24-
test('when called it updates the value of an existing key', () => {
20+
it('updates the value of an existing key', () => {
2521
// Prepare
2622
const cache = new LRUCache();
2723
cache.add('a', 1);
@@ -34,7 +30,7 @@ describe('Class: LRUMap', () => {
3430
expect(cache.get('a')).toBe(2);
3531
});
3632

37-
test('when called it removes the oldest item when the cache is full', () => {
33+
it('removes the oldest item when the cache is full', () => {
3834
// Prepare
3935
const cache = new LRUCache({ maxSize: 2 });
4036
cache.add('a', 1);
@@ -50,7 +46,7 @@ describe('Class: LRUMap', () => {
5046
expect(cache.get('c')).toBe(3);
5147
});
5248

53-
test('when called and maxSize is 0, it skips cache', () => {
49+
it('it skips the cache when max size is zero', () => {
5450
// Prepare
5551
const cache = new LRUCache({ maxSize: 0 });
5652

@@ -63,7 +59,7 @@ describe('Class: LRUMap', () => {
6359
});
6460

6561
describe('Method: get', () => {
66-
test('when called it returns the value of an existing key', () => {
62+
it('returns the value of an existing key', () => {
6763
// Prepare
6864
const cache = new LRUCache();
6965
cache.add('a', 1);
@@ -75,7 +71,7 @@ describe('Class: LRUMap', () => {
7571
expect(value).toBe(1);
7672
});
7773

78-
test('when called it returns undefined for a non-existing key', () => {
74+
it('returns undefined for a non-existing key', () => {
7975
// Prepare
8076
const cache = new LRUCache();
8177

@@ -86,7 +82,7 @@ describe('Class: LRUMap', () => {
8682
expect(value).toBeUndefined();
8783
});
8884

89-
test('when called it marks the item as the most recently used', () => {
85+
it('marks the item as the most recently used', () => {
9086
// Prepare
9187
const cache = new LRUCache();
9288
cache.add('a', 1);
@@ -104,7 +100,7 @@ describe('Class: LRUMap', () => {
104100
});
105101

106102
describe('Method: has', () => {
107-
test('when called it returns true for an existing key', () => {
103+
it('returns true for an existing key', () => {
108104
// Prepare
109105
const cache = new LRUCache();
110106
cache.add('a', 1);
@@ -116,7 +112,7 @@ describe('Class: LRUMap', () => {
116112
expect(hasKey).toBe(true);
117113
});
118114

119-
test('when called it returns false for a non-existing key', () => {
115+
it('returns false for a non-existing key', () => {
120116
// Prepare
121117
const cache = new LRUCache();
122118

@@ -129,7 +125,7 @@ describe('Class: LRUMap', () => {
129125
});
130126

131127
describe('Method: remove', () => {
132-
test('when called it removes the item from the cache', () => {
128+
it('removes the item from the cache', () => {
133129
// Prepare
134130
const cache = new LRUCache();
135131
cache.add('a', 1);
@@ -146,7 +142,7 @@ describe('Class: LRUMap', () => {
146142
expect(cache.get('a')).toBeUndefined();
147143
});
148144

149-
test('when called on an empty cache it does nothing', () => {
145+
it('it does nothing when called on a non-existing key', () => {
150146
// Prepare
151147
const cache = new LRUCache();
152148
cache.add('a', 1);

0 commit comments

Comments
 (0)