测试后钩子中“无法读取未定义的属性”并打开 TCP 句柄

问题描述 投票:0回答:1

我正在开发一个 Fastify 项目,使用 Jest 和 Supertest 进行端到端测试。但是,我遇到了几个问题:

  • TypeError:无法读取未定义的属性(读取“长度”)- 当我尝试执行此操作时,此错误似乎源自 Fastify 挂钩 测试一个简单的健康检查端点。
  • 检测到打开 TCP 句柄 - 运行测试后,Jest 不退出 干净地检测到一个打开的 TCP 句柄 (TCPWRAP),即使我 在 afterAll() 钩子中调用 server.close() 。

这是我的基本测试设置:

测试(

server.e2e.test.ts
):

import supertest from 'supertest';
import buildServer from '../server';

describe('Server', () => {
  let app: Awaited<ReturnType<typeof buildServer>>;

  beforeAll(async () => {
    app = await buildServer();
  });

  afterAll(async () => {
    app.close();
  });

  test('GET /api/auth/healthcheck', async () => {
    await supertest(app.server).get('/api/auth/healthcheck').expect(200);
  });
});

Fastify 服务器 (

server.ts
):

import Fastify from 'fastify';
import sensiblePlugin from '@fastify/sensible';
import cookiePlugin from '@fastify/cookie';
import csrfPlugin from '@fastify/csrf-protection';
import helmetPlugin from '@fastify/helmet';
import authRoutes from './routes';

const buildServer = async () => {
  const fastify = Fastify({ logger: true });

  await fastify.register(sensiblePlugin);
  await fastify.register(cookiePlugin);
  await fastify.register(csrfPlugin);
  await fastify.register(helmetPlugin);
  await fastify.register(authRoutes, { prefix: '/api/auth' });

  return fastify;
};

export default buildServer;

错误1:

TypeError: Cannot read properties of undefined (reading 'length')

      at next (../../node_modules/.pnpm/[email protected]/node_modules/fastify/lib/hooks.js:334:32)
      at preParsingHookRunner (../../node_modules/.pnpm/[email protected]/node_modules/fastify/lib/hooks.js:364:3)
      at runPreParsing (../../node_modules/.pnpm/[email protected]/node_modules/fastify/lib/route.js:568:5)
      at Object.routeHandler [as handler] (../../node_modules/.pnpm/[email protected]/node_modules/fastify/lib/route.js:490:7)
      at Router.callHandler (../../node_modules/.pnpm/[email protected]/node_modules/find-my-way/index.js:552:14)
      at Router.lookup (../../node_modules/.pnpm/[email protected]/node_modules/find-my-way/index.js:530:17)
      at Server.preRouting (../../node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js:886:14)

错误2:

thrown: "Exceeded timeout of 5000 ms for a test.
    Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

      13 |   });
      14 |
    > 15 |   test('GET /api/auth/healthcheck', async () => {
         |   ^
      16 |     await supertest(app.server).get('/api/auth/healthcheck').expect(200);
      17 |   });
      18 | });

      at src/__tests__/server.e2e.test.ts:15:3
      at Object.<anonymous> (src/__tests__/server.e2e.test.ts:4:1)
typescript jestjs supertest fastify
1个回答
0
投票

await app.listen({ port: port });
之后
build
呢?

可以添加

  afterAll(async () => {
    app.close();
  }, 4000);

所以如果超过这个时间,它就会中止,而不等待承诺解决。

© www.soinside.com 2019 - 2024. All rights reserved.