Skip to content

test(auth): migrate tests to Vitest - #10393

Open
Manvi1203 wants to merge 1 commit into
mainfrom
feature/vitest-auth
Open

Manvi1203 wants to merge 1 commit into
mainfrom
feature/vitest-auth

Conversation

@Manvi1203

Copy link
Copy Markdown
Contributor

Description

Migrates @firebase/auth unit, browser, and integration tests from legacy Karma & Mocha/Chai/Sinon to Vitest (Node + Browser Chromium).

Key Changes

  • Vitest Runner:
    • Added vitest.config.mjs extending config/vitest.base.mjs with dual workspace projects (node and browser with Chromium) and removed deprecated karma.conf.js.
    • Configured teardownTimeout: 1000 and project-specific excludes for platform directories (platform_browser, platform_cordova, platform_react_native, test/integration/**).
  • Test Scripts:
    • Standardized package.json test scripts to Vitest commands (test:all, test:node, test:browser, test:browser:debug).
    • Preserved multi-target flags (--project=browser and --project=node) to support Auth's dual environments.
  • Test Modernization & Assertions:
    • Converted legacy Chai assertions (expect().to.equal, to.be.null, to.be.rejectedWith, etc.) to native Vitest matchers (toBe, toBeNull, rejects.toThrow, etc.) across all 130+ test suites.
    • Converted Sinon stubs, spies, and fake timers to Vitest equivalents (vi.fn, vi.spyOn, vi.useFakeTimers, vi.advanceTimersByTimeAsync).
  • ESM & Browser Fixes:
    • Attached .catch(() => {}) on floating background promises (e.g., in indexed_db.ts and recaptcha_enterprise_verifier.ts) to eliminate unhandled promise rejections in browser tests.
    • Handled pending operation promise guards in abstract_popup_redirect_operation.ts.
    • Converted export const enum DefaultConfig in src/core/auth/auth_impl.ts to export enum DefaultConfig to preserve enum values at runtime in ESM.
    • Converted type-only re-exports in src/core/index.ts and src/model/public_types.ts to export type { ... }.
  • Changeset:
    • Added .changeset/vitest-auth-migration.md (@firebase/auth: patch) documenting the test suite migration, enum preservation, and type re-exports.

Testing & Impact

  • Test Count: 1,739 tests passed across 173 test files (75 Node files [719 passed, 3 skipped], 98 Browser files [1,020 passed]) [100% parity, 0 regressions].
  • Build & Verification: yarn build, API extractor (docgen), and yarn format succeed cleanly with 0 unintended API changes.

@Manvi1203
Manvi1203 requested review from a team and sam-gc as code owners September 15, 2026 22:53
@changeset-bot

changeset-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 329354f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/auth Patch
@firebase/auth-compat Patch
firebase Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request migrates the @firebase/auth test suite from Karma, Mocha, Chai, and Sinon to Vitest, which includes deleting the Karma configuration, updating package.json scripts, and refactoring assertions and mocks across numerous test files. Feedback on these changes highlights a critical bug in auth_impl.test.ts where await setTimeout is used incorrectly, causing assertions to be skipped. Additionally, several obsolete test scripts in package.json still reference the deleted Karma runner and need migration or removal, and a conditional test block in redirect.test.ts can be refactored to use Vitest's idiomatic describe.runIf() method.

expect(auth.currentUser).toBe(null);
expect(authStateChangedSpy).toHaveBeenCalledTimes(1);

await setTimeout(() => {

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.

critical

Awaiting setTimeout does not pause execution because the global setTimeout returns a timeout ID (or a Timeout object in Node), not a Promise. As a result, this test finishes executing synchronously before the 5-second delay completes, meaning the assertions inside the .then() block are never actually run or verified.\n\nTo fix this, you should use a Promise-based delay or Vitest's fake timers. For example:\n\ntypescript\nawait new Promise(resolve => setTimeout(resolve, 5000));\nawait promiseVar;\nexpect(auth.currentUser).toBe(user);\n

"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all",
"test:integration:local": "run-s --npm-path npm test:node:integration:local test:browser:integration:local test:webdriver",
"test:browser": "karma start --local",
"test:browser": "vitest run --project=browser",

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.

high

Since karma.conf.js has been deleted in this PR, several remaining scripts that still reference karma start (such as test:browser:unit, test:browser:integration, test:cordova, etc.) are now broken. Please migrate these remaining test suites to Vitest or remove the obsolete scripts.

Comment on lines 216 to 218
resolver,
true
);

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.

medium

Instead of using a ternary operator to conditionally skip the describe block, you can use Vitest's idiomatic describe.runIf() method.

  describe.runIf(typeof window !== 'undefined')(
    '_getAndClearPendingRedirectStatus',
    () => {

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.

1 participant