NestJs 测试在所有测试中创建应用程序的单个实例

标签 nestjs

我遇到了问题查询数据库时出错:数据库错误:致命:抱歉,已经有太多客户端,我确信这是因为正在为每个实例实例化应用程序的新实例测试套件。我尝试将应用程序创建分解为一个帮助文件,该文件如下所示

import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from '../../src/app.module';
import { PrismaService } from '../../src/prisma.service';

declare global {
  var app: INestApplication | undefined;
}

export const getApp = async () => {
  if (global.app) {
    return global.app;
  }

  const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
    providers: [PrismaService],
  }).compile();

  const app = moduleFixture.createNestApplication();
  await app.init();
  global.app = app;
  return app;
};

但这不起作用,当我添加控制台日志时,我可以看到该应用程序正在为每个测试套件实例化。

这就是我典型的 before 钩子(Hook)的样子

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

最佳答案

我遇到了同样的问题,并通过使用 Jest 的 globalSetup 解决了它和 globalTeardown选项。我创建应用程序的一个实例,并在运行测试之前为数据库播种,并在测试完成后销毁它并运行拆卸。我使用全局变量来引用应用程序实例。

在我的jest-e2e.json中:

{
    ...
    "globalSetup": "<rootDir>/test/test-setup.ts",
    "globalTeardown": "<rootDir>/test/test-teardown.ts"
}

test-setup.ts:

import * as fs from 'fs';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';

import { AppModule } from './../src/app.module';
import { WriteDataSource } from './../src/database/database.module';

module.exports = async () => {

    const moduleRef = await Test.createTestingModule({

        imports: [ AppModule ]

    })
    .compile();

    global.app = moduleRef.createNestApplication<NestFastifyApplication>(

        new FastifyAdapter()
        
    );
    
    await global.app.init();
    await global.app.getHttpAdapter().getInstance().ready();

    const seedQuery = fs.readFileSync(__dirname + '/scripts/database-seed.sql', { encoding: 'utf-8' });
    await WriteDataSource.manager.query(seedQuery);

};

test-teardown.ts:

import * as fs from 'fs';

import { WriteDataSource } from '../src/database/database.module';

module.exports = async () => {

    const teardownQuery = fs.readFileSync(__dirname + '/scripts/database-teardown.sql', { encoding: 'utf-8' }).replace(/\n/g, '');
    await WriteDataSource.query(teardownQuery);

    await WriteDataSource.destroy();

    await global.app.close();

};

关于NestJs 测试在所有测试中创建应用程序的单个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70781024/

相关文章:

nestjs - 使用 nestjs 和 multer 上传文件

javascript - 在 NestJS 微服务中公开普通的 http 端点

typescript - 为什么partialType 不使属性可以为空?

post - 如何在 NestJS 中使用 HttpModule 发布表单数据

nestjs - 如何使用适当的 API 文档使 Nestjs DTO 中必需的两个字段之一?

node.js - 使用 @AuthGuard、Passport 在 NestJS 中进行社交登录

sockets - NestJS多个WebSocketGateway

express - Vue + Express(NestJS) 不可能完成的任务,您正在使用 Vue 的仅运行时版本,其中

jestjs - 找不到 Nestjs 测试模块

Nestjs全局缓存: CacheInterceptor problem