Skip to content

Commit 9548d77

Browse files
dreamorosiam29d
andauthored
test(idempotency): migrate to vitest (#3124)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent 4318e53 commit 9548d77

20 files changed

+402
-830
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ jobs:
4343
workspace: [
4444
"packages/batch",
4545
"packages/commons",
46-
"packages/jmespath"
46+
"packages/jmespath",
47+
"packages/idempotency"
4748
]
4849
fail-fast: false
4950
steps:
@@ -90,7 +91,6 @@ jobs:
9091
-w packages/tracer \
9192
-w packages/metrics \
9293
-w packages/parameters \
93-
-w packages/idempotency \
9494
-w packages/parser \
9595
-w packages/event-handler
9696
- name: Run unit tests
@@ -99,7 +99,6 @@ jobs:
9999
-w packages/tracer \
100100
-w packages/metrics \
101101
-w packages/parameters \
102-
-w packages/idempotency \
103102
-w packages/parser \
104103
-w packages/event-handler
105104
check-examples:

Diff for: .husky/pre-push

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ npm t \
22
-w packages/logger \
33
-w packages/metrics \
44
-w packages/tracer \
5-
-w packages/idempotency \
65
-w packages/parameters \
76
-w packages/parser \
87
-w packages/event-handler

Diff for: package-lock.json

+16-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/idempotency/package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
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": "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",
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",
@@ -127,7 +127,6 @@
127127
"@aws-lambda-powertools/testing-utils": "file:../testing",
128128
"@aws-sdk/client-dynamodb": "^3.658.1",
129129
"@aws-sdk/lib-dynamodb": "^3.658.1",
130-
"aws-sdk-client-mock": "^4.0.2",
131-
"aws-sdk-client-mock-jest": "^4.0.2"
130+
"aws-sdk-client-mock": "^4.0.2"
132131
}
133132
}

Diff for: packages/idempotency/src/middleware/makeHandlerIdempotent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const makeHandlerIdempotent = (
122122
});
123123

124124
const idempotencyHandler = new IdempotencyHandler({
125-
functionToMakeIdempotent: /* istanbul ignore next */ () => ({}),
125+
functionToMakeIdempotent: /* v8 ignore next */ () => ({}),
126126
functionArguments: [],
127127
idempotencyConfig,
128128
persistenceStore,
+31-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1+
import { vi } from 'vitest';
12
import { BasePersistenceLayer } from '../../src/persistence/BasePersistenceLayer.js';
3+
import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js';
4+
import type { IdempotencyRecord } from '../../src/persistence/IdempotencyRecord.js';
25

36
/**
47
* Dummy class to test the abstract class BasePersistenceLayer.
58
*
69
* This class is used in the unit tests.
710
*/
811
class PersistenceLayerTestClass extends BasePersistenceLayer {
9-
protected _deleteRecord = jest.fn();
10-
protected _getRecord = jest.fn();
11-
protected _putRecord = jest.fn();
12-
protected _updateRecord = jest.fn();
12+
public _deleteRecord = vi.fn();
13+
public _getRecord = vi.fn();
14+
public _putRecord = vi.fn();
15+
public _updateRecord = vi.fn();
1316
}
1417

15-
export { PersistenceLayerTestClass };
18+
/**
19+
* Dummy class to test the abstract class DynamoDBPersistenceLayer.
20+
*
21+
* This class is used in the unit tests.
22+
*/
23+
class DynamoDBPersistenceLayerTestClass extends DynamoDBPersistenceLayer {
24+
public _deleteRecord(record: IdempotencyRecord): Promise<void> {
25+
return super._deleteRecord(record);
26+
}
27+
28+
public _getRecord(idempotencyKey: string): Promise<IdempotencyRecord> {
29+
return super._getRecord(idempotencyKey);
30+
}
31+
32+
public _putRecord(_record: IdempotencyRecord): Promise<void> {
33+
return super._putRecord(_record);
34+
}
35+
36+
public _updateRecord(record: IdempotencyRecord): Promise<void> {
37+
return super._updateRecord(record);
38+
}
39+
}
40+
41+
export { PersistenceLayerTestClass, DynamoDBPersistenceLayerTestClass };

Diff for: packages/idempotency/tests/tsconfig.json

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

Diff for: packages/idempotency/tests/unit/EnvironmentVariableService.test.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
/**
2-
* Test EnvironmentVariableService class
3-
*
4-
* @group unit/idempotency/environment-variables-service
5-
*/
1+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
62
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
73

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

117
beforeEach(() => {
12-
jest.resetModules();
138
process.env = { ...ENVIRONMENT_VARIABLES };
149
});
1510

@@ -18,9 +13,10 @@ describe('Class: EnvironmentVariableService', () => {
1813
});
1914

2015
describe('Method: getFunctionName', () => {
21-
test('When called, it gets the Lambda function name from the environment variable', () => {
16+
it('gets the Lambda function name from the environment variable', () => {
2217
// Prepare
23-
const expectedName = process.env.AWS_LAMBDA_FUNCTION_NAME;
18+
const expectedName = 'test-function';
19+
process.env.AWS_LAMBDA_FUNCTION_NAME = expectedName;
2420

2521
// Act
2622
const lambdaName = new EnvironmentVariablesService().getFunctionName();
@@ -29,7 +25,7 @@ describe('Class: EnvironmentVariableService', () => {
2925
expect(lambdaName).toEqual(expectedName);
3026
});
3127

32-
test('When called without the environment variable set, it returns an empty string', () => {
28+
it('it returns an empty string when the Lambda function name is not set', () => {
3329
// Prepare
3430
process.env.AWS_LAMBDA_FUNCTION_NAME = undefined;
3531

Diff for: packages/idempotency/tests/unit/IdempotencyConfig.test.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
/**
2-
* Test IdempotencyConfig class
3-
*
4-
* @group unit/idempotency/config
5-
*/
61
import context from '@aws-lambda-powertools/testing-utils/context';
2+
import { afterAll, beforeEach, describe, expect, it } from 'vitest';
73
import { IdempotencyConfig } from '../../src/index.js';
84
import type { IdempotencyConfigOptions } from '../../src/types/index.js';
95

106
describe('Class: IdempotencyConfig', () => {
117
const ENVIRONMENT_VARIABLES = process.env;
128

139
beforeEach(() => {
14-
jest.clearAllMocks();
15-
jest.resetModules();
1610
process.env = { ...ENVIRONMENT_VARIABLES };
1711
});
1812

@@ -21,7 +15,7 @@ describe('Class: IdempotencyConfig', () => {
2115
});
2216

2317
describe('Method: configure', () => {
24-
test('when configured with an empty config object, it initializes the config with default values', () => {
18+
it('initializes the config with default values', () => {
2519
// Prepare
2620
const configOptions = {};
2721

@@ -42,7 +36,7 @@ describe('Class: IdempotencyConfig', () => {
4236
);
4337
});
4438

45-
test('when configured with a config object, it initializes the config with the provided configs', () => {
39+
it('initializes the config with the provided configs', () => {
4640
// Prepare
4741
const configOptions: IdempotencyConfigOptions = {
4842
eventKeyJmesPath: 'eventKeyJmesPath',
@@ -73,7 +67,7 @@ describe('Class: IdempotencyConfig', () => {
7367
});
7468

7569
describe('Method: registerLambdaContext', () => {
76-
test('when called, it stores the provided context', async () => {
70+
it('stores the provided context', async () => {
7771
// Prepare
7872
const config = new IdempotencyConfig({});
7973

0 commit comments

Comments
 (0)