栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Jest测试asnyc代码(或使用jsdom测试“ image.onload”)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Jest测试asnyc代码(或使用jsdom测试“ image.onload”)

当前的实现

checkSize
是异步的,并且始终返回
undefined

您应该使用回调或返回

Promise

function checkSizeWithCallback(src, width, height, callback) {  const image = new Image();  image.onload = evt => {    const result = evt.target.width >= width && evt.target.height >= height;    callback(null, result);  };  image.onerror = // TODO: handle onerror  image.src = src; }it('...', done => {  checkSizeWithCallback(, (err, result) => {    expect(result).toEqual(true);    done(err);  });});

function checkSizeWithPromise(src, width, height) {  return new Promise((resolve, reject) => {    const image = new Image();    image.onload = evt => {      const result = evt.target.width >= width && evt.target.height >= height;      resolve(result);    };    image.onerror = // TODO: handle onerror    imageObj.src = src;   });}it('...', () => {  return checkSizeWithPromise()    .then(result => {      expect(result).toEqual(true);  });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633566.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号