canvaswhen run unit test cases with jest. For more browser environment, you can use jest-electron for real browser runtime.
This should only be installed as a development dependency (
devDependencies) as it is only designed for testing.
npm i --save-dev jest-canvas-mock
package.json under the
jest, create a
setupFiles array and add
jest-canvas-mock to the array.
If you already have a
setupFiles attribute you can also append
jest-canvas-mock to the array.
More about in configuration section.
Alternatively you can create a new setup file which then requires this module or
require statement to an existing setup file.
Add that file to your
This mock strategy implements all the canvas functions and actually verifies the parameters. If a
known condition would cause the browser to throw a
TypeError or a
DOMException, it emulates the
error. For instance, the
CanvasRenderingContext2D#arc function will throw a
TypeError if the
radius is negative, or if it was not provided with enough parameters.
// arc throws a TypeError when the argument length is less than 5expectctx.arc1, 2, 3, 4.toThrowTypeError;// when radius is negative, arc throws a dom exception when all parameters are finiteexpectctx.arc0, 0, -10, 0, Math.PI * 2.toThrowDOMException;
The function will do
Number type coercion and verify the inputs exactly like the browser does. So
this is valid input.
expectctx.arc"10", "10", "20", "0", "6.14".not.toThrow;
Another part of the strategy is to validate input types. When using the
CanvasRenderingContext2D#fill function, if you pass it an invalid
fillRule it will throw a
TypeError just like the browser does.
expectctx.fill"invalid!".toThrowTypeError;expectctx.fillnew Path2D, "invalid!".toThrowTypeError;
We try to follow the ECMAScript specification as closely as possible.
There are multiple ways to validate canvas state. There are currently three
static methods attached
CanvasRenderingContext2D class. The first way to use this feature is by using the
/*** In order to see which functions and properties were used for the test, you can use `__getEvents`* to gather this information.*/;expectevents.toMatchSnapshot; // jest will assert the events match the snapshot
The second way is to inspect the current path associated with the context.
ctx.beginPath;ctx.arc1, 2, 3, 4, 5;ctx.moveTo6, 7;ctx.rect6, 7, 8, 9;ctx.closePath;/*** Any method that modifies the current path (and subpath) will be pushed to an event array. When* using the `__getPath` method, that array will sliced and usable for snapshots.*/;expectpath.toMatchSnapshot;
The third way is to inspect all of the success draw calls submitted to the context.
ctx.drawImageimg, 0, 0;/*** Every drawImage, fill, stroke, fillText, or strokeText function call will be logged in an event* array. This method will return those events here for inspection.*/;expectcalls.toMatchSnapshot;
In some cases it may be useful to clear the events or draw calls that have already been logged.
// Clear eventsctx.__clearEvents;// Clear draw callsctx.__clearDrawCalls;
Override default mock return value
You can override the default mock return value in your test to suit your need. For example, to override return value of