Skip to content

Commit

Permalink
test: add tstyche type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Feb 8, 2025
1 parent c10e11e commit 18f166c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
61 changes: 61 additions & 0 deletions test/type.test.ts
Original file line number Diff line number Diff line change
@@ -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('<app router>', () => {
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<NtarhInitAppRouter['appHandler']>();

expect({
async LOL() {
return NextResponse.json({});
},
async GET(_req: NextRequest, _params: { params: { recipeId: string } }) {
return NextResponse.json({});
}
}).type.not.toBeAssignableTo<NtarhInitAppRouter['appHandler']>();
});
});

describe('<pages router>', () => {
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');
}
});
});
});
});
65 changes: 0 additions & 65 deletions test/unit-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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('<pages router>', () => {
Expand Down Expand Up @@ -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);
}
});
});
});
});

0 comments on commit 18f166c

Please sign in to comment.