Skip to content

feat(auth): migrate metrics/logging/monitoring tests from Mocha to Jest#20243

Merged
vbudhram merged 1 commit intomainfrom
fxa-12562
Mar 24, 2026
Merged

feat(auth): migrate metrics/logging/monitoring tests from Mocha to Jest#20243
vbudhram merged 1 commit intomainfrom
fxa-12562

Conversation

@vbudhram
Copy link
Copy Markdown
Contributor

@vbudhram vbudhram commented Mar 23, 2026

Because

  • The FxA auth-server is migrating from Mocha to Jest as part of FXA-12536
  • Metrics, logging, and monitoring test files in test/local/ used sinon/proxyquire/chai patterns that are being replaced with Jest-native equivalents
  • lib/monitoring.js had zero test coverage

This pull request

  • Migrates 7 Mocha test files to co-located Jest .spec.ts files in lib/
  • Adds 1 new test file for lib/monitoring.js (previously untested)
  • Uses Jest-native mocking (jest.fn(), jest.spyOn(), jest.mock()) instead of sinon
  • Replaces proxyquire with jest.resetModules() + jest.doMock() for modules with load-time side effects
  • Does not delete old Mocha files — deletion is a separate follow-up

Test migration summary

# Old Test New Spec Tests Assertions
1 test/local/metricsCache.js lib/metricsCache.spec.ts 8 Identical
2 test/local/sentry.js lib/sentry.spec.ts 10 Identical
3 (none — new test) lib/monitoring.spec.ts 8 New coverage
4 test/local/log.js lib/log.spec.ts 28 Identical
5 test/local/metrics/context.js lib/metrics/context.spec.ts 39 Identical
6 test/local/metrics/amplitude.js lib/metrics/amplitude.spec.ts 475* Identical
7 test/local/metrics/events.js lib/metrics/events.spec.ts 33* Identical
8 test/local/metrics/glean.ts lib/metrics/glean/index.spec.ts 53 Identical
Total 654 No gaps

Issue

Closes: https://mozilla-hub.atlassian.net/browse/FXA-12562

Checklist

  • My commit is GPG signed
  • Tests pass locally (if applicable)
  • Documentation updated (if applicable)
  • RTL rendering verified (if UI changed)

Other Information

Verification:

cd packages/fxa-auth-server
npx jest --testPathPattern="(metricsCache|lib/sentry|monitoring|lib/log|metrics/context|metrics/amplitude|metrics/events|metrics/glean/index)\.spec" --no-coverage
# Result: 8 suites, 654 tests, 0 failures

Key decisions:

  • jest.mock('@sentry/node') used for sentry.spec.ts because Sentry exports are frozen/non-configurable — jest.spyOn cannot redefine them
  • monitoring.spec.ts extracts filterSentryEvent from initMonitoring call args since it's not exported
  • events.spec.ts uses sinon .callCount/.args for metricsContext methods since mocks.mockMetricsContext() returns sinon spies
  • M6 adds .catch(() => {}) on an intentionally-rejected promise to avoid Node 22 unhandled rejection crashes

@vbudhram vbudhram self-assigned this Mar 24, 2026
@vbudhram vbudhram marked this pull request as ready for review March 24, 2026 13:31
@vbudhram vbudhram requested a review from a team as a code owner March 24, 2026 13:31
Copilot AI review requested due to automatic review settings March 24, 2026 13:31
Copy link
Copy Markdown

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 advances the FxA auth-server’s migration from Mocha to Jest by moving metrics/logging/monitoring tests into co-located lib/**/*.spec.ts suites (and adding new coverage for previously-untested monitoring initialization behavior).

Changes:

  • Migrates Mocha test/local/** suites to Jest lib/**/*.spec.ts, replacing sinon/proxyquire/chai patterns with Jest-native mocking.
  • Adds a new Jest suite for lib/monitoring.js to cover its module-load initMonitoring() behavior and Sentry event filtering.
  • Introduces Jest coverage for supporting modules like Sentry config, metrics cache, and metrics event emitters.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/fxa-auth-server/lib/sentry.spec.ts Jest migration of Sentry-related tests and helper coverage.
packages/fxa-auth-server/lib/monitoring.spec.ts New Jest coverage for lib/monitoring.js module-load monitoring init and filter behavior.
packages/fxa-auth-server/lib/metricsCache.spec.ts Jest migration for MetricsRedis cache behavior with Redis/TypeDI isolated via mocks.
packages/fxa-auth-server/lib/metrics/glean/index.spec.ts Jest migration for Glean server-side event metrics and error logging behavior.
packages/fxa-auth-server/lib/metrics/events.spec.ts Jest migration for metrics event emission behavior (activity/flow/route flow events).
packages/fxa-auth-server/lib/metrics/context.spec.ts Jest migration for metrics context stash/get/gather/propagate/validate behaviors.
packages/fxa-auth-server/lib/metrics/amplitude.spec.ts Jest migration for Amplitude event mapping/validation tests (with TypeDI involvement).
packages/fxa-auth-server/lib/log.spec.ts Jest migration for logger initialization, amplitude validation handling, and notifications.

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

Comment on lines +124 to +132
it('can be set up when sentry is not enabled', async () => {
let throws = false;
try {
await configureSentry(server, config);
} catch (err) {
throws = true;
}
expect(throws).toBe(false);
});
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

The test "can be set up when sentry is not enabled" doesn’t actually disable Sentry: beforeEach always sets config.sentry.dsn to a non-empty value, so this test exercises the same enabled branch as the prior test. Set config.sentry.dsn to a falsy value for this test (or avoid setting it unconditionally in beforeEach) so the no-DSN path is covered.

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +49
Container.set(StatsD, { increment: jest.fn() });

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

const mocks = require('../../test/mocks');
const { version } = require('../../package.json');

const amplitudeModule = require('./amplitude');

const mockAmplitudeConfig = {
schemaValidation: true,
rawEvents: false,
};

const DAY = 1000 * 60 * 60 * 24;
const WEEK = DAY * 7;
const MONTH = DAY * 28;

// ---------------------------------------------------------------------------
// Tests
//
// NOTE: mocks.mockLog() returns sinon spies, so we access call data via the
// sinon API (.callCount, .args) rather than the jest mock API (.mock.calls).
// StatsD instances created with jest.fn() use the jest API.
// ---------------------------------------------------------------------------

describe('metrics/amplitude', () => {
afterEach(() => {
jest.restoreAllMocks();
});

afterAll(() => {
Container.reset();
});
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

This spec mutates the global TypeDI Container at module load (Container.set(StatsD, ...)) and only calls Container.reset() in afterAll. That can leak DI state into other Jest suites running in the same worker and make results order-dependent. Please move the Container.set into a beforeEach and call Container.reset() in afterEach (this is the pattern used in other specs, e.g. lib/account-events.spec.ts:56-58, lib/account-delete.spec.ts:156-159).

Copilot uses AI. Check for mistakes.
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 added this

@vbudhram
Copy link
Copy Markdown
Contributor Author

@nshirley I've also addressed feedback from copilot and reviewed this code (verified parity on assertions, tests and coverage). The biggest change was the added coverage to lib/monitoring.js, not sure but original ticket called out adding it but there was no tests for it. It was straightforward to add so I did.

Welcome to give this a pass over as well.

Copy link
Copy Markdown
Contributor

@nshirley nshirley left a comment

Choose a reason for hiding this comment

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

Thanks for adding the new tests for monitoring!

@vbudhram vbudhram merged commit cf94c97 into main Mar 24, 2026
21 checks passed
@vbudhram vbudhram deleted the fxa-12562 branch March 24, 2026 18:12
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