Notes icon indicating copy to clipboard operation
Notes copied to clipboard

配置jest让其window上存在ontouchstart

Open any86 opened this issue 6 years ago • 0 comments

如果你的代码里有一段是这样的:

// 是否支持touch事件
export const SUPPORT_TOUCH = ('ontouchstart' in window);

因为默认jest集成的jsdom的window上是没有ontouchstart属性的, 所以我们需要自己加一个:

// jest.config.js
module.exports = {
    ...
    globals: {
        ontouchstart: null
    }
};

现在在每个测试文件中, 都可以直接识别到ontouchstart了, 是不是很简单 🐳

any86 avatar Feb 03 '19 02:02 any86