【问题标题】:Jest open handle RANDOMBYTESREQUEST on NestJS GraphQL在 NestJS GraphQL 上开玩笑打开句柄 RANDOMBYTESREQUEST
【发布时间】:2021-07-01 15:36:13
【问题描述】:

当我遇到以下错误时

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();
  });
});

我已经尝试过:

  • 模拟 uuid.v4 但随后其他测试开始失败
  • 模拟装饰器,但保护装饰器失败

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

【问题讨论】:

    标签: javascript node.js jestjs ts-jest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-15
      • 2022-08-07
      • 1970-01-01
      • 2020-05-27
      • 2022-11-18
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多