New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock canvas #1782
Comments
In case anyone needs this, here's how I solved it:
|
Please don't do that. It will break in a future patch release. Instead, override the getContext() method, i.e. window.HTMLCanvasElement.prototype.getContext = ... |
Thanks @domenic ! |
@domenic I can't get that to work. My overridden method does not get called. I use jest and setup script file as follows:
Sorry for polluting global in node but this is a very old and big code base which I try to migrate to Jest. Would be very nice if mocking canvas (without using canvas package since not supported in Windows) was covered by official docs and not only as a comment in an issue. |
Why isn't canvas-prebuilt an option for you? That's what we are using in our project without any issue (Win, Mac and Linux, although everything has to be x64). |
In general we're not planning to add docs or help with one-off mocking issues. We've covered this in https://github.com/tmpvar/jsdom#intervening-before-parsing in general and any specific issues are going to be related to your specific codebase. |
Here's a simple way to mock canvas I came up with: //
// 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); |
Thanks a lot! The canvas prebuilt package seems to be working. |
Hello @cattermo, |
@micabe Maybe that specific function is not supported? |
It s working now after |
Maybe jest-canvas-mock can help. |
The solution is simple, just install |
That is still not working because of the way jsdom checks for the canvas modules at lib/jsdom/utils.js:
Doing require('canvas') returns an Object that has the Canvas function. It does not returns the Canvas function straight away. Or am I wrong on this issue? It worked well when I was using the canvas-prebuilt module, but it seems the canvas module has a different API. I am using canvas' latest version, 2.0.1. |
You can just do:
if actual implementation is not important for you |
I have the same problem. Looks like the issues is not with jsdom as the fix was merged a while ago in #1964 and available since version |
Without installing canvas, or canvas-prebuilt was able to get rid off errors related to HTMLCanvasElement, as well any others canvas methods nicely proposed by @endel as a hack to get around this problem. As @hustcc proposed, used jest-canvas-mock with finding quite clean solution. For technical details please take a look on this comment. |
I was seeing messages like "Cannot read webkitBackingStorePixelRatio of null", as well as " |
Got feedback. It is related to jest being stuck with older node versions. If you need support for latest node, look here: https://www.npmjs.com/package/jest-environment-jsdom-thirteen. This is jest specific though, so perhaps a little off-topic here. Sharing anyhow for posterity.. |
@grtjn - checked the jest-environment-jsdom-thirteen dependency, but in my case I still got these 2 exactly the same errors
Keeping my solution mentioned earlier, about Node version I wouldn't say 10.15 is a deprecated one, as I got the problems for such environment
|
FYI: I had to append |
Link to Cairo OS toolset dependency? Couldn't find that one. |
@danieldanielecki See main page of 'canvas' package: https://www.npmjs.com/package/canvas#compiling |
@grtjn thanks for it - maybe at some point will take an advantage of it. I was missing it, but looks more complicated than my solution so keeping the project as is. |
Sorry to necropost but @grtjn suggestion of installing jest-environment-jsdom-thirteen (I used fourteen just since it's more recent) fixed my issue. I tried using jest-canvas-mock but we did some weird stuff where we fake createImageBitmap using node-canvas when OffscreenCanvas is not available, which it is not in many browsers, and jest-canvas-mock got pretty confused by this. Maybe another day we'll get that working...it is a cool mock library. |
I got TypeError: Cannot assign to read only property 'getContext' of object '[object Object]' when trying to do this, did someone know how to fix it? |
Is it possible to mock canvas without having the actual implementation (
canvas
/canvas-prebuilt
)?I'd like to prevent this error from happening, as the canvas functionality is not really important for me:
The text was updated successfully, but these errors were encountered: