Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/hosting/implicitInit.spec.ts
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

View workflow job for this annotation

GitHub Actions / unit (24)

'readTemplateStub' is assigned a value but never used

Check failure on line 13 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'readTemplateStub' is assigned a value but never used

Check failure on line 13 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'readTemplateStub' is assigned a value but never used
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

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `.stub(templatesModule,·"readTemplateSync").returns(⏎······"/*--CONFIG--*/\n/*--EMULATORS--*/"⏎····` with `⏎······.stub(templatesModule,·"readTemplateSync")⏎······.returns("/*--CONFIG--*/\n/*--EMULATORS--*/"`

Check failure on line 21 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `.stub(templatesModule,·"readTemplateSync").returns(⏎······"/*--CONFIG--*/\n/*--EMULATORS--*/"⏎····` with `⏎······.stub(templatesModule,·"readTemplateSync")⏎······.returns("/*--CONFIG--*/\n/*--EMULATORS--*/"`

Check failure on line 21 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `.stub(templatesModule,·"readTemplateSync").returns(⏎······"/*--CONFIG--*/\n/*--EMULATORS--*/"⏎····` with `⏎······.stub(templatesModule,·"readTemplateSync")⏎······.returns("/*--CONFIG--*/\n/*--EMULATORS--*/"`
"/*--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

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `"var·firebaseConfig·=·{\n··\"appId\":·\"my-app\"\n}"` with `'var·firebaseConfig·=·{\n··"appId":·"my-app"\n}'`

Check failure on line 40 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `"var·firebaseConfig·=·{\n··\"appId\":·\"my-app\"\n}"` with `'var·firebaseConfig·=·{\n··"appId":·"my-app"\n}'`

Check failure on line 40 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `"var·firebaseConfig·=·{\n··\"appId\":·\"my-app\"\n}"` with `'var·firebaseConfig·=·{\n··"appId":·"my-app"\n}'`
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");

Check warning on line 45 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
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

Avoid using any as 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.

Suggested change
const err: any = new Error("Auth failed");
const err = new Error("Auth failed") as Error & { context: { response: { statusCode: number } } };
References
  1. Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)

err.context = { response: { statusCode: 403 } };

Check warning on line 46 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .context on an `any` value
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

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`

Check failure on line 64 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 64 in src/hosting/implicitInit.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`
);
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');
});
});
Loading