diff --git a/test/type.test.ts b/test/type.test.ts new file mode 100644 index 00000000..77d3248b --- /dev/null +++ b/test/type.test.ts @@ -0,0 +1,61 @@ +import { NextResponse } from 'next/server'; +import { describe, expect, it } from 'tstyche'; + +import { testApiHandler } from 'universe'; + +import type { NextRequest } from 'next/server'; +import type { NtarhInitAppRouter } from 'universe'; + +describe('::testApiHandler', () => { + describe('', () => { + it('supports type generics', async () => { + await testApiHandler<{ a: number }>({ + appHandler: { + async GET() { + // ? We shouldn't be type checking this deep + return Response.json({ b: 1 }); + } + }, + test: async ({ fetch }) => { + expect(await (await fetch()).json()).type.toHaveProperty('a'); + expect(await (await fetch()).json()).type.not.toHaveProperty('b'); + } + }); + }); + + it('does not throw any AppRouteUserlandModule-related type errors', async () => { + expect({ + async GET(_req: NextRequest, _params: { params: { recipeId: string } }) { + return NextResponse.json({}); + } + }).type.toBeAssignableTo(); + + expect({ + async LOL() { + return NextResponse.json({}); + }, + async GET(_req: NextRequest, _params: { params: { recipeId: string } }) { + return NextResponse.json({}); + } + }).type.not.toBeAssignableTo(); + }); + }); + + describe('', () => { + it('supports type generics', async () => { + await testApiHandler<{ a: number }>({ + pagesHandler: async (_, res) => { + // ? Formerly we would expect an error to be thrown here, but in + // ? NTARH@4 we've relaxed the type checking so that only fetch::json + // ? is affected. I believe this leads to more accurate and more + // ? useful type checking for NTARH users. + res.send({ b: 1 }); + }, + test: async ({ fetch }) => { + expect(await (await fetch()).json()).type.toHaveProperty('a'); + expect(await (await fetch()).json()).type.not.toHaveProperty('b'); + } + }); + }); + }); +}); diff --git a/test/unit-index.test.ts b/test/unit-index.test.ts index 63359c57..0e1d1897 100644 --- a/test/unit-index.test.ts +++ b/test/unit-index.test.ts @@ -11,7 +11,6 @@ import { NextResponse } from 'next/server'; import { $originalGlobalFetch, testApiHandler } from 'universe'; import type { NextApiRequest, NextApiResponse } from 'next'; -import type { NextRequest } from 'next/server'; jest.mock('cookie'); @@ -1458,49 +1457,6 @@ describe('::testApiHandler', () => { ]); }); }); - - // TODO: move this test to tstyche - it('supports type generics', async () => { - expect.hasAssertions(); - - await testApiHandler<{ a: number }>({ - appHandler: { - async GET() { - // ? We shouldn't be type checking this deep - return Response.json({ b: 1 }); - } - }, - test: async ({ fetch }) => { - // @ts-expect-error: b does not exist (this test "fails" if no TS error) - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - (await (await fetch()).json()).b; - expect(true).toBe(true); - } - }); - }); - - it('does not throw any AppRouteUserlandModule-related type errors', async () => { - expect.hasAssertions(); - - const appHandler = { - async GET(req: NextRequest, { params }: { params: { recipeId: string } }) { - const { recipeId } = params; - expect(req).toBeDefined(); - expect(recipeId).toBe('1'); - return NextResponse.json({}); - } - }; - - await testApiHandler({ - rejectOnHandlerError: true, - // This test "fails" if there is a TS error (caught by CI/linting) - appHandler, - params: { recipeId: '1' }, - async test({ fetch }) { - await expect(fetch()).resolves.toBeDefined(); - } - }); - }); }); describe('', () => { @@ -2448,26 +2404,5 @@ describe('::testApiHandler', () => { expect(errorSpy).toHaveBeenCalledTimes(4); }); }); - - // TODO: move this test to tstyche - it('supports type generics', async () => { - expect.hasAssertions(); - - await testApiHandler<{ a: number }>({ - pagesHandler: async (_, res) => { - // ? Formerly we would expect an error to be thrown here, but in - // ? NTARH@4 we've relaxed the type checking so that only fetch::json - // ? is affected. I believe this leads to more accurate and more - // ? useful type checking for NTARH users. - res.send({ b: 1 }); - }, - test: async ({ fetch }) => { - // @ts-expect-error: b does not exist (this test "fails" if no TS error) - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - (await (await fetch()).json()).b; - expect(true).toBe(true); - } - }); - }); }); });