【发布时间】:2022-01-25 16:35:59
【问题描述】:
我正在为 Next.js 应用程序中的 API 路由编写集成测试,我想知道将 index.test.ts 文件放在 /pages 目录下是否有任何问题。我希望测试尽可能接近文件,而不是必须在 __test__ 目录中映射项目结构。
./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);
});
});
【问题讨论】:
标签: javascript testing jestjs next.js