Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-canvas-mock

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-canvas-mock

Mock a canvas in your jest tests.

  • 2.5.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8M
increased by1.75%
Maintainers
2
Weekly downloads
 
Created

What is jest-canvas-mock?

The jest-canvas-mock npm package is a mock for the HTML Canvas API, designed to be used with the Jest testing framework. It allows developers to write tests for canvas operations without the need for a browser environment. This is particularly useful for unit testing canvas-related code in Node.js environments or in continuous integration systems where a DOM may not be available.

What are jest-canvas-mock's main functionalities?

Mocking Canvas Context

This feature allows you to mock the 2D context of a canvas element, enabling you to test canvas drawing methods like fillRect.

import 'jest-canvas-mock';
test('mock canvas context', () => {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  ctx.fillRect(0, 0, 100, 100);
  expect(ctx.fillRect).toHaveBeenCalledWith(0, 0, 100, 100);
});

Mocking Canvas Image Data

This feature allows you to mock the creation of image data, which is useful for testing image manipulation on the canvas.

import 'jest-canvas-mock';
test('mock canvas image data', () => {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  const imageData = ctx.createImageData(100, 100);
  expect(imageData.data.length).toBe(40000);
});

Mocking Canvas Path Operations

This feature allows you to mock canvas path operations like beginPath, moveTo, lineTo, and stroke, which is useful for testing drawing paths on the canvas.

import 'jest-canvas-mock';
test('mock canvas path operations', () => {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  ctx.beginPath();
  ctx.moveTo(50, 50);
  ctx.lineTo(100, 100);
  ctx.stroke();
  expect(ctx.beginPath).toHaveBeenCalled();
  expect(ctx.moveTo).toHaveBeenCalledWith(50, 50);
  expect(ctx.lineTo).toHaveBeenCalledWith(100, 100);
  expect(ctx.stroke).toHaveBeenCalled();
});

Other packages similar to jest-canvas-mock

Keywords

FAQs

Package last updated on 18 May 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc