cagov / ui-claim-tracker

This repo contains the Claim Status Tracker app, which helps Californians better understand what’s happening with their unemployment claim and benefits.
9 stars 4 forks source link

Correct typing issues with mocks in queryApiGateway test #587

Open lomky opened 2 years ago

lomky commented 2 years ago

Description

Issues with how the Jest mocks and the Typescript server interact means that the typing reports errors for every mocked function we are invoking. In particular, calls to fetch and fs return type errors as if they were the actual objects, instead of their mocked counterparts.

Discovered & spun out of #571

$ yarn typecheck
yarn run v1.22.17
$ tsc -p .
tests/utils/queryApiGateway.test.ts:50:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

50     fetch.mockResolvedValue(new Response(JSON.stringify(mockedResponse)))
             ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:54:21 - error TS2339: Property 'mockImplementation' does not exist on type '{ (path: number | PathLike, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer; (path: number | PathLike, options: BufferEncoding | { ...; }): string; (path: number | PathLike, options?: BufferEncoding | ... 2 more ... | undefined): string | Buffer; }'.

54     fs.readFileSync.mockImplementation(() => {
                       ~~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:71:34 - error TS2554: Expected 1-2 arguments, but got 0.

71     const resp: Response = await fetch()
                                    ~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:19624:24
    19624 declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
                                 ~~~~~~~~~~~~~~~~~~
    An argument for 'input' was not provided.

tests/utils/queryApiGateway.test.ts:97:11 - error TS2339: Property 'mockRejectedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

97     fetch.mockRejectedValue(new Error(networkErrorMessage))
             ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:121:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

121     fetch.mockResolvedValue(errorResponse403)
              ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:145:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

145     fetch.mockResolvedValue(new Response('not json'))
              ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:168:21 - error TS2339: Property 'mockImplementation' does not exist on type '{ (path: number | PathLike, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer; (path: number | PathLike, options: BufferEncoding | { ...; }): string; (path: number | PathLike, options?: BufferEncoding | ... 2 more ... | undefined): string | Buffer; }'.

168     fs.readFileSync.mockImplementation(() => {
                        ~~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:194:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

194     fetch.mockResolvedValue(new Response(JSON.stringify(null)))
              ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:252:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

252     fetch.mockResolvedValue(new Response(JSON.stringify(longNullishResponse)))
              ~~~~~~~~~~~~~~~~~

tests/utils/queryApiGateway.test.ts:282:11 - error TS2339: Property 'mockResolvedValue' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.

282     fetch.mockResolvedValue(new Response(JSON.stringify(shortNullResponse)))
              ~~~~~~~~~~~~~~~~~

Found 10 errors.

Acceptance Criteria