Skip to content

Commit

Permalink
add a client.close call to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyalaribe committed Oct 13, 2023
1 parent ed162de commit 05987bd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions axios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe("Axios Interceptors", () => {
expect(JSON.stringify(response.body)).toBe(JSON.stringify({ ping: "pong" }));
expect(published).toBe(true);
expect(pingCalled).toBe(true);
await client.close();
});
it("should intercept axios via responseError", async () => {
let published = false;
Expand Down Expand Up @@ -124,5 +125,6 @@ describe("Axios Interceptors", () => {
expect(JSON.stringify(response.body)).toBe(JSON.stringify({ hello: "error" }));
// expect(published).toBe(true)
// expect(pingCalled).toBe(true)
await client.close();
});
});
4 changes: 4 additions & 0 deletions axios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ describe("Axios Interceptors", () => {
expect(JSON.stringify(response.body)).toBe(JSON.stringify({ ping: "pong" }));
expect(published).toBe(true);
expect(pingCalled).toBe(true);

await client.close();
});

it("should intercept axios via responseError", async () => {
Expand Down Expand Up @@ -113,5 +115,7 @@ describe("Axios Interceptors", () => {
expect(JSON.stringify(response.body)).toBe(JSON.stringify({ hello: "error" }));
// expect(published).toBe(true)
// expect(pingCalled).toBe(true)

await client.close();
});
});
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class APIToolkit {
}
return new APIToolkit(pubsubClient, topic_id, project_id, config);
}
async close() {
await __classPrivateFieldGet(this, _APIToolkit_topic, "f")?.flush();
await __classPrivateFieldGet(this, _APIToolkit_pubsub, "f")?.close();
}
static async getClientMetadata(rootURL, apiKey) {
const resp = await (0, node_fetch_1.default)(rootURL + "/api/client_metadata", {
method: "GET",
Expand Down
8 changes: 8 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(response.body.status).toBe("success");
expect(published).toBe(true);
await client.close();
});
it("should get data", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -138,6 +139,7 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);
await client.close();
});
it("should check sub routes", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -174,6 +176,7 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);
await client.close();
});
it("should check sub sub sub routes", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -214,6 +217,7 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);
await client.close();
});
it("should ignore path for endpoins with OPTION", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -250,6 +254,7 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("OPTIONS ignored");
expect(published).toBe(true);
await client.close();
});
});
describe("File Upload Endpoint", () => {
Expand Down Expand Up @@ -311,6 +316,7 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("File upload successful.");
expect(published).toBe(true);
await client.close();
});
it("should upload files (formidable)", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -362,6 +368,7 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("Uploaded successfully");
expect(published).toBe(true);
await client.close();
});
it("should upload files (busboy)", async () => {
const app = (0, express_1.default)();
Expand Down Expand Up @@ -422,6 +429,7 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("Uploaded successfully");
expect(published).toBe(true);
await client.close();
});
});
describe("testing headers and jsonpath redaction", () => {
Expand Down
16 changes: 16 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(response.body.status).toBe("success");
expect(published).toBe(true);

await client.close()
});

it("should get data", async () => {
Expand Down Expand Up @@ -126,6 +128,8 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);

await client.close()
});

it("should check sub routes", async () => {
Expand Down Expand Up @@ -167,6 +171,8 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);

await client.close()
});

it("should check sub sub sub routes", async () => {
Expand Down Expand Up @@ -212,6 +218,8 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData));
expect(published).toBe(true);

await client.close()
});

it("should ignore path for endpoins with OPTION", async () => {
Expand Down Expand Up @@ -251,6 +259,8 @@ describe("Express SDK API Tests", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("OPTIONS ignored");
expect(published).toBe(true);

await client.close()
});
});

Expand Down Expand Up @@ -325,6 +335,8 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("File upload successful.");
expect(published).toBe(true);

await client.close()
});

it("should upload files (formidable)", async () => {
Expand Down Expand Up @@ -379,6 +391,8 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("Uploaded successfully");
expect(published).toBe(true);

await client.close()
});

it("should upload files (busboy)", async () => {
Expand Down Expand Up @@ -443,6 +457,8 @@ describe("File Upload Endpoint", () => {
expect(response.status).toBe(200);
expect(response.body.message).toBe("Uploaded successfully");
expect(published).toBe(true);

await client.close()
});
});

Expand Down
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export class APIToolkit {
return new APIToolkit(pubsubClient, topic_id, project_id, config);
}

public async close(){
await this.#topic?.flush();
await this.#pubsub?.close();
}

static async getClientMetadata(rootURL: string, apiKey: string) {
const resp = await fetch(rootURL + "/api/client_metadata", {
method: "GET",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apitoolkit-express",
"version": "1.1.20",
"version": "1.1.21",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 05987bd

Please sign in to comment.