无论我设置什么时间,测试总是超过超时(NestJS、Jest、TypeORM)

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

我的测试总是抛出错误“超出了钩子的超时(我正在使用的超时值)毫秒”。我已经尝试使用 jest 函数“jest.setTimeout()”将超时值增加到 10000、20000 甚至 100000,但它一直抛出此错误。

这是错误的打印:

city.e2e-spec.ts

  import * as request from 'supertest';
  import { Test } from '@nestjs/testing';
  import { INestApplication } from '@nestjs/common';
  import { CityModule } from '@app/modules/public/city';
  import { City } from '@app/entity/city/city.entity';
  import { createConnections, getRepository } from 'typeorm';
  import { getRepositoryToken } from '@nestjs/typeorm';

  jest.useFakeTimers();
  jest.setTimeout(10000);

  describe('City E2E', () => {
    let app: INestApplication;

    beforeAll(async () => {
      await createConnections();

      const moduleRef = await Test.createTestingModule({
        imports: [CityModule],
        providers: [{ provide: getRepositoryToken(City), useValue: getRepository(City) }]
      }).compile();

      app = moduleRef.createNestApplication();
      app.setGlobalPrefix('v1/public');

      await app.init();
    });

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

    it('[GET] /cities', () => {
      return request(app.getHttpServer())
        .get('/cities')
        .expect(200)
        .expect(['array with cities...']);
    });
  });
jestjs nestjs typeorm
2个回答
0
投票

您的问题可能是由数据库连接引起的。

await createConnections();


0
投票

我最近遇到了同样的问题,我刚刚发现这是由于 jest 配置文件中的设置造成的。 你能分享一下你的 Jest 设置吗?

如果全局启用

fakeTimers
选项,这会扰乱设置数据库模块(在我的例子中为 TypeORM)的过程。尝试将
doNotFake: ['nextTick', 'setImmediate']
添加到
fakeTimers

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