test(installations): migrate installations to Vitest - #10369
Conversation
|
There was a problem hiding this comment.
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.
…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
…elism to prevent IndexedDB race conditions
…to prevent mock restoration
… document unhandledrejection listener
…llation-entry.test.ts
8e2e253 to
2a78ace
Compare
a564c09 to
d66b02c
Compare
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
packages/installations/vitest.config.mjskarma.conf.jsand Webpack preprocessor. Extends sharedconfig/vitest.base.mjsand filters projects to thebrowserproject (project.test.name === 'browser') since@firebase/installationsrelies on browser primitives (IndexedDB, Web Cryptocrypto.getRandomValues).packages/installations/src/testing/setup.tscompare-headers.ts) and restores mocks/timers (vi.useRealTimers(),vi.restoreAllMocks()) inafterEachacross all Vitest runs.packages/installations/src/testing/vitest-globals.d.ts/// <reference types="vitest/globals" />and augmentsAssertion<T>withtoContainHeaders(expectedHeaders: Record<string, string>)so test files do not need runtimeimport { ... } from 'vitest'.packages/installations/package.jsontest:*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
packages/installations/src/testing/compare-headers.tsAssertion.addMethod('containHeaders', ...)plugin withexpect.extend({ toContainHeaders(received, expectedHeaders) })soHeadersassertions work natively with Vitestexpect(headers).toContainHeaders(...).packages/installations/src/functions/get-id.test.ts&packages/installations/src/functions/get-token.test.tsTypeError: Cannot redefine property), sosinon.stub(moduleNamespace, 'fn')cannot mutate exports directly. Addedvi.mock('../helpers/get-installation-entry', { spy: true }),vi.mock('../helpers/refresh-auth-token', { spy: true }), andvi.mock('../api/on-id-change', { spy: true })sovi.spyOn(module, 'fn')can intercept calls while keeping the real implementation active by default.Mechanical Test Suite Migrations (~17
*.test.tsfiles)All remaining files under
packages/installations/src/**/*.test.tsare mechanical translations following the cheat sheet below.Vitest Equivalents
Vitest uses a Jest-compatible
expectand mocking API (vi) while keepingdescribe/ittest structure:before(() => ...)/after(() => ...)beforeAll(() => ...)/afterAll(() => ...)(beforeEach/afterEachare unchanged)===)expect(x).to.equal(y)/expect(x).to.be.trueexpect(x).toBe(y)/expect(x).toBe(true)expect(x).to.deep.equal(y)expect(x).toEqual(y)expect(headers).to.containHeaders(expected)expect(headers).toContainHeaders(expected)(expect.extend)await expect(promise).to.be.rejectedWith(/msg/)await expect(promise).rejects.toThrow(/msg/)sinon.spy(),sinon.stub(obj, 'm'),sinon.useFakeTimers()vi.fn(),vi.spyOn(obj, 'm'),vi.useFakeTimers()sinon.stub(moduleNamespace, 'fn')vi.mock('./module', { spy: true })+vi.spyOn(moduleNamespace, 'fn')Other Changes
@firebase/installationsis a browser-first SDK that depends directly onIndexedDBandcrypto.getRandomValues. Filteringvitest.config.mjstoproject.test.name === 'browser'ensures tests run in a real Chromium environment via Playwright rather than against synthetic Node shims.vitestImports:All test files rely on
globals: trueandsrc/testing/vitest-globals.d.ts, keeping test files clean without boilerplateimport { describe, it, expect, vi } from 'vitest'statements.Performance Improvement
test:browser)