javascript - 在 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();
});
});
我试图:
- 模拟 uuid.v4 但其他测试开始失败
- 模拟装饰器,但守卫装饰器失败
知道如何解决这个问题吗?
解决方案
我的解决方案是将 AfterAll 更改为 afterEach
推荐阅读
- c# - 使用 MVVMLight 时如何设置 Web API DependencyResolver
- sql - PL/pgSQL 函数在 pgAdmin 之外无法正确运行
- matlab - 符号分化
- debugging - 使用 Mocha 和 Atom 进行交互式调试
- python - 熊猫在午夜后一天添加
- rust - 在递归函数中使用 impl Trait
- excel - 如何替换记录中的 GUID,然后在 Excel“唯一”公式中只返回唯一的?
- django - Django Signal没有将用户添加到组
- ios - iOS 应用程序通过分页使用多个 JSON
- c++ - 错误:无法将“int&”类型的非常量左值引用绑定到“int”类型的右值