简体   繁体   中英

How to mock the entire AbortController class using jest

I'm using Jest and AbortController at the same time. AbortController has a signal property which returns an AbortSignal , which itself has a reason property. 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"'."

I cannot use jest.mock() because AbortController isn't imported.

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 :

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

The tests weren't compiling because of a typing problem. Jest was using @types/node . This solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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