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

Advice for testing uPlot in unit tests (Jest) #497

Closed
hmillison opened this issue Apr 16, 2021 · 5 comments
Closed

Advice for testing uPlot in unit tests (Jest) #497

hmillison opened this issue Apr 16, 2021 · 5 comments
Labels
question Further information is requested

Comments

@hmillison
Copy link

hmillison commented Apr 16, 2021

Curious what the best practice for interacting with uPlot in my UI unit tests is because uPlot is using canvas under the hood it seems to fail to render in the Jest environment due to Uncaught TypeError: Cannot read property ‘clearRect’ of undefined.

Is the recommendation to mock uPlot in this case or is there a different approach used by folks?

Environment used for testing:
Node 14.15
Jest, React v16, React Testing Library

@leeoniya
Copy link
Owner

leeoniya commented Apr 16, 2021

there's this: https://www.npmjs.com/package/canvas, but it has no Path2D support: Automattic/node-canvas#1116

your best bet will be to automate a headless browser. see:

https://jestjs.io/docs/puppeteer and
https://github.com/playwright-community/jest-playwright

@leeoniya leeoniya added the question Further information is requested label Apr 16, 2021
@leeoniya
Copy link
Owner

also check out #175

@hmillison
Copy link
Author

Thanks for the fast response ❤️ ! Appreciate pointing me in the right direction on this.

I have some automated tests with Cypress so that is definitely an option. I will experiment with the canvas library to see if that can give me what i was looking for, thanks!

@hmillison
Copy link
Author

For anyone who comes across this issue in the future, Jest Canvas Mock (https://www.npmjs.com/package/jest-canvas-mock) did the trick of getting this library to render without errors.

Note that nothing will actually render in the canvas so you will most likely need to go the browser test route if you want to test the functionality of uPlot in your app.

@nielsuit227
Copy link

Hi @hmillison, thanks for pointing towards this package. I'm quite curious how you ended up using this? Did you combine it with cypress or check all draw calls with jest?

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

No branches or pull requests

3 participants