Skip to content

Instantly share code, notes, and snippets.

@eoikonomou
Created February 7, 2020 14:29
Show Gist options
  • Save eoikonomou/cd2166933707bb35be2b082e4c605ab2 to your computer and use it in GitHub Desktop.
Save eoikonomou/cd2166933707bb35be2b082e4c605ab2 to your computer and use it in GitHub Desktop.
Mocking canvas for tests
//
// Mock Canvas / Context2D calls
//
function mockCanvas (window) {
window.HTMLCanvasElement.prototype.getContext = function () {
return {
fillRect: function() {},
clearRect: function(){},
getImageData: function(x, y, w, h) {
return {
data: new Array(w*h*4)
};
},
putImageData: function() {},
createImageData: function(){ return []},
setTransform: function(){},
drawImage: function(){},
save: function(){},
fillText: function(){},
restore: function(){},
beginPath: function(){},
moveTo: function(){},
lineTo: function(){},
closePath: function(){},
stroke: function(){},
translate: function(){},
scale: function(){},
rotate: function(){},
arc: function(){},
fill: function(){},
measureText: function(){
return { width: 0 };
},
transform: function(){},
rect: function(){},
clip: function(){},
};
}
window.HTMLCanvasElement.prototype.toDataURL = function () {
return "";
}
}
const document = jsdom.jsdom(undefined, {
virtualConsole: jsdom.createVirtualConsole().sendTo(console)
});
const window = document.defaultView;
mockCanvas(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment