typescript - 如何在 Typescript 中重新定义 window.addEventListener 的类型?

标签 typescript jestjs

出于测试目的,我必须模拟 window.addEventListener (开 Jest ,空模拟足以进行 spy 事件)。所以我这样做是这样的:

window.addEventListener = jest.fn();
window.removeEventListener = jest.fn();

问题是,当我执行 window.addEventListener.mockClear() 时, typescript 提示该方法不存在。

enter image description here

解决这个问题的最佳方法是什么?

解决方法:目前我正在这样做,但我不喜欢一直这样做

(window.addEventListener as jest.Mock<{}>).mockClear();
(window.addEventListener as jest.Mock<{}>).mockClear();

最佳答案

我认为不可能扩充现有接口(interface)来向该接口(interface)中声明的方法添加一些属性。

但是您可以重写这些方法。有一种方法可以避免一直编写类型转换:您可以定义自己的 MockWindow 接口(interface),并根据需要键入 addEventListenerremoveEventListener 。然后,您可以将真实的 window 对象强制转换为 MockWindow 一次,然后使用它:

interface MockWindow extends Window {
    addEventListener: jest.Mock<{}> & typeof window.addEventListener;
    removeEventListener: jest.Mock<{}> & typeof window.removeEventListener;
}

function mockWindow() {
    window.addEventListener = jest.fn();
    window.removeEventListener = jest.fn();
    return window as MockWindow;
}

let w = mockWindow();


w.addEventListener('load',function() {});
w.addEventListener.mockClear();  

实际上,因为在当前版本的 jest Typings 中,jest.Mock 接口(interface)已经具有非常宽松的调用签名

interface Mock<T> extends Function {
    ...
    (...args: any[]): any;

任何声明为jest.Mock的东西都可以使用任意数量的任意参数来调用,并且它使得将& typeof window.addEventListener添加到addEventListener的类型中 不必要。

更新:事实证明 typescript 具有足够的表达能力,因此您可以编写通用的、类型检查的函数:

function mockMethods<T, MethodName extends keyof T>(o: T, methodNames:MethodName[])
: T & {[K in MethodName]: T[K] & jest.Mock<{}>} {
    methodNames.forEach(k => {
       o[k] = jest.fn() as any;
    });
    return o as any; // typescript doesn't allow this cast: 
                     // as (T & {[K in MethodKeys]: T[K] & jest.Mock<{}>});
}

let w = mockMethods(window, ['addEventListener', 'removeEventListener']);

w.addEventListener('load', function() {});
w.addEventListener.mockClear();

// you can still call other window methods
w.alert('x');

// and their types are unaffected
w.alert.mockClear(); // error: Property 'mockClear' does not exist 
                     // on type '(message?: any) => void'.

// and you can't mock non-existent methods
let u = mockMethods(window, ['z']); // error: Argument of type '"z"[]'  
                     //  is not assignable to parameter of type '("blur" | ...

关于typescript - 如何在 Typescript 中重新定义 window.addEventListener 的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784380/

相关文章:

javascript - 重置包装器的 ESLint 友好方式

typescript - 使用 TypeScript 的 should.js 问题

html - 如何检测用户何时停止滚动 - Angular 5

typescript - 如何在类型注释中使用函数返回的类?

javascript - 使用 Webpack 与主模块一起构建独立模块

Angular 8 和 jest - 找不到文件 : jest-preset-angular/InlineHtmlStripStylesTransformer. js

typescript - Jest Mock + Typescript 错误 TS2339

c# - Swagger 可以生成自定义泛型类型吗?

javascript - 访问另一个函数中的 jest.fn 函数

javascript - 开 Jest : Simulate that a string property doesn't exist