vitest-canvas-mock icon indicating copy to clipboard operation
vitest-canvas-mock copied to clipboard

ReferenceError: jest is not defined

Open ethan-bloom opened this issue 2 years ago • 21 comments

ReferenceError: jest is not defined ❯ node_modules/jest-canvas-mock/lib/classes/CanvasRenderingContext2D.js:121:19 ❯ new CanvasRenderingContext2D node_modules/jest-canvas-mock/lib/classes/CanvasRenderingContext2D.js:120:15 ❯ HTMLCanvasElement.getContext2d node_modules/jest-canvas-mock/lib/mock/prototype.js:27:19 ❯ HTMLCanvasElement.mockCall node_modules/@vitest/spy/dist/index.js:50:17 ❯ HTMLCanvasElement.getContext2d [as getContext] node_modules/tinyspy/dist/index.js:39:20 ❯ Object.platformApi.measureText node_modules/zrender/lib/core/platform.js:35:41 ❯ getWidth node_modules/zrender/lib/contain/text.js:16:51 ❯ Module.getLineHeight node_modules/zrender/lib/contain/text.js:68:12 ❯ Module.parsePlainText node_modules/zrender/lib/graphic/helper/parseText.js:90:54

ethan-bloom avatar Nov 22 '23 08:11 ethan-bloom

I'm seeing the same thing using vitest-canvas-mock 0.3.3, which looks like it itself is using jest-canvas-mock 2.5.2

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
ReferenceError: jest is not defined
⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
 ❯ node_modules/jest-canvas-mock/lib/mock/createImageBitmap.js node_modules/jest-canvas-mock/lib/mock/createImageBitmap.js:9:16
 ❯ __require2 node_modules/.vitest/deps/window-HHD6YH5J.js:10:50
 ❯ node_modules/jest-canvas-mock/lib/window.js node_modules/jest-canvas-mock/lib/window.js:16:49
 ❯ __require2 node_modules/.vitest/deps/window-HHD6YH5J.js:10:50
 ❯ node_modules/.vitest/deps/window-HHD6YH5J.js:3612:16
 ❯ ModuleJob.run node:internal/modules/esm/module_job:217:25
 ❯ ModuleLoader.import node:internal/modules/esm/loader:316:24
 ❯ importMockWindow node_modules/vitest-canvas-mock/dist/index.js:16:27

riccjohn avatar Nov 22 '23 15:11 riccjohn

I have noticed I am only encountering this issue when I enable lazyUpdate in the echarts library. I am unsure why this could be triggering this issue?

image image https://echarts.apache.org/en/api.html#echartsInstance.setOption

ethan-bloom avatar Nov 23 '23 08:11 ethan-bloom

hey

It may be asynchronous related issue. try:

  beforeEach(() => {
    vi.useFakeTimers({ shouldAdvanceTime: true });
  });

mute--- avatar Nov 29 '23 15:11 mute---

I'm seeing this too

gdams avatar Dec 05 '23 20:12 gdams

I'm seeing the same thing using vitest-canvas-mock 0.3.3, which looks like it itself is using jest-canvas-mock 2.5.2

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
ReferenceError: jest is not defined
⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
 ❯ node_modules/jest-canvas-mock/lib/mock/createImageBitmap.js node_modules/jest-canvas-mock/lib/mock/createImageBitmap.js:9:16
 ❯ __require2 node_modules/.vitest/deps/window-HHD6YH5J.js:10:50
 ❯ node_modules/jest-canvas-mock/lib/window.js node_modules/jest-canvas-mock/lib/window.js:16:49
 ❯ __require2 node_modules/.vitest/deps/window-HHD6YH5J.js:10:50
 ❯ node_modules/.vitest/deps/window-HHD6YH5J.js:3612:16
 ❯ ModuleJob.run node:internal/modules/esm/module_job:217:25
 ❯ ModuleLoader.import node:internal/modules/esm/loader:316:24
 ❯ importMockWindow node_modules/vitest-canvas-mock/dist/index.js:16:27

Like @riccjohn, I also see this ReferenceError: jest is not defined error with an initial stack trace entry of

Object.<anonymous> node_modules/jest-canvas-mock/lib/mock/createImageBitmap.js:9:16

I am using vitest-canvas-mock v0.3.3. In my case, this is a somewhat uncommon error that seems to occur at random (I have not been able to consistently reproduce it).

cooperwalbrun avatar Dec 19 '23 03:12 cooperwalbrun

Have you tried advancing timers?

mute--- avatar Dec 19 '23 11:12 mute---

Have you tried advancing timers?

I can do this, however since the error is very infrequent/inconsistent for me, I am not sure how I will know for certain that advancing timers fixes the issue. I will try it nonetheless.

cooperwalbrun avatar Dec 19 '23 22:12 cooperwalbrun

hey

It may be asynchronous related issue. try:

  beforeEach(() => {
    vi.useFakeTimers({ shouldAdvanceTime: true });
  });

@mute--- where would I put this? In my vitest-setup.ts file?

gdams avatar Dec 20 '23 09:12 gdams

whereever you find it appropriate place :) i'd put this in test file where i use timers. don't forget to actually advance time by vitest.advanceTimersByTime(sometime) after canvas dependent component is rendered

mute--- avatar Dec 20 '23 10:12 mute---

Have you tried advancing timers?

I can do this, however since the error is very infrequent/inconsistent for me, I am not sure how I will know for certain that advancing timers fixes the issue. I will try it nonetheless.

Update: even after adding vi.useFakeTimers({ shouldAdvanceTime: true }); and vi.advanceTimersByTime(250); before each unit test involving a component that renders a canvas, I am still seeing the ReferenceError: jest is not defined error with the same createImageBitmap.js:9:16 stack trace.

cooperwalbrun avatar Jan 03 '24 01:01 cooperwalbrun

Adding this line in my test setup file seems to have fixed it for me

vi.stubGlobal("jest", vi);

vitest-canvas-mock does something similar but for some reason it seems to not take effect

davidnaas avatar Jan 16 '24 13:01 davidnaas

Update: even after adding vi.useFakeTimers({ shouldAdvanceTime: true }); and vi.advanceTimersByTime(250); before each unit test involving a component that renders a canvas, I am still seeing the ReferenceError: jest is not defined error with the same createImageBitmap.js:9:16 stack trace.

you should advance timer after component render

vitest-canvas-mock does something similar but for some reason it seems to not take effect

vitest-canvas-mock clears the reference in afterAll callback and rendering fails because of its async nature (due to RAF)

mute--- avatar Jan 16 '24 13:01 mute---

Update: even after adding vi.useFakeTimers({ shouldAdvanceTime: true }); and vi.advanceTimersByTime(250); before each unit test involving a component that renders a canvas, I am still seeing the ReferenceError: jest is not defined error with the same createImageBitmap.js:9:16 stack trace.

you should advance timer after component render

vitest-canvas-mock does something similar but for some reason it seems to not take effect

vitest-canvas-mock clears the reference in afterAll callback and rendering fails because of its async nature (due to RAF)

Adding vi.advanceTimersByTime(250) somewhat arbitrarily in tests doesn't seem like a satisfactory fix to me. In my case it's not so clearcut which tests are actually causing this in the first place so there would be a lot of trial and error just sprinkling this line all over the place. This difficulty is also compounded by the fact that the tests have to be run in singleThread mode, i.e without isolation, for the canvas mock to work at all. It makes it very messy to figure out how and when all setup and teardown functions are run

davidnaas avatar Jan 16 '24 16:01 davidnaas

you should advance timer after component render

My apologies, I did use advanceTimersByTime after the component was rendered, not before. I just made a mistake when I was updating my previous comment the other day.

On a separate note, I am going to try using @davidnaas's workaround and see if that yields better results.

cooperwalbrun avatar Jan 17 '24 00:01 cooperwalbrun

Is there a better solution?

Davont avatar Jan 25 '24 12:01 Davont

A workaround is for users to patch the jest-canvas-mock dependency and change jest to vi. There is only one instance at present. This can be done with patch-package for npm users, or alternatively via yarn/pnpm patch.

brentertz avatar Feb 09 '24 19:02 brentertz

I'm having this issue as well. I'll audit for timers and report back.

I presumed it was related to the deprecation of deps.inline in recent versions of vite. As I now have to put it in server.deps.inline. I suppose that is not the case, which is somewhat of a relief.

There's no compat issues if we have proper peer deps right?


Update, added shouldAdvanceTime: true to my one call to vi.useFakeTimers. So far I've passed 2/2 times. Need to rerun a few more times to know for sure that this is reliable but it works so far.

johnnyshankman avatar Feb 13 '24 18:02 johnnyshankman

I tried using vi.useFakeTimers({ shouldAdvanceTime: true }) and vi.advanceTimersByTime(250), and successfully solved some of the test cases. However, there are still some test cases that have not passed.

lumixraku avatar May 20 '24 07:05 lumixraku

For me it worked perfectly when run locally, but it caused flaky behaviour on CI (GH Actions). All the workarounds posted here didn't help me.

What helped me was to drop vitest-canvas-mock entirely and use vi-canvas-mock instead. 🙈

They seem to have 0 dependencies to jest 🎉

pixelmanya avatar Dec 04 '24 13:12 pixelmanya

For me it worked perfectly when run locally, but it caused flaky behaviour on CI (GH Actions). All the workarounds posted here didn't help me.

What helped me was to drop vitest-canvas-mock entirely and use vi-canvas-mock instead. 🙈

They seem to have 0 dependencies to jest 🎉

Vitest methods have to be available globally or it won't work:

In your Vite configuration file:

export default defineConfig({
  // ...
  test: {
    // ...
    globals: true,
  },
})

mtancoigne avatar Dec 06 '24 07:12 mtancoigne