Currently, I am in the process of writing integration tests for the API routes within a Next.js application. One question that has arisen is whether it would be problematic to place the index.test.ts
file under the /pages
directory. My preference leans towards keeping the test file as close to its associated code file as possible, rather than having to organize it within a separate __test__
directory.
./pages/api/path/index.ts
handler.get(async (req: NextApiRequest, res: NextApiResponse) => {
...
});
export default handler;
./pages/api/path/index.test.ts
import { testClient } from "__test__/utils/testClient";
describe("Testing API POST: /api", () => {
test("Should return 401 when not authenticated", async () => {
const request = testClient({ handler });
const res = await request.post("/api/applications");
expect(res.status).toEqual(401);
});
});