程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中?

开发过程中遇到“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中的问题如何解决?下面主要结合日常开发的经验,给出你关于“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中的解决方法建议,希望对你解决“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中有所启发或帮助;

我想测试我的 WebSocket,但我遇到的问题是我的测试出错并且测试过程一直在运行并且没有结束。 (错误见图片)。

“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中

已经导致错误的代码:

describe("Event Gateway (E2E)",() => {
    let app: InestApplication;

    beforeAll(async () => {
        app = await globalBeforeAll([Gatewaymodule]);
    });

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

    describe("Connection to WebSocket",() => {
        it("should be successfully",() => {
            // here should come some expectations ...
        });
    });
});

globalBeforeAll 只是一个帮助方法,使测试更具可读性。这包含了守卫的覆盖以及另外的 useWebSocketAdapter(new redisIoAdapter(application))

redisIoAdapter 稍后将成为它自己的适配器,然后我可以使用它通过 redis 制作 PubSub。出于测试目的,它目前看起来非常基本,如下所示:

import { ServerOptions } from "socket.io";
import { IoAdapter } from "@nestJs/platform-socket.io";

export class redisIoAdapter extends IoAdapter {
    createIOServer(port: number,options?: ServerOptions): any {
        return super.createIOServer(port,options);
    }
}

我做错了什么?

解决方法

问题确实出在 globalBeforeAll 中。以前,该方法如下所示:

export async function globalBeforeAll(additionalModules: Array<{ new (): any }>): Promise<InestApplication> {
    const app = await Test.createTesTingModule(ModuleMetadataBuilder.build(additionalModules,truE))
        .overrideGuard(JwtAuthGuard)
        .useClass(OverridesAuthGuard)
        .overrideGuard(RolesAuthGuard)
        .useClass(OverridesRolesGuard)
        .compile()
        .then((application) => application
            .createnestApplication()
            .useWebSocketAdapter(new redisIoAdapter(app))
            .init(),);

    app.enableShutdownHooks();
    Authorizer.init(app);

    return app;
}

我不得不按如下方式更改它,现在它可以工作了:

export async function globalBeforeAll(additionalModules: Array<{ new (): any }>): Promise<InestApplication> {
    const app = await Test.createTesTingModule(ModuleMetadataBuilder.build(additionalModules,truE))
        .overrideGuard(JwtAuthGuard)
        .useClass(OverridesAuthGuard)
        .overrideGuard(RolesAuthGuard)
        .useClass(OverridesRolesGuard)
        .compile()
        .then((application) => application.createnestApplication());

    app.useWebSocketAdapter(new redisIoAdapter(app));
    app.enableShutdownHooks();
    Authorizer.init(app);

    return app.init();
}

大佬总结

以上是大佬教程为你收集整理的“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中全部内容,希望文章能够帮你解决“类型错误:无法读取未定义的属性‘关闭’”在 E2E 测试中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:测试中