question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose CanvasRenderingContext2D via the `canvas` package

See original GitHub issue

Clear and concise description of the problem

I tried to use vitest in a project where I extensively use pixi.js and therefore, the Canvas element. As far as I know, jsdom does not expose canvas functionality out of the box but it can be emulated by using the node-canvas package (https://github.com/jsdom/jsdom/issues/3042).

Suggested solution

What I tried was bootstraping the canvas package in the jsdom environment with the following code

try {
  const { Context2d } = await importModule("canvas");
  Object.defineProperty(global, "CanvasRenderingContext2D", {
    get() {
      return Context2d
    }
  })
} catch (e) { }

inserted somewhere at https://github.com/vitest-dev/vitest/blob/27d6bcb10d6c00ae678769c6f48422ffe9d7d142/packages/vitest/src/env/jsdom.ts#L28-L30

Not the nicest implementation, but I hope you get the idea.

Alternative

No response

Additional context

By the way: Thank’s for this awesome package. It was so easy to setup and works damn good!

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
regenrekcommented, Dec 28, 2021

@hetsch could maybe share some code how you solved it? Trying to test a konvajs project where I need canvas too. Can’t figure it out how to test it with jsdom and vitest.

I’m running into:

Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
2reactions
hetschcommented, Dec 23, 2021

Thank you, @antfu and @patak-dev. Was my bad. Putting the code in a setup file works perfectly fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CanvasRenderingContext2D - Web APIs | MDN
Chrome Edge CanvasRenderingContext2D Full support. Chrome1. Toggle history Full support. Edge12. T... arc Full support. Chrome1. Toggle history Full support. Edge12. T... arcTo Full support. Chrome1....
Read more >
React + Rust + Wasm: Canvas Rendering | by Nikhil Gupta
Let's go through the code step-by-step. Expose a function called `render` that takes the ID of Canvas element already added to the DOM;...
Read more >
canvas-sequencer - npm
Store, serialize, parse, and execute sequences of canvas context instructions.. Latest version: 3.1.0, last published: 7 months ago.
Read more >
How to fix getImageData() error The canvas has been tainted ...
Would it work for you to save a copy of the image to your server using PHP and ... edit-1: peeps pointed out...
Read more >
canvas | Yarn - Package Manager
Installation. $ npm install canvas. By default, binaries for macOS, Linux and Windows will be downloaded. If you want to build from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found