index.test.ts我正在 Next.js 应用程序中为 API 路由编写集成测试,我想知道将文件放在目录下是否有任何问题/pages。我希望测试尽可能接近文件,而不是必须在__test__目录内映射项目结构。
./pages/api/path/index.ts
handler.get(async (req: NextApiRequest, res: NextApiResponse) => {
...
});
export default handler;
Run Code Online (Sandbox Code Playgroud)
./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);
});
});
Run Code Online (Sandbox Code Playgroud)