首页 > 解决方案 > 在 NestJS GraphQL 上开玩笑打开句柄 RANDOMBYTESREQUEST

问题描述

我遇到以下错误时

jest --runInBand --detectOpenHandles
Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  RANDOMBYTESREQUEST

      3 | import { UserAuth } from 'src/auth/dto/user.jwt';
      4 | 
    > 5 | export const CurrentUser = createParamDecorator(
        |                            ^
      6 |   (data: unknown, context: ExecutionContext): UserAuth => {
      7 |     const ctx = GqlExecutionContext.create(context);
      8 |     return ctx.getContext().req.user;

      at rng (node_modules/uuid/dist/rng.js:18:21)
      at Object.v4 (node_modules/uuid/dist/v4.js:17:63)
      at Object.createParamDecorator (node_modules/@nestjs/common/decorators/http/create-route-param-metadata.decorator.js:14:30)
      at Object.<anonymous> (src/common/guards/current-user.guard.ts:5:28)

根据我的调查,此错误来自内部 UUID 模块上的包加密 import { createParamDecorator} from '@nestjs/common';

所有测试都通过了,但 jest 没有退出,我的文件如下:

import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { UserAuth } from 'src/auth/dto/user.jwt';

export const CurrentUser = createParamDecorator(
  (data: unknown, context: ExecutionContext): UserAuth => {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req.user;
  }
);

和测试:

describe('AuthService', () => {
  let service: AuthService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        rootMongooseTestModule(),
        PassportModule.register({ defaultStrategy: 'jwt' }),
        CacheModule.register(),
        TwilioModule.forRoot({
          accountSid: process.env.ACCOUNT_SID,
          authToken: process.env.AUTH_TOKEN,
        }),
        UserModule,
      ],
      providers: [AuthService, AuthResolver, AuthConfig, CookieService, JwtStrategy],
      exports: [AuthService],
      controllers: [AuthController],
    }).compile();

    service = module.get<AuthService>(AuthService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  afterAll(async (done) => {
    await closeInMongoDConnection();
    done();
  });
});

我试图:

知道如何解决这个问题吗?

标签: javascriptnode.jsjestjsts-jest

解决方案


我的解决方案是将 AfterAll 更改为 afterEach


推荐阅读