Skip to content

Commit

Permalink
chore: quiet console.error in api.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
skyqrose committed Dec 16, 2024
1 parent fcc7aac commit b4dc03d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export default tseslint.config(
"@typescript-eslint/no-empty-function": "off",
// expect.any is untyped and triggers this all the time.
"@typescript-eslint/no-unsafe-assignment": "off",
// sometimes the easiest way to mock a complex test setup is to mutate something
"better-mutation/no-mutation": "off",
/* we do `view = render(); view.getBy()` which also doesn't require destructuring
and has less global mutable state than `screen`. */
"testing-library/prefer-screen-queries": "off",
Expand Down
20 changes: 17 additions & 3 deletions js/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ describe("get", () => {
});

describe("useApiResult", () => {
let spyConsoleError: jest.SpiedFunction<typeof console.error>;
beforeEach(() => {
spyConsoleError = jest.spyOn(console, "error");
});
afterEach(() => {
spyConsoleError.mockRestore();
});

test("returns loading state", () => {
const RawData = z.string();
const parser = (s: string) => s;
Expand Down Expand Up @@ -167,8 +175,10 @@ describe("useApiResult", () => {
const RawData = z.string();
const parser = (s: string) => s;

const { promise, resolve } = PromiseWithResolvers<Response>();
// this test triggers console.error. quiet it.
spyConsoleError.mockImplementationOnce(() => {});

const { promise, resolve } = PromiseWithResolvers<Response>();
jest.mocked(fetch).mockReturnValue(promise);

const { result } = renderHook(useApiResult, {
Expand All @@ -192,8 +202,10 @@ describe("useApiResult", () => {
const RawData = z.string();
const parser = (s: string) => s;

const { promise, resolve } = PromiseWithResolvers<Response>();
// this test triggers console.error. quiet it.
spyConsoleError.mockImplementationOnce(() => {});

const { promise, resolve } = PromiseWithResolvers<Response>();
jest.mocked(fetch).mockReturnValue(promise);

const { result } = renderHook(useApiResult, {
Expand All @@ -218,8 +230,10 @@ describe("useApiResult", () => {
const RawData = z.string();
const parser = (s: string) => s;

const { promise, resolve } = PromiseWithResolvers<Response>();
// this test triggers console.error. quiet it.
spyConsoleError.mockImplementationOnce(() => {});

const { promise, resolve } = PromiseWithResolvers<Response>();
jest.mocked(fetch).mockReturnValue(promise);

const { result } = renderHook(useApiResult, {
Expand Down
1 change: 0 additions & 1 deletion js/test/helpers/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable better-mutation/no-mutation */
import { MetaDataKey } from "../../util/metadata";

export const putMetaData = (key: MetaDataKey, value: string) => {
Expand Down
2 changes: 0 additions & 2 deletions js/test/helpers/promiseWithResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export const PromiseWithResolvers = <T>(): {
let resolve: ((value: T | PromiseLike<T>) => void) | undefined = undefined;
let reject: ((reason?: unknown) => void) | undefined = undefined;
const promise = new Promise<T>((res, rej) => {
// eslint-disable-next-line better-mutation/no-mutation
resolve = res;
// eslint-disable-next-line better-mutation/no-mutation
reject = rej;
});

Expand Down

0 comments on commit b4dc03d

Please sign in to comment.