-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: improve coverage for hosting implicit SDK initialization #10360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { expect } from "chai"; | ||
| import * as sinon from "sinon"; | ||
| import { implicitInit } from "./implicitInit"; | ||
| import * as fetchWebSetupModule from "../fetchWebSetup"; | ||
| import * as templatesModule from "../templates"; | ||
| import { EmulatorRegistry } from "../emulator/registry"; | ||
| import { Emulators } from "../emulator/types"; | ||
| import * as utils from "../utils"; | ||
|
|
||
| describe("hosting/implicitInit", () => { | ||
| let fetchWebSetupStub: sinon.SinonStub; | ||
| let getCachedWebSetupStub: sinon.SinonStub; | ||
| let readTemplateStub: sinon.SinonStub; | ||
|
Check failure on line 13 in src/hosting/implicitInit.spec.ts
|
||
| let getInfoStub: sinon.SinonStub; | ||
| let urlStub: sinon.SinonStub; | ||
| let logWarningStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| fetchWebSetupStub = sinon.stub(fetchWebSetupModule, "fetchWebSetup"); | ||
| getCachedWebSetupStub = sinon.stub(fetchWebSetupModule, "getCachedWebSetup"); | ||
| readTemplateStub = sinon.stub(templatesModule, "readTemplateSync").returns( | ||
|
Check failure on line 21 in src/hosting/implicitInit.spec.ts
|
||
| "/*--CONFIG--*/\n/*--EMULATORS--*/" | ||
| ); | ||
| getInfoStub = sinon.stub(EmulatorRegistry, "getInfo"); | ||
| urlStub = sinon.stub(EmulatorRegistry, "url"); | ||
| logWarningStub = sinon.stub(utils, "logLabeledWarning"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it("should successfully initialize with direct configuration", async () => { | ||
| const mockConfig = { appId: "my-app" }; | ||
| fetchWebSetupStub.resolves(mockConfig); | ||
|
|
||
| const res = await implicitInit({}); | ||
|
|
||
| expect(res.json).to.include('"appId": "my-app"'); | ||
| expect(res.js).to.include("var firebaseConfig = {\n \"appId\": \"my-app\"\n}"); | ||
|
Check failure on line 40 in src/hosting/implicitInit.spec.ts
|
||
| expect(res.js).to.include("var firebaseEmulators = undefined"); | ||
| }); | ||
|
|
||
| it("should fallback to cache if fetch fails with 403", async () => { | ||
| const err: any = new Error("Auth failed"); | ||
| err.context = { response: { statusCode: 403 } }; | ||
| fetchWebSetupStub.rejects(err); | ||
| getCachedWebSetupStub.returns({ appId: "cached-app" }); | ||
|
|
||
| const res = await implicitInit({}); | ||
|
|
||
| expect(logWarningStub).to.have.been.calledWithMatch("hosting", /Authentication error/); | ||
| expect(res.json).to.include('"appId": "cached-app"'); | ||
| }); | ||
|
|
||
| it("should log warning when absolutely no config is available", async () => { | ||
| fetchWebSetupStub.rejects(new Error("Network off")); | ||
| getCachedWebSetupStub.returns(undefined); | ||
|
|
||
| const res = await implicitInit({}); | ||
|
|
||
| expect(logWarningStub).to.have.been.calledWithMatch( | ||
| "hosting", | ||
| /Could not fetch web app configuration/ | ||
|
Check failure on line 64 in src/hosting/implicitInit.spec.ts
|
||
| ); | ||
| expect(res.json).to.equal(undefined); | ||
| }); | ||
|
|
||
| it("should populate emulators list when active", async () => { | ||
| fetchWebSetupStub.resolves({ appId: "app" }); | ||
| getInfoStub.withArgs(Emulators.FIRESTORE).returns({ host: "localhost", port: 8080 }); | ||
| urlStub.withArgs(Emulators.FIRESTORE).returns({ host: "localhost:8080" }); | ||
|
|
||
| const res = await implicitInit({}); | ||
|
|
||
| expect(res.emulatorsJs).to.include('"host": "localhost"'); | ||
| expect(res.emulatorsJs).to.include('"port": 8080'); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using
anyas an escape hatch, as per the repository style guide (Line 38). You can use a more specific type or an intersection type to define the expected error structure for testing purposes.References
anyorunknownas an escape hatch. Define proper interfaces/types or use type guards. (link)