jest-canvas-mock icon indicating copy to clipboard operation
jest-canvas-mock copied to clipboard

Parameter 1 is not of type 'Path2D'

Open markov00 opened this issue 2 years ago • 2 comments

Hi, we are trying to replace the canvas npm dependency with just your mock to speedup our ci infrastructure. I've replaced the library and I'm getting the following error:

'CanvasRenderingContext2D': parameter 1 is not of type 'Path2D'

when something like this exists in my code:

ctx.stroke(new Path2D(line));

The problem seems caused by the fact that Path2D is available in my jest environment when running the test (Path2D doesn't probably need any special os-dependent treatment and is available in the jsdom) and you, correctly, don't mock if it is present: https://github.com/hustcc/jest-canvas-mock/blob/315d56b2dacf292f27fe84b690620957ddeadbdd/src/window.js#L47 The thing is that you check for a path2d being an instance of Your Path2D (and not the possible already available Path2D) https://github.com/hustcc/jest-canvas-mock/blob/315d56b2dacf292f27fe84b690620957ddeadbdd/src/classes/CanvasRenderingContext2D.js#L705-L710

markov00 avatar Aug 27 '21 09:08 markov00

Yes. This is quite the predicament. I will look at this today.

jtenner avatar Aug 27 '21 13:08 jtenner

@jtenner a similar issue happens also with the DOMMatrix as described here https://github.com/hustcc/jest-canvas-mock/issues/85 The canvas context getTransform returns a DOMMatrix that is an instance of the internal implementation, not the one coming from the environment if present on window.

markov00 avatar Sep 01 '21 10:09 markov00