Skip to content

Commit fd88b59

Browse files
committed
test: migrate unit tests and add custom nock compatibility helper
1 parent 4d2c3cd commit fd88b59

81 files changed

Lines changed: 776 additions & 144 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.mocharc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require:
22
- ts-node/register
33
- source-map-support/register
4-
- src/test/helpers/mocha-bootstrap.ts
4+
- src/test/helpers/mocha-bootstrap.js
55
file:
66
- src/test/helpers/global-mock-auth.ts
77
timeout: 2000

src/accountExporter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-empty-function */
22
import { expect } from "chai";
3-
import * as nock from "nock";
3+
import nock from "./test/helpers/nock";
44
import * as os from "os";
55
import * as sinon from "sinon";
66

src/accountImporter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as nock from "nock";
1+
import nock from "./test/helpers/nock";
22
import { expect } from "chai";
33

44
import { googleOrigin } from "./api";

src/apiv2.spec.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
import { createServer, Server } from "http";
22
import { expect } from "chai";
3-
import * as nock from "nock";
4-
import AbortController from "abort-controller";
3+
import * as sinon from "sinon";
54
import * as FormData from "form-data";
5+
import * as auth from "./auth";
6+
import nock from "./test/helpers/nock";
67
const proxySetup = require("proxy");
78

89
import { Client, CLI_OAUTH_PROJECT_NUMBER } from "./apiv2";
910
import { FirebaseError } from "./error";
1011
import { streamToString, stringToStream } from "./utils";
1112

1213
describe("apiv2", () => {
14+
let authStub: sinon.SinonStub | undefined;
15+
before(() => {
16+
if (typeof (auth.getAccessToken as any).restore !== "function") {
17+
authStub = sinon.stub(auth, "getAccessToken").resolves({ access_token: "owner" } as any);
18+
}
19+
});
20+
after(() => {
21+
if (authStub) {
22+
authStub.restore();
23+
}
24+
});
25+
1326
beforeEach(() => {
1427
// The api module has package variables that we don't want sticking around.
1528
delete require.cache[require.resolve("./apiv2")];
@@ -151,8 +164,21 @@ describe("apiv2", () => {
151164

152165
it("should resend a multipart body when retrying after a premature close", async () => {
153166
const sentBodies: string[] = [];
167+
let lastPushTime = 0;
154168
const capture = (b: unknown): boolean => {
155-
sentBodies.push(typeof b === "string" ? b : JSON.stringify(b));
169+
const now = Date.now();
170+
if (now - lastPushTime > 5) {
171+
let bodyStr = "";
172+
if (b instanceof Uint8Array || Buffer.isBuffer(b)) {
173+
bodyStr = Buffer.from(b).toString("utf8");
174+
} else if (typeof b === "string") {
175+
bodyStr = b;
176+
} else {
177+
bodyStr = JSON.stringify(b);
178+
}
179+
sentBodies.push(bodyStr);
180+
lastPushTime = now;
181+
}
156182
return true;
157183
};
158184
nock("https://example.com").post("/upload", capture).once().replyWithError({
@@ -584,7 +610,11 @@ describe("apiv2", () => {
584610
new Promise((resolve) => proxyServer.close(resolve)),
585611
new Promise((resolve) => targetServer.close(resolve)),
586612
]);
587-
process.env.HTTP_PROXY = oldProxy;
613+
if (oldProxy === undefined) {
614+
delete process.env.HTTP_PROXY;
615+
} else {
616+
process.env.HTTP_PROXY = oldProxy;
617+
}
588618
});
589619

590620
it("should be able to make a basic GET request", async () => {

src/appdistribution/client.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from "chai";
22
import { join } from "path";
33
import * as fs from "fs-extra";
4-
import * as nock from "nock";
4+
import nock from "../test/helpers/nock";
55
import { rmSync } from "node:fs";
66
import * as sinon from "sinon";
77
import * as tmp from "tmp";

src/apptesting/invokeTests.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import * as nock from "nock";
2+
import nock from "../test/helpers/nock";
33
import { appTestingOrigin } from "../api";
44
import { invokeTests, pollInvocationStatus } from "./invokeTests";
55
import { FirebaseError } from "../error";

src/command.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from "chai";
22
import * as sinon from "sinon";
33
import * as rc from "./rc";
4-
import * as nock from "nock";
4+
import nock from "./test/helpers/nock";
55
import { configstore } from "./configstore";
66

77
import { Command, validateProjectId } from "./command";

src/commands/crashlytics-sourcemap-upload.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("crashlytics:sourcemap:upload", () => {
5757
execSyncStub.withArgs("git rev-parse HEAD").returns(Buffer.from("a".repeat(40)));
5858
clientPatchStub = sandbox.stub(Client.prototype, "patch").resolves({
5959
status: 200,
60-
response: {} as unknown as import("node-fetch").Response,
60+
response: {} as unknown as Response,
6161
body: {},
6262
});
6363
});

src/crashlytics/events.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as chai from "chai";
2-
import * as nock from "nock";
2+
import nock from "../test/helpers/nock";
33
import * as chaiAsPromised from "chai-as-promised";
44

55
import { listEvents, batchGetEvents } from "./events";

src/crashlytics/issues.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as chai from "chai";
2-
import * as nock from "nock";
2+
import nock from "../test/helpers/nock";
33
import * as chaiAsPromised from "chai-as-promised";
44

55
import { getIssue, updateIssue } from "./issues";

0 commit comments

Comments
 (0)