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

Error with jest-canvas-mock and jest.resetAllMocks() #103

Closed
qiuhanWEI opened this issue Oct 20, 2022 · 7 comments
Closed

Error with jest-canvas-mock and jest.resetAllMocks() #103

qiuhanWEI opened this issue Oct 20, 2022 · 7 comments

Comments

@qiuhanWEI
Copy link

Hi, I'm using jest.resetAllMocks() in afterEach like this:

afterEach(() => {
  jest.resetAllMocks();
});

and then, the first test case is successful but the following tests fail, because context is undefined:

const canvas: HTMLCanvasElement = document.createElement('canvas');
const context: CanvasRenderingContext2D | null = canvas.getContext('2d');

image

I have to hack like this:

import canvasMock from 'jest-canvas-mock/lib/mock/prototype';

afterEach(() => {
  jest.resetAllMocks();
});

beforeEach(() => {
  canvasMock();
});

Then all the test cases can be successful. But i still get an ERROR:

Could not find a declaration file for module 'jest-canvas-mock/lib/mock/prototype'

image

  • So My question is:

Is there a better way for us to use jest-canvas-mock with jest.resetAllMocks() ?

Thanks!

@ducna-lts
Copy link

Have you found any solution yet, I have the same problem, don't know how to setup mock before start.

@qiuhanWEI
Copy link
Author

Have you found any solution yet, I have the same problem, don't know how to setup mock before start.

Not yet. @ducna-lts
I have to use the hack way import canvasMock from 'jest-canvas-mock/lib/mock/prototype';
and invoke canvasMock in beforeEach.

@akphi
Copy link

akphi commented Feb 16, 2023

Another way is to create a setup file

// mockCanvas.js
import canvasMock from 'jest-canvas-mock/lib/mock/prototype.js';

beforeEach(() => {
  canvasMock();
});

and add that as a setup file in Jest config

setupFilesAfterEnv: [
     'mockCanvas.js'
    ],

@pbomb
Copy link

pbomb commented Mar 14, 2023

@hustcc @nicks I'm running into this issue as well and see that it seems to have been resolved by #98 several months back. Any chance y'all could make another release to get this fix out? It's even more needed since Jest changed the behavior of the restoreMocks configuration that now removes the implementation of jest.fn() mocks after each test.

@pbomb
Copy link

pbomb commented Mar 20, 2023

@hustcc @nicks bumping to see if it might be possible to publish another version of this library. Thanks!

@nicks
Copy link
Contributor

nicks commented Mar 20, 2023

@pbomb i don't have any special publishing powers on this repo. if it helps, my team used yarn patch to downstream it into our repo without waiting on a release.

@hustcc
Copy link
Owner

hustcc commented Mar 23, 2023

@hustcc hustcc closed this as completed Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants