Skip to content

Commit 60e3606

Browse files
committed
chore: migrated nhsNotifyLambda tests passing
1 parent ecc5541 commit 60e3606

10 files changed

Lines changed: 195 additions & 156 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/nhsNotifyLambda/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 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/nhsNotifyLambda/tests/notify.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {jest, describe, it} from "@jest/globals"
1+
import {vi, describe, it} from "vitest"
22

33
import {Logger} from "@aws-lambda-powertools/logger"
44
import {logNotificationRequest} from "../src/utils/notify"
@@ -7,13 +7,11 @@ import {LOG_MESSAGES} from "@psu-common/utilities"
77

88
// Mock crypto.randomUUID
99
const mockUUID = "550e8400-e29b-41d4-a716-446655440000"
10-
globalThis.crypto = {
11-
randomUUID: () => mockUUID
12-
} as unknown as Crypto
10+
vi.spyOn(globalThis.crypto, "randomUUID").mockReturnValue(mockUUID)
1311

14-
const mockInfo = jest.fn()
15-
const mockError = jest.fn()
16-
const mockWarn = jest.fn()
12+
const mockInfo = vi.fn()
13+
const mockError = vi.fn()
14+
const mockWarn = vi.fn()
1715

1816
describe("logNotificationRequest", () => {
1917
let mockLogger: Logger
@@ -70,7 +68,7 @@ describe("logNotificationRequest", () => {
7068

7169
data = [dataItem1, dataItem2]
7270

73-
jest.clearAllMocks()
71+
vi.clearAllMocks()
7472
})
7573

7674
it("should log legacy notification request summary", () => {

packages/nhsNotifyLambda/tests/testAuth.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import {jest} from "@jest/globals"
1+
import {vi} from "vitest"
22
import nock from "nock"
33

44
import {Logger} from "@aws-lambda-powertools/logger"
55
import axios, {AxiosInstance} from "axios"
66

7-
const mockImportPKCS8 = jest.fn()
8-
const mockSignJWTConstructor = jest.fn()
9-
jest.unstable_mockModule("jose", async () => ({
7+
const {mockImportPKCS8, mockSignJWTConstructor} = vi.hoisted(() => ({
8+
mockImportPKCS8: vi.fn(),
9+
mockSignJWTConstructor: vi.fn()
10+
}))
11+
12+
vi.mock("jose", async () => ({
1013
__esModule: true,
1114
importPKCS8: mockImportPKCS8,
1215
SignJWT: mockSignJWTConstructor
@@ -31,7 +34,7 @@ describe("tokenExchange", () => {
3134
let axiosInstance: AxiosInstance
3235

3336
beforeEach(() => {
34-
jest.clearAllMocks()
37+
vi.clearAllMocks()
3538
logger = new Logger({serviceName: "test-service"})
3639

3740
axiosInstance = axios.create({baseURL: host})
@@ -56,7 +59,7 @@ describe("tokenExchange", () => {
5659
setExpirationTime: function () {
5760
return this
5861
},
59-
sign: jest.fn().mockImplementation(() => Promise.resolve("signed.jwt.token"))
62+
sign: vi.fn().mockImplementation(() => Promise.resolve("signed.jwt.token"))
6063
}
6164
mockSignJWTConstructor.mockImplementation(() => fakeJwtInstance)
6265

@@ -101,7 +104,7 @@ describe("tokenExchange", () => {
101104
setExpirationTime() {
102105
return this
103106
},
104-
sign: jest.fn().mockImplementation(() => Promise.resolve("jwt-tkn"))
107+
sign: vi.fn().mockImplementation(() => Promise.resolve("jwt-tkn"))
105108
}))
106109

107110
nock(`${host}`)
@@ -131,7 +134,7 @@ describe("tokenExchange", () => {
131134
setExpirationTime() {
132135
return this
133136
},
134-
sign: jest.fn().mockImplementation(() => Promise.resolve("jwt-tkn"))
137+
sign: vi.fn().mockImplementation(() => Promise.resolve("jwt-tkn"))
135138
}))
136139

137140
nock(`${host}`)

packages/nhsNotifyLambda/tests/testHelpers.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
import {jest} from "@jest/globals"
2-
3-
import * as sqs from "@aws-sdk/client-sqs"
1+
import {vi} from "vitest"
2+
import type {Message} from "@aws-sdk/client-sqs"
43

54
import {PSUDataItem} from "@psu-common/commonTypes"
65
import {NotifyDataItemMessage} from "../src/utils"
76

7+
const {mockSend} = vi.hoisted(() => ({mockSend: vi.fn()}))
8+
9+
vi.mock("@aws-sdk/client-sqs", async (importOriginal) => {
10+
const sqs = await importOriginal<typeof import("@aws-sdk/client-sqs")>()
11+
return {
12+
...sqs,
13+
SQSClient: vi.fn().mockImplementation(() => ({
14+
send: mockSend
15+
}))
16+
}
17+
})
18+
819
// Similarly mock the SQS client
920
export function mockSQSClient() {
10-
const mockSend = jest.fn()
11-
jest.unstable_mockModule("@aws-sdk/client-sqs", () => {
12-
return {
13-
...sqs,
14-
SQSClient: jest.fn().mockImplementation(() => ({
15-
send: mockSend
16-
}))
17-
}
18-
})
1921
return {mockSend}
2022
}
2123

22-
export function constructMessage(overrides: Partial<sqs.Message> = {}): sqs.Message {
24+
export function constructMessage(overrides: Partial<Message> = {}): Message {
2325
return {
2426
MessageId: "messageId",
2527
Attributes: {

packages/nhsNotifyLambda/tests/testNhsNotifyLambda.test.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
import {
2-
jest,
2+
vi,
33
describe,
44
it,
55
beforeAll,
6+
beforeEach,
67
afterEach
7-
} from "@jest/globals"
8+
} from "vitest"
89

910
import {constructPSUDataItem, constructPSUDataItemMessage} from "./testHelpers"
1011

11-
const mockGetParameter = jest.fn().mockImplementation(() => "parameter_value")
12-
jest.unstable_mockModule(
12+
const {
13+
mockGetParameter,
14+
mockAddPrescriptionMessagesToNotificationStateStore,
15+
mockRemoveSQSMessages,
16+
mockReportQueueStatus,
17+
mockDrainQueue,
18+
mockCheckCooldownForUpdate,
19+
mockMakeBatchNotifyRequest,
20+
mockInfo,
21+
mockError,
22+
mockWarn
23+
} = vi.hoisted(() => ({
24+
mockGetParameter: vi.fn().mockImplementation(() => "parameter_value"),
25+
mockAddPrescriptionMessagesToNotificationStateStore: vi.fn(),
26+
mockRemoveSQSMessages: vi.fn(),
27+
mockReportQueueStatus: vi.fn(),
28+
mockDrainQueue: vi.fn(),
29+
mockCheckCooldownForUpdate: vi.fn(),
30+
mockMakeBatchNotifyRequest: vi.fn(),
31+
mockInfo: vi.fn(),
32+
mockError: vi.fn(),
33+
mockWarn: vi.fn()
34+
}))
35+
36+
vi.mock(
1337
"@aws-lambda-powertools/parameters/ssm",
1438
async () => ({
1539
__esModule: true,
1640
getParameter: mockGetParameter
1741
})
1842
)
1943

20-
const mockAddPrescriptionMessagesToNotificationStateStore = jest.fn()
21-
const mockRemoveSQSMessages = jest.fn()
22-
const mockReportQueueStatus = jest.fn()
23-
const mockDrainQueue = jest.fn()
24-
const mockCheckCooldownForUpdate = jest.fn()
25-
const mockMakeBatchNotifyRequest = jest.fn()
26-
27-
jest.unstable_mockModule(
44+
vi.mock(
2845
"../src/utils",
2946
async () => ({
3047
__esModule: true,
@@ -37,14 +54,11 @@ jest.unstable_mockModule(
3754
})
3855
)
3956

40-
const mockInfo = jest.fn()
41-
const mockError = jest.fn()
42-
const mockWarn = jest.fn()
43-
jest.unstable_mockModule(
57+
vi.mock(
4458
"@aws-lambda-powertools/logger",
4559
async () => ({
4660
__esModule: true,
47-
Logger: jest.fn().mockImplementation(() => ({
61+
Logger: vi.fn().mockImplementation(() => ({
4862
info: mockInfo,
4963
error: mockError,
5064
warn: mockWarn
@@ -62,11 +76,15 @@ import {mockEventBridgeEvent} from "@psu-common/testing"
6276
const ORIGINAL_ENV = {...process.env}
6377

6478
describe("Unit test for NHS Notify lambda handler", () => {
79+
beforeEach(() => {
80+
mockGetParameter.mockReset()
81+
mockGetParameter.mockImplementation(() => "parameter_value")
82+
})
83+
6584
afterEach(() => {
6685
process.env = {...ORIGINAL_ENV}
6786

68-
jest.clearAllMocks()
69-
jest.restoreAllMocks()
87+
vi.clearAllMocks()
7088
})
7189

7290
it("When the getParameter call fails, the handler throws an error", async () => {
@@ -293,7 +311,7 @@ describe("Unit test for NHS Notify lambda handler", () => {
293311
Promise.resolve({messages: [msg], isEmpty: false})
294312
)
295313

296-
const nowSpy = jest.spyOn(Date, "now")
314+
const nowSpy = vi.spyOn(Date, "now")
297315
.mockImplementationOnce(() => 0) // start time
298316
.mockImplementationOnce(() => (14 * 60 * 1000) + 1)
299317

packages/nhsNotifyLambda/tests/testSecrets.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import {jest} from "@jest/globals"
1+
import {vi} from "vitest"
22

3-
const mockGetSecret = jest.fn<() => Promise<string | null>>()
4-
jest.unstable_mockModule(
3+
const {mockGetSecret} = vi.hoisted(() => ({
4+
mockGetSecret: vi.fn<() => Promise<string | null>>()
5+
}))
6+
7+
vi.mock(
58
"@aws-lambda-powertools/parameters/secrets",
69
async () => ({
710
__esModule: true,
@@ -16,7 +19,7 @@ describe("loadSecrets", () => {
1619
process.env.API_KEY_SECRET = "api-key-secret-name"
1720
process.env.PRIVATE_KEY_SECRET = "private-key-secret-name"
1821
process.env.KID_SECRET = "kid-secret-name"
19-
jest.clearAllMocks()
22+
vi.clearAllMocks()
2023
})
2124

2225
it("should load and trim secrets successfully", async () => {

0 commit comments

Comments
 (0)