Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this package be used to avoid ReferenceError: ImageData is not defined? #2003

Closed
lindapaiste opened this issue Mar 6, 2022 · 2 comments
Labels

Comments

@lindapaiste
Copy link

Issue or Feature

I am trying to run Jest tests on a code where the code itself (not the test) uses an if (variable instanceof ImageData) check. I included setupFiles: ["canvas"] in my jest.config.js file. When running tests, I get an error:

ReferenceError: ImageData is not defined

I know that this is because ImageData is a DOM construct and doesn't exist in the Node.js environment.

The canvas package includes its own implementation of ImageData and I'm wondering if that can be used as a polyfill. I don't get errors when using instanceof checks on HTMLCanvasElement and other DOM elements. Is my setup wrong, or is what I'm trying to do just not possible? And if it's not possible, is there a better way to check whether or not a variable is an ImageData object?

Your Environment

  • Version of node-canvas (output of npm list canvas or yarn list canvas):
  • Environment
    node v16.13.0 on Windows 10
@LinusU
Copy link
Collaborator

LinusU commented Mar 16, 2022

I would recommend this package:

https://github.com/node-gfx/image-data

My goal is to use this in node-canvas in the future, it has a lot of tests to make sure that it works exactly the same as in the browser.

Something like this should do:

global.ImageData = require('@canvas/image-data')

@atsikov
Copy link

atsikov commented Dec 17, 2022

Solution from this comment didn't work me as putImageData threw likely because of a mismatching class.
However turned out everything could be fixed simply adding ImageData from canvas package into global scope

const canvas = require('canvas')

global.ImageData = canvas.ImageData

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants