Skip to content

test(auth): migrate log/statsd/fxaMailer mocks to typed Jest fixtures#20705

Open
nshirley wants to merge 1 commit into
mainfrom
FXA-13708
Open

test(auth): migrate log/statsd/fxaMailer mocks to typed Jest fixtures#20705
nshirley wants to merge 1 commit into
mainfrom
FXA-13708

Conversation

@nshirley

@nshirley nshirley commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Because:

  • test/mocks.js is an untyped ~1.2k-line monolith whose shared factories obscure test intent and cannot track production-type drift (FXA-13708).

This commit:

  • Replaces mocks.mockLog()/mockStatsd() with inline createMock() and createMock() (@golevelup/ts-jest) across ~60 specs, plus any hand-rolled statsd stubs.
  • Adds test/fixtures/ (README + fxa-mailer.ts with installMockFxaMailer/ uninstallMockFxaMailer) and migrates the 16 route specs off mocks.mockFxaMailer(), with Container teardown.
  • Completes the AuthLogger interface (flowEvent, activityEvent, amplitudeEvent, summary) so the typed log mock matches the real logger.
  • Removes the now-dead mockStatsd and mockFxaMailer slices from test/mocks.js (mockLog stays; mockRequest still consumes it internally).
  • Adds @golevelup/ts-jest as a devDependency. Legacy mocks.mockMailer() and its negative "not called" guards are left intact; no file tests the legacy mailer sending path.

Issue that this pull request solves

Closes: (issue number)

Checklist

Put an x in the boxes that apply

  • My commit is GPG signed.
  • If applicable, I have modified or added tests which pass locally.
  • I have added necessary documentation (if appropriate).
  • I have verified that my changes render correctly in RTL (if appropriate).
  • I have manually reviewed all AI generated code.

How to review (Optional)

  • Key files/areas to focus on:
  • Suggested review order:
  • Risky or complex parts:

Screenshots (Optional)

Please attach the screenshots of the changes made in case of change in user interface.

Other information (Optional)

Any other information that is important to this pull request.

@nshirley nshirley force-pushed the FXA-13708 branch 3 times, most recently from 84ce3c0 to 72f8373 Compare June 8, 2026 16:08
// ./cloud-tasks route module, which re-exports it: ./cloud-tasks imports this
// validators module, so requiring it here created a circular dependency that
// left ReasonForDeletion undefined depending on module load order.
const { ReasonForDeletion } = require('@fxa/shared/cloud-tasks');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the comment here, but left it for the time being to remind myself.

I suspect, as more things are ripped out of the shared mocks.ts, we'll see this more. There was a hidden circular dependency. The import here was a re-export from the source, but the mocks.ts also imported all of the relevant modules in order, thus loading them into the dependency cache and hiding this problem.

Because:
- test/mocks.js's untyped shared factories obscure intent and miss type
  drift (FXA-13708).

This commit:
- Swaps mocks.mockLog()/mockStatsd() for inline createMock<T>() and adds
  test/fixtures/fxa-mailer.ts (installMockFxaMailer) across ~60 specs.
- Completes the AuthLogger interface and removes the now-dead mockStatsd and
  mockFxaMailer slices from test/mocks.js.
- Breaks a validators <-> cloud-tasks circular dependency the migration
  exposed (validators now imports ReasonForDeletion from @fxa/shared/cloud-tasks).
@nshirley nshirley marked this pull request as ready for review June 10, 2026 22:46
@nshirley nshirley requested a review from a team as a code owner June 10, 2026 22:46
Copilot AI review requested due to automatic review settings June 10, 2026 22:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors fxa-auth-server tests away from the legacy test/mocks.js factories toward typed Jest mocks (@golevelup/ts-jest), adds a small fixture for DI-installed FxaMailer, and fixes a circular dependency in route validators.

Changes:

  • Added @golevelup/ts-jest and migrated many specs to createMock<T>() for AuthLogger/StatsD and related collaborators.
  • Introduced test/fixtures/ (README + fxa-mailer.ts) to install/uninstall a typed FxaMailer mock in the TypeDI Container, and removed the old mockStatsd/mockFxaMailer slices from test/mocks.js.
  • Updated AuthLogger’s TypeScript interface and adjusted validators.js to import ReasonForDeletion from @fxa/shared/cloud-tasks to avoid a circular dependency.

Reviewed changes

Copilot reviewed 72 out of 73 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
yarn.lock Adds lock entry for @golevelup/ts-jest.
packages/fxa-auth-server/package.json Adds @golevelup/ts-jest as a devDependency.
packages/fxa-auth-server/test/mocks.js Removes mockStatsd + mockFxaMailer exports/implementation.
packages/fxa-auth-server/test/fixtures/README.md Documents new typed test-fixture conventions and when to add fixtures.
packages/fxa-auth-server/test/fixtures/fxa-mailer.ts Adds install/uninstall helpers for a Container-registered FxaMailer mock.
packages/fxa-auth-server/lib/types.ts Expands AuthLogger interface to include missing methods used in production.
packages/fxa-auth-server/lib/routes/validators.js Avoids circular dependency by importing ReasonForDeletion from the shared package.
packages/fxa-auth-server/lib/tokens/token.spec.ts Migrates mockLog() usage to createMock<AuthLogger>().
packages/fxa-auth-server/lib/subscription-account-reminders.in.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/server.in.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/senders/index.spec.ts Migrates nullLog to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/utils/signup.spec.ts Uses typed AuthLogger mock and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/utils/signin.spec.ts Uses typed AuthLogger mock and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/utils/oauth.spec.ts Installs typed FxaMailer fixture (Container) for tests.
packages/fxa-auth-server/lib/routes/utils/clients.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/unblock-codes.spec.ts Installs typed FxaMailer fixture and migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/totp.spec.ts Migrates logger/statsd mocks to typed createMock<AuthLogger/StatsD>() and installs typed FxaMailer.
packages/fxa-auth-server/lib/routes/subscriptions/support.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/stripe.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/stripe-webhooks.spec.ts Migrates logger mock to typed createMock<AuthLogger>() and tidies some spy formatting.
packages/fxa-auth-server/lib/routes/subscriptions/play-pubsub.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/paypal-notifications.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/mozilla.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/google.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/subscriptions/apple.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/session.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/security-events.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/recovery-phone.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/recovery-key.spec.ts Migrates logger mock to typed createMock<AuthLogger>() and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/recovery-codes.spec.ts Migrates logger mock to typed createMock<AuthLogger>() and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/passwordless.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/password.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/oauth/index.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/newsletters.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/mfa.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/linked-accounts.spec.ts Installs typed FxaMailer fixture and migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/routes/ip-profiling.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/geo-location.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/emails.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/routes/devices-and-sessions.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/cms.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/routes/cloud-tasks.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/attached-clients.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/routes/account.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/redis.in.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/pushbox/index.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/push.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/profile/updates.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/subscription-reminders.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/stripe.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/payments/paypal/processor.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/paypal/helper.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/iap-config.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/google-play/user-manager.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/google-play/purchase-manager.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/google-play/play-billing.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/apple-app-store/purchase-manager.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/apple-app-store/apple-iap.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/iap/apple-app-store/app-store-helper.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/payments/capability.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/metrics/context.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/metrics/amplitude.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/inactive-accounts/index.spec.ts Migrates logger/statsd mocks to typed mocks and installs typed FxaMailer fixture.
packages/fxa-auth-server/lib/google-maps-services.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/email/utils/helpers.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/email/notifications.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/email/delivery.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/email/delivery-delay.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/email/bounces.spec.ts Migrates logger/statsd mocks to typed mocks.
packages/fxa-auth-server/lib/devices.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/db.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/cad-reminders.in.spec.ts Migrates logger mock to typed createMock<AuthLogger>().
packages/fxa-auth-server/lib/account-delete.spec.ts Migrates logger/statsd mocks to typed mocks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +41 to +45
const mailer = createMock<FxaMailer>({
canSend: () => true,
...overrides,
});
Container.set(FxaMailer, mailer);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

canSend will hopefully go away soon, and we should always be testing the fxaMailer route over falling back to the 'old' mailer style, so there's really no need to override this on the mock

@nshirley nshirley changed the title [wip] test(auth): migrate log/statsd/fxaMailer mocks to typed Jest fixtures test(auth): migrate log/statsd/fxaMailer mocks to typed Jest fixtures Jun 11, 2026
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.

2 participants