如何使用Jest测试NextJS应用程序的路由API路由

huangapple go评论97阅读模式
英文:

How to test NextJS App Router API route with Jest

问题

以下是翻译好的内容:

  1. 我有一个非常简单的API路由,我想要测试:
  2. 文件 ./src/app/api/name
  3. ```typescript
  4. import { NextResponse } from 'next/server';
  5. export async function GET() {
  6. const name = process.env.NAME;
  7. return NextResponse.json({
  8. name,
  9. });
  10. }

我尝试这样测试它:

  1. import { GET } from './route';
  2. describe('name', () => {
  3. it('should return url from environment variables', async () => {
  4. const result = await GET();
  5. const { name } = await result.json();
  6. expect(name).toEqual('test');
  7. });
  8. });

但是然后出现了错误:

  1. FAIL src/app/api/name/route.spec.ts
  2. Test suite failed to run
  3. ReferenceError: Response is not defined
  4. 9 | }
  5. 10 |
  6. at Object.Response (node_modules/next/src/server/web/spec-extension/response.ts:29:51)
  7. at Object.<anonymous> (node_modules/next/dist/server/web/exports/next-response.js:12:19)
  8. at Object.<anonymous> (src/api/name/route.ts:11:62)
  9. at Object.<anonymous> (src/api/name/route.spec.ts:5:16)

我尝试按照一些关于测试NextJS API路由的说明,但它们都基于页面路由。

我该如何正确测试这个?

  1. <details>
  2. <summary>英文:</summary>
  3. I have a very simple API route which I would like to test:
  4. File ./src/app/api/name
  5. ```typescript
  6. import { NextResponse } from &#39;next/server&#39;;
  7. export async function GET() {
  8. const name = process.env.NAME;
  9. return NextResponse.json({
  10. name,
  11. });
  12. }

I have tried to test this like this:

  1. import { GET } from &#39;./route&#39;;
  2. describe(&#39;name&#39;, () =&gt; {
  3. it(&#39;should return url from environment variables&#39;, async () =&gt; {
  4. const result = await GET();
  5. const { name } = await result.json();
  6. expect(name).toEqual(&#39;test&#39;);
  7. });
  8. });

But then an error is thrown:

  1. FAIL src/app/api/name/route.spec.ts
  2. Test suite failed to run
  3. ReferenceError: Response is not defined
  4. 9 | }
  5. 10 |
  6. at Object.Response (node_modules/next/src/server/web/spec-extension/response.ts:29:51)
  7. at Object.&lt;anonymous&gt; (node_modules/next/dist/server/web/exports/next-response.js:12:19)
  8. at Object.&lt;anonymous&gt; (src/api/name/route.ts:11:62)
  9. at Object.&lt;anonymous&gt; (src/api/name/route.spec.ts:5:16)

I tried to follow some instructions on testing NextJS API routes, but they're all based on the Pages Router.

How can I properly test this?

答案1

得分: 1

我已找到解决方案。测试未能正常工作,因为我的jest配置将'testEnvironment'设置为'jest-environment-jsdom'。

在.spec.ts文件的顶部添加以下内容后,初始测试设置确实有效:

  1. /**
  2. * @jest-environment node
  3. */
英文:

I have found the solution. The test didn't work, since my jest configuration sets 'testEnvironment' to 'jest-environment-jsdom'.

The initial test setup does work after adding the following to the top of the .spec.ts file:

  1. /**
  2. * @jest-environment node
  3. */

huangapple
  • 本文由 发表于 2023年6月1日 15:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定