当前的实现
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); });});


