Skip to content

Commit

Permalink
try to fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
ptpaterson committed May 8, 2024
1 parent 11319ea commit 56fcd82
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions __tests__/unit/fetch-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
} from "../../src";
import { getDefaultHTTPClientOptions } from "../client";

let fetchClient: FetchClient;

const dummyRequest: HTTPRequest = {
client_timeout_ms: 10000,
data: { query: "" },
Expand All @@ -33,20 +31,16 @@ const dummyStats = {
};

describe("fetch client", () => {
beforeAll(() => {
fetchClient = new FetchClient(getDefaultHTTPClientOptions());
});

afterAll(() => {
fetchClient.close();
});

beforeEach(() => {
jest.resetModules();
fetchMock.resetMocks();
});

it("returns a valid query response on success", async () => {
expect.assertions(7);

const fetchClient = new FetchClient(getDefaultHTTPClientOptions());

const expected: QuerySuccess<{ foo: string }> = {
data: { foo: "bar" },
summary: "summary",
Expand All @@ -73,10 +67,15 @@ describe("fetch client", () => {
expect(body.query_tags).toEqual(expected.query_tags);
expect(body.stats).toEqual(expected.stats);
}

fetchClient.close()
});

it("returns a valid query response on failure", async () => {
expect.assertions(7);

const fetchClient = new FetchClient(getDefaultHTTPClientOptions());

const expected: QueryFailure = {
error: {
code: "invalid_query",
Expand Down Expand Up @@ -107,10 +106,15 @@ describe("fetch client", () => {
expect(body.query_tags).toEqual(expected.query_tags);
expect(body.stats).toEqual(expected.stats);
}

fetchClient.close()
});

it("returns a NetworkError if fetch rejects", async () => {
expect.assertions(2);

Check failure on line 114 in __tests__/unit/fetch-client.test.ts

View workflow job for this annotation

GitHub Actions / Jest Tests 20

__tests__/unit/fetch-client.test.ts ► fetch client ► returns a NetworkError if fetch rejects

Failed test found in: reports/jest-junit.xml Error: Error: expect.assertions(2)
Raw output
Error: expect.assertions(2)

Expected two assertions to be called but received zero assertion calls.
    at Object.<anonymous> (/home/runner/work/fauna-js/fauna-js/__tests__/unit/fetch-client.test.ts:114:12)
    at Promise.then.completed (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/utils.js:293:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/utils.js:226:10)
    at _callCircusTest (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/run.js:248:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/run.js:184:3)
    at _runTestsForDescribeBlock (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/run.js:86:9)
    at _runTestsForDescribeBlock (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/run.js:81:9)
    at run (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/run.js:26:3)
    at runAndTransformResultsToJestFormat (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:120:21)
    at jestAdapter (/home/runner/work/fauna-js/fauna-js/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/home/runner/work/fauna-js/fauna-js/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/home/runner/work/fauna-js/fauna-js/node_modules/jest-runner/build/runTest.js:444:34)
    at Object.worker (/home/runner/work/fauna-js/fauna-js/node_modules/jest-runner/build/testWorker.js:106:12)

const fetchClient = new FetchClient(getDefaultHTTPClientOptions());

fetchMock.mockRejectOnce(new Error("oops"));
try {
await fetchClient.request(dummyRequest);
Expand All @@ -122,6 +126,8 @@ describe("fetch client", () => {
expect(e.cause).toBeDefined();
}
}

fetchClient.close()
});

it("returns a NetworkError if client timeout causes an abort", async () => {
Expand All @@ -134,6 +140,8 @@ describe("fetch client", () => {
const badClient = new FetchClient(getDefaultHTTPClientOptions());

await badClient.request({ ...dummyRequest, client_timeout_ms: 1 });

badClient.close()
} catch (e) {
if (e instanceof NetworkError) {
expect(e.message).toEqual(
Expand Down

0 comments on commit 56fcd82

Please sign in to comment.