Skip to content

Chore: [AEA-0000] - migrate jest to vitest#2950

Merged
wildjames merged 23 commits intomainfrom
aea-0000-jest-to-vitest
Apr 10, 2026
Merged

Chore: [AEA-0000] - migrate jest to vitest#2950
wildjames merged 23 commits intomainfrom
aea-0000-jest-to-vitest

Conversation

@tstephen-nhs
Copy link
Copy Markdown
Contributor

Summary

  • 🤖 Operational or Infrastructure Change

Details

Migrate testing framework from jest to vitest

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 7, 2026

This PR is linked to a ticket in an NHS Digital JIRA Project. Here's a handy link to the ticket:

AEA-0000

@tstephen-nhs tstephen-nhs marked this pull request as ready for review April 9, 2026 14:20
Copilot AI review requested due to automatic review settings April 9, 2026 14:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates the monorepo’s TypeScript test runner from Jest to Vitest, introducing a shared Vitest base config and updating package-level configs, scripts, and tests accordingly.

Changes:

  • Added a shared vitest.default.config.ts and per-package vitest.config.ts files.
  • Updated package unit scripts and VS Code test/debug configurations to use Vitest.
  • Migrated existing Jest tests/mocking patterns (jest.*, unstable_mockModule) to Vitest equivalents (vi.*, vi.mock, vi.hoisted), and removed Jest config files.

Reviewed changes

Copilot reviewed 110 out of 111 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
vitest.default.config.ts New shared Vitest defaults (test include/coverage config).
sonar-project.properties Updates exclusions to match Vitest-era helpers/config naming.
packages/updatePrescriptionStatus/vitest.config.ts Package Vitest config merged with shared defaults (+ setupFiles).
packages/updatePrescriptionStatus/tests/validation/testRequestValidationViaHandler.test.ts Migrates test from Jest globals/mocking to Vitest.
packages/updatePrescriptionStatus/tests/validation/testRequestFieldValidation.test.ts Switches test imports from Jest to Vitest.
packages/updatePrescriptionStatus/tests/validation/testRequestContentValidation.test.ts Switches fake timers from Jest to Vitest.
packages/updatePrescriptionStatus/tests/utils/testValidateNhsNumber.test.ts Adds Vitest imports for test.
packages/updatePrescriptionStatus/tests/utils/testUtils.ts Reworks test utilities for Vitest; removes Jest-only helpers.
packages/updatePrescriptionStatus/tests/testUpdatePrescriptionStatus.test.ts Migrates module mocking/timers to Vitest.
packages/updatePrescriptionStatus/tests/testSqsClient.test.ts Migrates mocks/spies to Vitest (AWS + secrets + utilities).
packages/updatePrescriptionStatus/tests/testPrescriptionIntercept.test.ts Migrates DynamoDB/utilities mocking + spies to Vitest.
packages/updatePrescriptionStatus/tests/testHandler.test.ts Migrates handler integration tests/mocks to Vitest.
packages/updatePrescriptionStatus/tests/testDatabaseClient.test.ts Migrates DynamoDB client mocking/spies to Vitest.
packages/updatePrescriptionStatus/package.json Updates unit script to run Vitest with coverage.
packages/updatePrescriptionStatus/jest.config.ts Removes Jest config (package-level).
packages/updatePrescriptionStatus/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/updatePrescriptionStatus/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/statusLambda/vitest.config.ts Adds package Vitest config merged with defaults.
packages/statusLambda/tests/test-handler.test.ts Migrates spies/imports from Jest to Vitest.
packages/statusLambda/package.json Updates unit script to Vitest.
packages/statusLambda/jest.config.ts Removes Jest config (package-level).
packages/statusLambda/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/statusLambda/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/specification/package.json Adjusts dependencies/devDependencies during migration.
packages/specification/jest.config.ts Removes Jest config (package-level).
packages/psuRestoreValidationLambda/vitest.config.ts Adds package Vitest config merged with defaults.
packages/psuRestoreValidationLambda/tests/handler.test.ts Migrates module mocks/spies to Vitest.
packages/psuRestoreValidationLambda/tests/compareTable.test.ts Migrates mocks/types/timers reset to Vitest.
packages/psuRestoreValidationLambda/package.json Updates unit script to Vitest.
packages/psuRestoreValidationLambda/jest.config.ts Removes Jest config (package-level).
packages/psuRestoreValidationLambda/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/psuRestoreValidationLambda/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/postDatedLambda/vitest.config.ts Adds package Vitest config merged with defaults (+ worker limits/setupFiles).
packages/postDatedLambda/tests/testSqs.test.ts Migrates SQS mocking/spies to Vitest.
packages/postDatedLambda/tests/testOrchestration.test.ts Migrates orchestration module mocks/timers to Vitest.
packages/postDatedLambda/tests/testMain.test.ts Migrates module mocks/cleanup to Vitest.
packages/postDatedLambda/tests/testDatabaseClient.test.ts Migrates DynamoDB client mocking to Vitest.
packages/postDatedLambda/tests/testBusinessLogic.test.ts Migrates timers/module reset usage to Vitest.
packages/postDatedLambda/package.json Updates unit script to Vitest (keeps memory option).
packages/postDatedLambda/jest.config.ts Removes Jest config (package-level).
packages/postDatedLambda/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/postDatedLambda/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/nhsNotifyUpdateCallback/vitest.config.ts Adds package Vitest config merged with defaults (+ setupFiles).
packages/nhsNotifyUpdateCallback/tests/testNhsNotifyCallbackLambda.test.ts Migrates helpers mocking/reset patterns to Vitest.
packages/nhsNotifyUpdateCallback/tests/testHelpers.test.ts Migrates spies/mocks/types to Vitest.
packages/nhsNotifyUpdateCallback/package.json Updates unit script to Vitest.
packages/nhsNotifyUpdateCallback/jest.config.ts Removes Jest config (package-level).
packages/nhsNotifyUpdateCallback/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/nhsNotifyUpdateCallback/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/nhsNotifyLambda/vitest.config.ts Adds package Vitest config merged with defaults (+ globals/setupFiles).
packages/nhsNotifyLambda/tests/testUtils.test.ts Migrates extensive test suite to Vitest mocks/spies/timers.
packages/nhsNotifyLambda/tests/testSecrets.test.ts Migrates secrets mocking to Vitest.
packages/nhsNotifyLambda/tests/testNhsNotifyLambda.test.ts Migrates module mocks + logger mocks + Date spies to Vitest.
packages/nhsNotifyLambda/tests/testHelpers.ts Migrates SQS client mocking/types to Vitest.
packages/nhsNotifyLambda/tests/testAuth.test.ts Migrates jose mocking + mocks to Vitest.
packages/nhsNotifyLambda/tests/notify.test.ts Migrates UUID mocking + mocks to Vitest.
packages/nhsNotifyLambda/package.json Updates unit script to Vitest.
packages/nhsNotifyLambda/jest.config.ts Removes Jest config (package-level).
packages/nhsNotifyLambda/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/nhsNotifyLambda/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/nhsd-psu-sandbox/vitest.config.ts Adds package Vitest config merged with defaults.
packages/nhsd-psu-sandbox/tests/test-handler.test.ts Switches Jest test imports to Vitest.
packages/nhsd-psu-sandbox/package.json Updates unit script to Vitest.
packages/nhsd-psu-sandbox/jest.config.ts Removes Jest config (package-level).
packages/nhsd-psu-sandbox/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/nhsd-psu-sandbox/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/gsul/vitest.config.ts Adds package Vitest config merged with defaults.
packages/gsul/tests/testRunDynamoDBQueries.test.ts Migrates module reset/spies to Vitest.
packages/gsul/tests/testHander.test.ts Migrates module reset/spies to Vitest.
packages/gsul/tests/testFilterOutFutureReduceToLatestUpdates.test.ts Adds Vitest imports for test.
packages/gsul/package.json Updates unit script to Vitest.
packages/gsul/jest.config.ts Removes Jest config (package-level).
packages/gsul/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/gsul/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/cpsuLambda/vitest.config.ts Adds package Vitest config merged with defaults.
packages/cpsuLambda/tests/testHandler.test.ts Migrates spies/timers/imports to Vitest.
packages/cpsuLambda/tests/format_1/business_states.test.ts Adds Vitest imports for test suite.
packages/cpsuLambda/package.json Updates unit script to Vitest.
packages/cpsuLambda/jest.config.ts Removes Jest config (package-level).
packages/cpsuLambda/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/cpsuLambda/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/common/utilities/vitest.config.ts Adds package Vitest config merged with defaults.
packages/common/utilities/tests/testTestConfig.test.ts Migrates module mocking/reset patterns to Vitest.
packages/common/utilities/package.json Updates unit script to Vitest.
packages/common/utilities/jest.config.ts Removes Jest config (package-level).
packages/common/testing/vitest.config.ts Adds package Vitest config merged with defaults.
packages/common/testing/package.json Updates unit script to Vitest (+ coverage / no-tests handling).
packages/common/middyErrorHandler/vitest.config.ts Adds package Vitest config merged with defaults.
packages/common/middyErrorHandler/tests/testErrorHandler.test.ts Migrates Jest mocks to Vitest mocks.
packages/common/middyErrorHandler/package.json Updates unit script to Vitest.
packages/common/middyErrorHandler/jest.config.ts Removes Jest config (package-level).
packages/common/middyErrorHandler/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/common/middyErrorHandler/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/common/commonTypes/vitest.config.ts Adds package Vitest config merged with defaults.
packages/common/commonTypes/package.json Updates unit script to Vitest (+ coverage / no-tests handling).
packages/checkPrescriptionStatusUpdates/vitest.config.ts Adds package Vitest config merged with defaults (+ setupFiles).
packages/checkPrescriptionStatusUpdates/tests/testHander.test.ts Migrates module reset/spies/types to Vitest.
packages/checkPrescriptionStatusUpdates/tests/testDynamoDBclient.test.ts Migrates imports from Jest to Vitest.
packages/checkPrescriptionStatusUpdates/package.json Updates unit script to Vitest.
packages/checkPrescriptionStatusUpdates/jest.config.ts Removes Jest config (package-level).
packages/checkPrescriptionStatusUpdates/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/checkPrescriptionStatusUpdates/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
packages/capabilityStatement/vitest.config.ts Adds package Vitest config merged with defaults.
packages/capabilityStatement/tests/test-handler.test.ts Migrates spies/imports to Vitest.
packages/capabilityStatement/package.json Updates unit script to Vitest.
packages/capabilityStatement/jest.config.ts Removes Jest config (package-level).
packages/capabilityStatement/.vscode/settings.json Removes Jest extension settings; adjusts testing UX.
packages/capabilityStatement/.vscode/launch.json Replaces Jest debug launch configs with Vitest equivalents.
package.json Adds Vitest + coverage + tsconfig-paths dependencies.
jest.default.config.ts Removes shared Jest default config.

Comment thread packages/specification/package.json
Comment on lines 75 to +88
@@ -68,12 +80,27 @@ const ORIGINAL_ENV = {...process.env}

describe("Integration tests for updatePrescriptionStatus handler", () => {
beforeEach(() => {
jest.resetModules()
vi.resetModules()
process.env = {...ORIGINAL_ENV}
jest.clearAllMocks()
jest.resetAllMocks()
jest.clearAllTimers()
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
vi.clearAllMocks()
vi.clearAllTimers()
vi.useFakeTimers().setSystemTime(DEFAULT_DATE)

Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vi.resetModules() is called in beforeEach, but handler/logger are imported once at module scope (const {handler, logger} = await import(...)). This means the reset does not actually reload updatePrescriptionStatus between tests, so module-level state (e.g. INT_ENVIRONMENT, cached SSM provider, testPrescriptionsLoaded) can leak across tests and environment changes won’t be reflected. To make the reset effective, import handler/logger inside beforeEach (after vi.resetModules()/process.env reset) and store them in variables used by the tests, or remove vi.resetModules() if reloading isn’t required.

Copilot uses AI. Check for mistakes.
Comment thread packages/nhsNotifyLambda/tests/testUtils.test.ts
Comment on lines 472 to 488
describe("checkCooldownForUpdate", () => {
let logger: Logger
let infoSpy: SpiedFunction<(msg: string, ...meta: Array<unknown>) => void>
let errorSpy: SpiedFunction<(msg: string, ...meta: Array<unknown>) => void>
let sendSpy: ReturnType<typeof jest.spyOn>
let infoSpy: Spy
let errorSpy: Spy
let sendSpy = vi.spyOn(DynamoDBDocumentClient.prototype, "send")

beforeEach(async () => {
jest.resetModules()
jest.clearAllMocks()
vi.resetModules()
vi.clearAllMocks()

process.env = {...ORIGINAL_ENV}

logger = new Logger({serviceName: "test-service"})
infoSpy = jest.spyOn(logger, "info")
errorSpy = jest.spyOn(logger, "error")
sendSpy = jest.spyOn(DynamoDBDocumentClient.prototype, "send")
infoSpy = vi.spyOn(logger, "info")
errorSpy = vi.spyOn(logger, "error")
sendSpy = vi.spyOn(DynamoDBDocumentClient.prototype, "send")
})
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here: sendSpy is created at declaration time (let sendSpy = vi.spyOn(...)) and re-created in beforeEach. This can cause Vitest to throw on the second spyOn or leave the spy active between tests. Make sendSpy a let without initialising it, create it in beforeEach, and restore it in afterEach (or call vi.restoreAllMocks()).

Copilot uses AI. Check for mistakes.
Comment thread packages/specification/package.json
Comment thread packages/updatePrescriptionStatus/tests/utils/testUtils.ts Outdated
Copy link
Copy Markdown
Contributor

@wildjames wildjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few sonar issues that I don't care about. We're happy to resolve promises the way we are, since it works, and most of the copilot comments are just repeats of the same issue, which hasn't appeared to stop the suite from working... so, sod it.

I do have two real comments though. Possibly no code change needed but questions I'd like answered

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 9, 2026

@wildjames wildjames merged commit 2e89d05 into main Apr 10, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants