
Security News
Meet the Socket Team at RSAC and BSidesSF 2025
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
vitest-canvas-mock
Advanced tools
The vitest-canvas-mock npm package is a mock implementation of the HTMLCanvasElement for use with the Vitest testing framework. It allows developers to test canvas-related code without needing a real browser environment.
Mocking Canvas Methods
This feature allows you to mock canvas methods such as getContext, fillRect, and toDataURL. This is useful for testing drawing operations without needing a real canvas element.
const { createCanvas } = require('vitest-canvas-mock');
const canvas = createCanvas(200, 200);
const ctx = canvas.getContext('2d');
ctx.fillRect(50, 50, 100, 100);
console.log(canvas.toDataURL());
Mocking Canvas Properties
This feature allows you to mock canvas properties like width and height. This is useful for testing how your code interacts with canvas dimensions.
const { createCanvas } = require('vitest-canvas-mock');
const canvas = createCanvas(200, 200);
canvas.width = 300;
canvas.height = 300;
console.log(canvas.width, canvas.height);
Mocking Image Loading
This feature allows you to mock image loading, which is useful for testing code that relies on images being loaded into the canvas.
const { createImage } = require('vitest-canvas-mock');
const img = createImage();
img.src = 'data:image/png;base64,...';
img.onload = () => {
console.log('Image loaded');
};
jest-canvas-mock is a similar package designed for use with the Jest testing framework. It provides a mock implementation of the HTMLCanvasElement and its methods. Compared to vitest-canvas-mock, jest-canvas-mock is tailored for Jest users, while vitest-canvas-mock is specifically designed for Vitest.
canvas-mock is a general-purpose canvas mocking library that can be used with various testing frameworks. It provides a comprehensive mock of the HTMLCanvasElement and its methods. While it is more versatile in terms of framework compatibility, it may require more setup compared to vitest-canvas-mock, which is optimized for Vitest.
Mock canvas
when running unit tests with vitest. Uses jest-canvas-mock.
npm install vitest-canvas-mock -D
vitest-canvas-mock
or add it to an existing setup file.// vitest.setup.ts
import 'vitest-canvas-mock'
vitest.config.ts
file, create a setupFiles
array and add that file:import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
setupFiles: ['./vitest.setup.ts'],
environment: 'jsdom',
deps: {
inline: ['vitest-canvas-mock'],
},
// For this config, check https://github.com/vitest-dev/vitest/issues/740
threads: false,
environmentOptions: {
jsdom: {
resources: 'usable',
},
},
},
})
View mock strategy doc here.
View snapshots doc here
MIT
FAQs
🌗 A module used to mock canvas in Vitest.
The npm package vitest-canvas-mock receives a total of 213,789 weekly downloads. As such, vitest-canvas-mock popularity was classified as popular.
We found that vitest-canvas-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
Security News
Biome's v2.0 beta introduces custom plugins, domain-specific linting, and type-aware rules while laying groundwork for HTML support and embedded language features in 2025.
Security News
Next.js has patched a critical vulnerability (CVE-2025-29927) that allowed attackers to bypass middleware-based authorization checks in self-hosted apps.