Skip to content

test(installations): migrate installations to Vitest - #10369

Merged
Manvi1203 merged 11 commits into
mainfrom
feature/vitest-installations
Sep 24, 2026
Merged

Manvi1203 merged 11 commits into
mainfrom
feature/vitest-installations

Conversation

@Manvi1203

@Manvi1203 Manvi1203 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Migrates @firebase/installations (packages/installations) unit test suites (src/**/*.test.ts) from Karma + Mocha + Chai + Sinon to Vitest in the Browser (Playwright Chromium) environment.


Description

Test Runner Config, Setup & CI Workflows

File What Changed & Why
packages/installations/vitest.config.mjs Replaces karma.conf.js and Webpack preprocessor. Extends shared config/vitest.base.mjs and filters projects to the browser project (project.test.name === 'browser') since @firebase/installations relies on browser primitives (IndexedDB, Web Crypto crypto.getRandomValues).
packages/installations/src/testing/setup.ts Registers custom header matcher (compare-headers.ts) and restores mocks/timers (vi.useRealTimers(), vi.restoreAllMocks()) in afterEach across all Vitest runs.
packages/installations/src/testing/vitest-globals.d.ts Declares /// <reference types="vitest/globals" /> and augments Assertion<T> with toContainHeaders(expectedHeaders: Record<string, string>) so test files do not need runtime import { ... } from 'vitest'.
packages/installations/package.json Updates test:* scripts (test, test:all, test:browser, test:ci, test:browser:debug) to run Vitest via Playwright browser provider (vitest run --project=browser).

Installations Test Harness & Custom Helpers

File What Changed & Why (Vitest Context)
packages/installations/src/testing/compare-headers.ts Replaces the legacy Chai Assertion.addMethod('containHeaders', ...) plugin with expect.extend({ toContainHeaders(received, expectedHeaders) }) so Headers assertions work natively with Vitest expect(headers).toContainHeaders(...).
packages/installations/src/functions/get-id.test.ts & packages/installations/src/functions/get-token.test.ts Why it changed: Native browser ESM module namespaces are sealed (TypeError: Cannot redefine property), so sinon.stub(moduleNamespace, 'fn') cannot mutate exports directly. Added vi.mock('../helpers/get-installation-entry', { spy: true }), vi.mock('../helpers/refresh-auth-token', { spy: true }), and vi.mock('../api/on-id-change', { spy: true }) so vi.spyOn(module, 'fn') can intercept calls while keeping the real implementation active by default.

Mechanical Test Suite Migrations (~17 *.test.ts files)

All remaining files under packages/installations/src/**/*.test.ts are mechanical translations following the cheat sheet below.


Vitest Equivalents

Vitest uses a Jest-compatible expect and mocking API (vi) while keeping describe / it test structure:

Concept Legacy (Mocha + Chai + Sinon + Karma) New (Vitest + Playwright Browser)
Suite Lifecycle Hooks before(() => ...) / after(() => ...) beforeAll(() => ...) / afterAll(() => ...) (beforeEach / afterEach are unchanged)
Strict Equality (===) expect(x).to.equal(y) / expect(x).to.be.true expect(x).toBe(y) / expect(x).toBe(true)
Deep Structural Equality expect(x).to.deep.equal(y) expect(x).toEqual(y)
Custom Header Matcher expect(headers).to.containHeaders(expected) expect(headers).toContainHeaders(expected) (expect.extend)
Async Promise Rejection await expect(promise).to.be.rejectedWith(/msg/) await expect(promise).rejects.toThrow(/msg/)
Spies, Stubs & Fake Timers sinon.spy(), sinon.stub(obj, 'm'), sinon.useFakeTimers() vi.fn(), vi.spyOn(obj, 'm'), vi.useFakeTimers()
ESM Module Namespace Spying sinon.stub(moduleNamespace, 'fn') vi.mock('./module', { spy: true }) + vi.spyOn(moduleNamespace, 'fn')

Other Changes

  1. Browser-Only Project Configuration:
    @firebase/installations is a browser-first SDK that depends directly on IndexedDB and crypto.getRandomValues. Filtering vitest.config.mjs to project.test.name === 'browser' ensures tests run in a real Chromium environment via Playwright rather than against synthetic Node shims.
  2. Zero Runtime vitest Imports:
    All test files rely on globals: true and src/testing/vitest-globals.d.ts, keeping test files clean without boilerplate import { describe, it, expect, vi } from 'vitest' statements.

Performance Improvement

Environment Before (Karma + Webpack) After (Vitest)
Browser Unit Tests (test:browser) ~20–30s (Karma + Webpack bundle) ~8.2s (~2.5x–3.5x faster)

@Manvi1203
Manvi1203 requested review from a team and avolkovi as code owners September 10, 2026 23:46
@changeset-bot

changeset-bot Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 9203d69

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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 test suite of the @firebase/installations package from Karma, Mocha, Chai, and Sinon to Vitest, updating scripts, configurations, assertions, and mocks. While the migration is mostly complete, several issues need to be addressed: the obsolete test:node script should be removed as only browser tests are configured; multiple test files incorrectly pass { spy: true } to vi.mock, which is unsupported in Vitest; and legacy Chai assertion syntax (such as .to.equal and .to.throw) remains in sleep.test.ts, buffer-to-base64-url-safe.test.ts, and extract-app-config.test.ts, which will cause runtime errors.

Comment thread packages/installations/package.json Outdated
Comment thread packages/installations/src/api/delete-installations.test.ts
Comment thread packages/installations/src/api/get-id.test.ts
Comment thread packages/installations/src/api/get-id.test.ts
Comment thread packages/installations/src/api/get-token.test.ts
Comment thread packages/installations/src/helpers/idb-manager.test.ts
Comment thread packages/installations/src/helpers/refresh-auth-token.test.ts
Comment thread packages/installations/src/util/sleep.test.ts
Comment thread packages/installations/src/helpers/buffer-to-base64-url-safe.test.ts Outdated
Comment thread packages/installations/src/helpers/extract-app-config.test.ts Outdated
…t residual Chai matchers

- Remove non-functional test:node script since Vitest runner is filtered to browser only
- Convert residual .to.equal, .to.deep.equal, and .to.throw Chai assertions to native Vitest matchers
- Update outdated Chai comment in compare-headers helper
Comment thread packages/installations/src/testing/setup.ts
Comment thread packages/installations/package.json Outdated
@Manvi1203
Manvi1203 force-pushed the feature/vitest-installations branch from 8e2e253 to 2a78ace Compare September 18, 2026 18:09
@Manvi1203
Manvi1203 force-pushed the feature/vitest-installations branch from a564c09 to d66b02c Compare September 23, 2026 20:01
@Manvi1203
Manvi1203 merged commit 32a7c8c into main Sep 24, 2026
51 of 53 checks passed
@Manvi1203
Manvi1203 deleted the feature/vitest-installations branch September 24, 2026 20:53
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