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't use jest-canvas-mock without jest #51

Open
jtenner opened this issue Oct 31, 2019 · 10 comments
Open

Can't use jest-canvas-mock without jest #51

jtenner opened this issue Oct 31, 2019 · 10 comments

Comments

@jtenner
Copy link
Collaborator

jtenner commented Oct 31, 2019

Example setup needed to bootstrap jest manually that can be a pain point:

const jest = {
  fn: (arg) => arg,
};
global.jest = jest;
const CanvasRenderingContext2D = require("jest-canvas-mock/lib/classes/CanvasRenderingContext2D").default;
const Path2D = require("jest-canvas-mock/lib/classes/Path2D").default;
global.Path2D = Path2D;

@hustcc is this something worth supporting?

@jtenner jtenner self-assigned this Oct 31, 2019
@hustcc
Copy link
Owner

hustcc commented Nov 3, 2019

I can not catch your problem, can give me a test case?

@hustcc
Copy link
Owner

hustcc commented Nov 3, 2019

if (!win.Path2D) win.Path2D = Path2D;

Is it because Path2D exists in jsdom? so mock failed?

@jtenner
Copy link
Collaborator Author

jtenner commented Nov 3, 2019

Yeah. I recently had to bootstrap this library without using jest so I could test a few wasm simd functions in my canvas library. I want to think on this before I make any suggestions.

@jtenner
Copy link
Collaborator Author

jtenner commented Nov 20, 2019

I believe this can be fixed by checking to see if jest exists in the global scope. If it doesn't we can just polyfill jest.fn to be an identity function.

Another really easy fix might also be to just import the Path2D class directly instead of using the global version.

I find that using new CanvasRenderingContext2D(width, height) is clunky for testing, but is very useful. Please see https://github.com/as2d/as2d as an example (which also uses jest, but manually bootstraps the canvas in the case of SIMD testing.)

@jtenner
Copy link
Collaborator Author

jtenner commented Apr 8, 2020

I will likely start to tackle this soon. I feel like people should be able to import this module without having to do any setup.

This means testing the module without jest in testing, and will require another test step.

@jtenner
Copy link
Collaborator Author

jtenner commented Dec 2, 2020

I believe this is still worth using. Perhaps the core functionality of emulating the CanvasRenderingContext2D class can be in it's own package?

@hustcc
Copy link
Owner

hustcc commented Feb 10, 2021

I believe this is still worth using. Perhaps the core functionality of emulating the CanvasRenderingContext2D class can be in it's own package?

Change this repo to mono repo?

@jtenner
Copy link
Collaborator Author

jtenner commented Feb 10, 2021

I don't entirely know the right answer. It would be nice for someone else to pick up the responsibility, because I'm only good at the canvas part. :)

@Confidenceman02
Copy link

Confidenceman02 commented Jul 30, 2021

You can get around this by importing the script inside of a setup module.

// jest.config.js

module.exports = {
  setupFilesAfterEnv: ["./jest.setup.js"],
  // other jest config
}

// jest.setup.js

require("jest-canvas-mock")

\\ jest setup stuff

Essentially the setup file works like a before block, so it's already in a jest context.

This should remove the jest global error.

@iainbeeston
Copy link

I'm having similar issues but for a totally separate reason. I like to use jest with the injectGlobals config variable set, so anything from jest has to be explicitly imported (rather than letting jest pollute the global namespace). However, I cannot do that with jest-canvas-mock because it assumes the jest global will be available everywhere. In my case, explicitly importing jest where it's used would solve the issue and make it compatible with injectGlobals.

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

No branches or pull requests

4 participants