any86 / Notes

:rocket: 笔记
https://github.com/any86/Notes/issues
28 stars 9 forks source link

配置jest让其window上存在ontouchstart #11

Open any86 opened 5 years ago

any86 commented 5 years ago

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

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

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

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

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