简体   繁体   English

如何使用 jest 模拟整个 AbortController 类

[英]How to mock the entire AbortController class using jest

I'm using Jest and AbortController at the same time.我同时使用JestAbortController AbortController has a signal property which returns an AbortSignal , which itself has a reason property. AbortController有一个signal属性,它返回一个AbortSignal ,它本身有一个reason属性。 However, the reason property isn't supported in node yet, thus all of my test are failing due to this.但是,节点中尚不支持原因属性,因此我的所有测试都因此而失败。

I've tried我试过了

jest.spyOn(AbortController.prototype, 'AbortController').mockImplementation(...)

to mock the constructor but I still got the following error :模拟构造函数,但我仍然收到以下错误:

"Argument of type '"AbortController"' is not assignable to parameter of type '"abort"'." “'"AbortController"' 类型的参数不能分配给 '"abort"' 类型的参数。"

I cannot use jest.mock() because AbortController isn't imported.我不能使用 jest.mock() 因为 AbortController 没有被导入。

I don't really know what to do from now on, has anyone ever tried this ?从现在开始我真的不知道该怎么办了,有人试过吗?

I solved this problem by adding these lines to my jest configuration, inside the package.json :我通过在 package.json 中将这些行添加到我的 jest 配置中解决了这个问题:

"jest": {
        [...yourConfig],
        "globals": {
            "ts-jest": {
                "isolatedModules": true
            }
        },
}

The tests weren't compiling because of a typing problem.由于打字问题,测试没有编译。 Jest was using @types/node . Jest 正在使用@types/node This solved the problem.这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM