-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Doesn't work with latest Create React App #72
Comments
Same here. It worked with |
Update: As far as I can tell, Jest / jsdom can handle |
My restriction is I cannot do this, hence using this package |
@albert-schilling Do you have any resource how to make it working?
Thanks! |
@mregula |
Thanks @albert-schilling ! What I did:
|
I'm running in to the same problem here. I follow the steps outlines above by @ztolley and I also update the file Afterwards I follow the suggestion of @albert-schilling installing node canvas and the test passed !, jsdom indeed was able to instantiate a canvas object. However it is my understanding that node canvas is less portable then |
Hey! Following up on that, the node For example:
|
Actually, the solution is to remove all dependencies to |
The lib |
I'm having this same problem. While using |
Using
|
- Changed konva import path - Fix target type for setHoveredStyle from Node to Node | Group - Fix imports - Some issue with jest-canvas-mock (hustcc/jest-canvas-mock#72) so replaced it with canvas
* Bump konva from 7.2.5 to 8.2.3 Bumps [konva](https://github.com/konvajs/konva) from 7.2.5 to 8.2.3. - [Release notes](https://github.com/konvajs/konva/releases) - [Changelog](https://github.com/konvajs/konva/blob/master/CHANGELOG.md) - [Commits](konvajs/konva@7.2.5...8.2.3) --- updated-dependencies: - dependency-name: konva dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * - Removed konva-node since "merged into konva" in 8.0.0 - Changed konva import path - Fix target type for setHoveredStyle from Node to Node | Group - Fix imports - Some issue with jest-canvas-mock (hustcc/jest-canvas-mock#72) so replaced it with canvas * added missing Group type * Upgrade konva to latest version Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Martin Henz <[email protected]> Co-authored-by: En Rong <[email protected]>
Let me look into this, but I believe that this package doesn't work with the latest react-create-app is because of the fact that the jest configuration file has I was testing against a file that had
which is functionally equivalent to having resetMocks set to true. So try flipping the |
"jest": {
"resetMocks": false
} Now the tests do not fail anymore. Source code of my dummy component: // @ts-nocheck
import * as React from 'react';
import { createRef, Ref, useState } from 'react';
type CanvasProps = {
onUpdate?: (dataURL: string) => void,
}
const Canvas: React.FC<CanvasProps> = ({ onUpdate }) => {
const canvasRef: Ref<HTMLCanvasElement> = createRef();
const draw = () => {
// https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(75, 75, 50, 0, Math.PI * 2, true);
ctx.moveTo(110, 75);
ctx.arc(75, 75, 35, 0, Math.PI, false);
ctx.moveTo(65, 65);
ctx.arc(60, 65, 5, 0, Math.PI * 2, true);
ctx.moveTo(95, 65);
ctx.arc(90, 65, 5, 0, Math.PI * 2, true);
ctx.stroke();
onUpdate && onUpdate(canvas.toDataURL());
};
const clear = () => {
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
onUpdate && onUpdate(canvas.toDataURL());
};
return (
<>
<div>
<button onClick={draw}>Draw</button>
<button onClick={clear}>Clear</button>
</div>
<div>
<canvas data-testid='canvas' ref={canvasRef} width={150} height={150}
style={{ border: '1px solid #000' }} />
</div>
</>
);
};
export default Canvas; And the test: // @ts-nocheck
import * as React from 'react';
import 'jest-canvas-mock';
import Canvas from './Canvas';
import { getByTestId, getByText, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
describe('Canvas', () => {
it('renders empty canvas', async () => {
const { container } = render(<Canvas />);
expect(container).toMatchSnapshot();
});
it('renders canvas element', async () => {
const { container } = render(<Canvas />);
userEvent.click(getByText(container, 'Draw'));
expect(container).toMatchSnapshot();
});
it('validates canvas state after draw was clicked', () => {
const { container } = render(<Canvas />);
userEvent.click(getByText(container, 'Draw'));
const canvas: HTMLCanvasElement = getByTestId(container, 'canvas');
const ctx = canvas.getContext('2d');
const events = ctx.__getEvents();
expect(events).toMatchSnapshot();
});
it('gets updated with `dataURL` on draw', () => {
const callback = jest.fn();
const { container } = render(<Canvas onUpdate={callback} />);
userEvent.click(getByText(container, 'Draw'));
expect(callback).toBeCalledWith('data:image/png;base64,00');
});
}); So, @ggayowsky you were right as it goes to the workaround. |
Same here, but |
would the maintainers accept a PR to expose I'm working in a codebase where lots of existing tests use |
I was able to workaround this by |
Steps to reproduce:
npx create-react-app myapp --template typescript
cd myapp
npm install jest-canvas-mock
4: Edit App.test.tsx and add the following test
npm run test
The test should be able to create a canvas context but it comes back undefined
The text was updated successfully, but these errors were encountered: