Skip to content

Commit e20aa35

Browse files
committed
chore: migrated postDatedLambda tests passing
1 parent 60e3606 commit e20aa35

9 files changed

Lines changed: 141 additions & 120 deletions

File tree

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
7-
{
8-
"type": "node",
9-
"name": "vscode-jest-tests.v2",
10-
"request": "launch",
11-
"args": [
12-
"--runInBand",
13-
"--watchAll=false",
14-
"--testNamePattern",
15-
"${jest.testNamePattern}",
16-
"--runTestsByPath",
17-
"${jest.testFile}",
18-
"--config",
19-
"${workspaceFolder}/jest.debug.config.ts"
20-
],
21-
"cwd": "${workspaceFolder}",
22-
"console": "integratedTerminal",
23-
"internalConsoleOptions": "neverOpen",
24-
"disableOptimisticBPs": true,
25-
"program": "${workspaceFolder}/../../node_modules/.bin/jest",
26-
"windows": {
27-
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
28-
},
29-
"env": {
30-
"POWERTOOLS_DEV": true,
31-
"NODE_OPTIONS": "--experimental-vm-modules"
32-
}
33-
}
4+
{
5+
"name": "vitest: all tests",
6+
"type": "node",
7+
"request": "launch",
8+
"cwd": "${workspaceFolder}",
9+
"runtimeExecutable": "${workspaceFolder}/../../node_modules/vitest/vitest.mjs",
10+
"args": [
11+
"run",
12+
"--config",
13+
"vitest.config.ts"
14+
],
15+
"env": {
16+
"POWERTOOLS_DEV": "true"
17+
},
18+
"console": "integratedTerminal",
19+
"internalConsoleOptions": "neverOpen"
20+
},
21+
{
22+
"name": "vitest: current file",
23+
"type": "node",
24+
"request": "launch",
25+
"cwd": "${workspaceFolder}",
26+
"runtimeExecutable": "${workspaceFolder}/../../node_modules/vitest/vitest.mjs",
27+
"args": [
28+
"run",
29+
"${relativeFile}",
30+
"--config",
31+
"vitest.config.ts"
32+
],
33+
"env": {
34+
"POWERTOOLS_DEV": "true"
35+
},
36+
"console": "integratedTerminal",
37+
"internalConsoleOptions": "neverOpen"
38+
}
3439
]
3540
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"jest.jestCommandLine": "/workspaces/eps-prescription-status-update-api/node_modules/.bin/jest --no-cache",
3-
"jest.nodeEnv": {
4-
"POWERTOOLS_DEV": true,
5-
"NODE_OPTIONS": "--experimental-vm-modules"
6-
}
2+
"testing.automaticallyOpenPeekView": "never"
73
}

packages/postDatedLambda/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "MIT",
88
"type": "module",
99
"scripts": {
10-
"unit": "POWERTOOLS_DEV=true NODE_OPTIONS=--experimental-vm-modules jest --no-cache --coverage",
10+
"unit": "POWERTOOLS_DEV=true NODE_OPTIONS=--max-old-space-size=8192 vitest run --config vitest.config.ts --coverage",
1111
"lint": "eslint --max-warnings 0 --fix --config ../../eslint.config.mjs .",
1212
"compile": "tsc",
1313
"test": "npm run compile && npm run unit",

packages/postDatedLambda/tests/testBusinessLogic.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
describe,
44
it,
55
afterEach,
6-
jest
7-
} from "@jest/globals"
6+
vi
7+
} from "vitest"
88

99
import {Logger} from "@aws-lambda-powertools/logger"
1010

@@ -21,7 +21,7 @@ async function loadBusinessLogic(
2121
envOverrides = {}
2222
): Promise<BusinessLogicModule> {
2323
// Makes sure that the environment is set before import each time
24-
jest.resetModules()
24+
vi.resetModules()
2525
process.env = {...ORIGINAL_ENV, ...envOverrides}
2626
return import("../src/businessLogic")
2727
}
@@ -77,7 +77,7 @@ function createMessage(
7777

7878
afterEach(() => {
7979
process.env = {...ORIGINAL_ENV}
80-
jest.useRealTimers()
80+
vi.useRealTimers()
8181
})
8282

8383
describe("businessLogic", () => {
@@ -185,8 +185,8 @@ describe("businessLogic", () => {
185185
})
186186

187187
it("should classify a message as immature when LastModified is in the future", async () => {
188-
jest.useFakeTimers()
189-
jest.setSystemTime(new Date("2026-01-01T12:00:00.000Z"))
188+
vi.useFakeTimers()
189+
vi.setSystemTime(new Date("2026-01-01T12:00:00.000Z"))
190190
const {determineAction} = await loadBusinessLogic()
191191
const logger = new Logger({serviceName: "post-dated-tests"})
192192
const message = createMessage({
@@ -203,8 +203,8 @@ describe("businessLogic", () => {
203203
})
204204

205205
it("should classify a message as matured when LastModified is in the past", async () => {
206-
jest.useFakeTimers()
207-
jest.setSystemTime(new Date("2026-01-03T12:00:00.000Z"))
206+
vi.useFakeTimers()
207+
vi.setSystemTime(new Date("2026-01-03T12:00:00.000Z"))
208208
const {determineAction} = await loadBusinessLogic()
209209
const logger = new Logger({serviceName: "post-dated-tests"})
210210
const message = createMessage({
@@ -221,8 +221,8 @@ describe("businessLogic", () => {
221221
})
222222

223223
it("should use the provided most recent record when determining maturity", async () => {
224-
jest.useFakeTimers()
225-
jest.setSystemTime(new Date("2026-01-05T12:00:00.000Z"))
224+
vi.useFakeTimers()
225+
vi.setSystemTime(new Date("2026-01-05T12:00:00.000Z"))
226226
const {determineAction} = await loadBusinessLogic()
227227
const logger = new Logger({serviceName: "post-dated-tests"})
228228
const message = createMessage({

packages/postDatedLambda/tests/testDatabaseClient.test.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@ import {
22
expect,
33
describe,
44
it,
5-
jest
6-
} from "@jest/globals"
7-
8-
import * as dynamo from "@aws-sdk/client-dynamodb"
5+
vi,
6+
beforeEach
7+
} from "vitest"
98

109
import {Logger} from "@aws-lambda-powertools/logger"
1110

1211
import {createMockPostModifiedDataItem} from "./testUtils"
1312

14-
// Uses unstable jest method to enable mocking while using ESM. To be replaced in future.
15-
export function mockDynamoDBClient() {
16-
const mockSend = jest.fn()
17-
jest.unstable_mockModule("@aws-sdk/client-dynamodb", () => {
18-
return {
19-
...dynamo,
20-
DynamoDBClient: jest.fn().mockImplementation(() => ({
21-
send: mockSend
22-
}))
23-
}
24-
})
25-
return {mockSend}
26-
}
27-
28-
const {mockSend} = mockDynamoDBClient()
13+
const {mockSend} = vi.hoisted(() => ({
14+
mockSend: vi.fn()
15+
}))
16+
17+
vi.mock("@aws-sdk/client-dynamodb", async () => {
18+
const dynamo = await vi.importActual<typeof import("@aws-sdk/client-dynamodb")>("@aws-sdk/client-dynamodb")
19+
return {
20+
...dynamo,
21+
DynamoDBClient: vi.fn().mockImplementation(() => ({
22+
send: mockSend
23+
}))
24+
}
25+
})
2926
const {
3027
getRecentDataItemByPrescriptionID,
3128
enrichMessagesWithMostRecentDataItem

packages/postDatedLambda/tests/testMain.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import {
2-
jest,
2+
vi,
3+
expect,
34
describe,
45
it,
56
beforeAll,
67
afterEach
7-
} from "@jest/globals"
8+
} from "vitest"
89

9-
const mockReportQueueStatus = jest.fn()
10-
jest.unstable_mockModule(
10+
const {
11+
mockReportQueueStatus,
12+
mockProcessPostDatedQueue
13+
} = vi.hoisted(() => ({
14+
mockReportQueueStatus: vi.fn(),
15+
mockProcessPostDatedQueue: vi.fn()
16+
}))
17+
18+
vi.mock(
1119
"../src/sqs",
1220
async () => ({
1321
__esModule: true,
1422
reportQueueStatus: mockReportQueueStatus
1523
})
1624
)
1725

18-
const mockProcessPostDatedQueue = jest.fn()
19-
jest.unstable_mockModule(
26+
vi.mock(
2027
"../src/orchestration",
2128
async () => ({
2229
__esModule: true,
@@ -37,8 +44,8 @@ describe("Unit test for post-dated lambda handler", () => {
3744
afterEach(() => {
3845
process.env = {...ORIGINAL_ENV}
3946

40-
jest.clearAllMocks()
41-
jest.restoreAllMocks()
47+
vi.clearAllMocks()
48+
vi.restoreAllMocks()
4249
})
4350

4451
it("should run the lambda handler successfully", async () => {

packages/postDatedLambda/tests/testOrchestration.test.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,46 @@ import {
22
expect,
33
describe,
44
it,
5-
jest
6-
} from "@jest/globals"
5+
vi,
6+
beforeEach,
7+
afterEach
8+
} from "vitest"
79

810
// Mock the imports from local modules
9-
const mockDetermineAction = jest.fn()
10-
const mockComputeTimeUntilMaturity = jest.fn().mockReturnValue(300)
11-
jest.unstable_mockModule("../src/businessLogic", () => {
11+
const {
12+
mockDetermineAction,
13+
mockComputeTimeUntilMaturity,
14+
mockEnrichMessagesWithMostRecentDataItem,
15+
mockReceivePostDatedSQSMessages,
16+
mockReportQueueStatus,
17+
mockForwardSQSMessageToNotificationQueue,
18+
mockRemoveSQSMessage,
19+
mockReturnMessageToQueue
20+
} = vi.hoisted(() => ({
21+
mockDetermineAction: vi.fn(),
22+
mockComputeTimeUntilMaturity: vi.fn().mockReturnValue(300),
23+
mockEnrichMessagesWithMostRecentDataItem: vi.fn(),
24+
mockReceivePostDatedSQSMessages: vi.fn(),
25+
mockReportQueueStatus: vi.fn(),
26+
mockForwardSQSMessageToNotificationQueue: vi.fn(),
27+
mockRemoveSQSMessage: vi.fn(),
28+
mockReturnMessageToQueue: vi.fn()
29+
}))
30+
31+
vi.mock("../src/businessLogic", () => {
1232
return {
1333
determineAction: mockDetermineAction,
1434
computeTimeUntilMaturity: mockComputeTimeUntilMaturity
1535
}
1636
})
1737

18-
const mockEnrichMessagesWithMostRecentDataItem = jest.fn()
19-
jest.unstable_mockModule("../src/databaseClient", () => {
38+
vi.mock("../src/databaseClient", () => {
2039
return {
2140
enrichMessagesWithMostRecentDataItem: mockEnrichMessagesWithMostRecentDataItem
2241
}
2342
})
2443

25-
const mockReceivePostDatedSQSMessages = jest.fn()
26-
const mockReportQueueStatus = jest.fn()
27-
const mockForwardSQSMessageToNotificationQueue = jest.fn()
28-
const mockRemoveSQSMessage = jest.fn()
29-
const mockReturnMessageToQueue = jest.fn()
30-
jest.unstable_mockModule("../src/sqs", () => {
44+
vi.mock("../src/sqs", () => {
3145
return {
3246
receivePostDatedSQSMessages: mockReceivePostDatedSQSMessages,
3347
reportQueueStatus: mockReportQueueStatus,
@@ -66,7 +80,7 @@ function enrich(messages: Array<PostDatedSQSMessage>): Array<PostDatedSQSMessage
6680
describe("orchestration", () => {
6781
describe("processMessages", () => {
6882
beforeEach(() => {
69-
jest.clearAllMocks()
83+
vi.clearAllMocks()
7084
mockForwardSQSMessageToNotificationQueue.mockReturnValue(Promise.resolve("forwarded-id"))
7185
mockRemoveSQSMessage.mockReturnValue(Promise.resolve())
7286
mockReturnMessageToQueue.mockReturnValue(Promise.resolve())
@@ -125,12 +139,17 @@ describe("orchestration", () => {
125139

126140
describe("processPostDatedQueue", () => {
127141
beforeEach(() => {
128-
jest.clearAllMocks()
142+
vi.clearAllMocks()
129143
mockForwardSQSMessageToNotificationQueue.mockReturnValue(Promise.resolve("forwarded-id"))
130144
mockRemoveSQSMessage.mockReturnValue(Promise.resolve())
131145
mockReturnMessageToQueue.mockReturnValue(Promise.resolve())
132146
})
133147

148+
afterEach(() => {
149+
vi.useRealTimers()
150+
vi.restoreAllMocks()
151+
})
152+
134153
it("should process the SQS queue correctly", async () => {
135154
const mockMessages: Array<PostDatedSQSMessage> = [
136155
{MessageId: "1", Body: "Message 1", prescriptionData: createMockPostModifiedDataItem({})},
@@ -157,7 +176,6 @@ describe("orchestration", () => {
157176
})
158177

159178
it("Should stop processing if the max runtime is exceeded", async () => {
160-
jest.useFakeTimers()
161179
const mockMessages: Array<PostDatedSQSMessage> = [
162180
{MessageId: "1", Body: "Message 1", prescriptionData: createMockPostModifiedDataItem({})},
163181
{MessageId: "2", Body: "Message 2", prescriptionData: createMockPostModifiedDataItem({})},
@@ -171,14 +189,13 @@ describe("orchestration", () => {
171189
mockEnrichMessagesWithMostRecentDataItem.mockReturnValue(enrich(mockMessages))
172190
const {MAX_QUEUE_RUNTIME} = await import("../src/orchestration")
173191
mockDetermineAction.mockReturnValue(PostDatedProcessingResult.FORWARD_TO_NOTIFICATIONS)
192+
vi.spyOn(Date, "now")
193+
.mockReturnValueOnce(0)
194+
.mockReturnValueOnce(MAX_QUEUE_RUNTIME + 1000)
174195

175-
const promise = processPostDatedQueue(logger)
176-
// Overrun by a second
177-
jest.advanceTimersByTime(MAX_QUEUE_RUNTIME + 1000)
178-
await promise
196+
await processPostDatedQueue(logger)
179197

180198
expect(mockReportQueueStatus).toHaveBeenCalled()
181-
jest.useRealTimers()
182199
})
183200

184201
it("should continue processing batches until message count drops below threshold", async () => {

0 commit comments

Comments
 (0)