-
-
Notifications
You must be signed in to change notification settings - Fork 56
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: Not implemented: HTMLCanvasElement.prototype.getContext #2
Comments
I messed one thing up ✌️ seems to work now |
@hustcc I just made a typo 😄 |
Thanks! Turns out all I actually had to do to fix this was, well, install the canvas npm package as described in the error message!
|
@dariusk and others: installing and building canvas requires the nodeJS headers (plus other native libraries) so that it can be built locally. If your CI is in an environment that does not have Internet access for security reasons and uses, say, an Artifactory server, downloading canvas and building, or even other prebuilt binaries (e.g., canvas-prebuilt) this will not work and mocking may be the right way to go unless you need to test the actual canvas drawing. As we do, we've solved this by building some base Docker images (with node-canvas installed and built locally) and using those images in CI as a base for our containers in which we run our unit and integration tests. @hustcc sorry for the delay, let me see if i can whip something up quickly. |
@hustcc apologies, I misspoke earlier and updated my original comment to be more precise. While the tests pass, JSDom complains for each one. This is a known issue with JSDom but while node-canvas is still in alpha, they're not going to fix it. Sadly, even wrapping with a try/catch (which probably isn't the best idea anyway) doesn't work. IMO, given that the point of this library is to mock the canvas element, deferring to a locally installed version, if present, isn't necessarily the right solution. People may want to test it, say via integration, and so it isn't necessary at the unit level. Here's the stacktrace:
|
So catch the throw by |
@hustcc No, wrapping with try/catch won't solve it. I didn't dig into JSDom's code as to why, though it would probably only take a few minutes. I might suggest allowing a configuration option (assuming Jest allows such a thing for setup files) to bypass a native canvas check and just use the mocked value. |
JSDom did not implement this method The point is I can not reproduce this problem locally, and what your jest and jsdom version? |
@hustcc actually I also installed canvas prebuilt that worked for me better than installing the canvas package 👍 That solved my problem |
@hustcc latest version of Jest, 22.3.0 and we do not install JSDom directly rather, Jest pulls it in (version 11.6.2 according to its package.json). In order to reproduce and utilize your actual mock code and have the error displayed, you will need to ensure that no prebuilt version of canvas is installed (and there are several as indicated above). As I mentioned, the tests still pass, but each test that ultimately uses canvas will log that error. @bytewiz That works for us when running locally, but our CI environments are locked down and do not have internet access for security reasons so downloading prebuilt binaries is not allowed. |
@icfantv Reproduce this problem locally. Jest@^22.0.0 will occur. JSDom's function We can pass the ci(when console.error) locally, but remote ci can not. Two way can solve it:
global.window.console.error = (...a) = { console.warning(...a) }; |
v1.0.1 published, used in echarts-for-react. In addition, I think PR can be send to JSDom to fix this issue. |
@hustcc sorry, I’m confused. I did reproduce the problem locally. Note that it is a cosmetic error and our Jest tests do still pass. Since we pulled your code in-house and modified it for or purposes, I’m not sure we need to do anything else. Regarding your modification of window.console, I don't think this is a good idea. Modification of the standard DOM is risky, even if only in the land of testing... |
use v1.0.1 can avoid the error. the lastest jsdom implement the function So when we run the ci with jest-canvas-mock@1.0.0, will execute print the error, because of the return is undefined, so we mock it successfully. The correct way for jsdom is throw error instead of print error when the function is waiting for completed. English is poor, I do not know can understand |
The error repetition at this version:
|
any detail information? such as screenshot, jsdom version, demo. |
jest: (23.6.0) |
Since we pulled this in-house and modified it, we have had no issues. I'd recommend snapshot testing for testing canvas drawings. Be aware that JSDom traps thrown errors and logs them to |
Thanks. Finally I override |
I think can send a pr to jsdom. |
If you don't need to test the getContext-method explicitly and for the sake of getting a cleaner test-log you can mock the method by:
Example of a test case
|
@ssolders Here is the snippet that sets the prototype method. HTMLCanvasElement.prototype.getContext = getContext2D; And the tests look like this. This issue is currently fixed in the alpha version up on npm. You might need to use the following command to install it. npm install --save-dev jest-canvas-mock@latest I just added these tests, and they will ship with the beta for clarity reasons. |
Solution proposed by @ssolders was almost fine to avoid the following error
In my case it I had to mock this method in my .spec.ts file
However, in my scenario I tested particles.js, having Jest as a testing framework I still had another error
With 1 line of code change was able to get rid off these 2, in essence to my Jest config setupFiles array (jest.config.js, using Nx workspace) imported jest-canvas-mock like so
Finding this as a leaner approach, maybe for some of you guys might be useful, here is a link to Medium super short post about global mocks for external libraries. The author says about global mocks, but I took the part with setupFiles in package.json. As mentioned earlier, using Nx workspace) for Angular, for me it was to set it up in jest.config.js, and it works fine. In my package.json don't have jsdom, below necessary code snippets to reproduce, as well dependencies with its versions. package.json
jest.config.js
Now my .spec.ts is simple as that, without throwing any error
Before even though the component was working totally on ng serve without any single error/warning. After running tests ng test I started to get all these errors, took 2-3 days for me to fix it, if anyone of you'd have similar issues, just try to follow the snippets, should be fine. |
I solved this problem is:
|
Solution proposed by @chenxiaochun solved my issue. Thank you! |
Looks great @chenxiaochun, that time I was solving the issue |
* Update styled-components, webpack, and babel-loader. * Remove builder and use dash-generate-components. * Add webpack config. * Update bundle location and .eslintignore. * Remove old build artefacts. * Fix tests. Change shallow mount to full mount; call render(); check props and not state of components. * Split exponentials and regular digits for PrecisionInput test. * Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps. * Add default value for required precision prop in PrecisionInput. * Comply with prop type for min in GraduatedBar test. * Comply with prop type for secondary in Indicator component. * Add jest-canvas-mock for Chrome colorpicker. See: hustcc/jest-canvas-mock#2 (comment) * Update requirements. * Move jest-canvas-mock to dev dependencies. * Update CHANGELOG. * Update version in package.json. * Add back build artifacts.
The fix for me was to |
Can I say, that this is not a solution? Why on earth, would I need another dependency? This is not acceptable I am afraid. Can someone, fix this, please. It seems that this is a problem for almost a year, and nothing is being done? Come on!! Until you can fix it though, please don't import unnecessary pacakges. Just do the following, either in the
|
Jest canvas mock implements this function and emulates a lot of the functions properly. If perhaps importing a mock package is too much work or overhead, then don't use it? |
* Use global value mappings in Overrides * Fix "Not implemented: HTMLCanvasElement.prototype.getContext" test output. See: hustcc/jest-canvas-mock#2 * Update yarn.lock with the new jest-canvas-mock dependency Co-authored-by: Brian Gann <briangann@users.noreply.github.com>
…hen running tests: "Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)" See: hustcc/jest-canvas-mock#2 (comment)
Canvas is super bulk. avoid using |
For anyone who is trying to switch from react-scripts / jest to Vitest None of the snippets I found worked, I had to modify it to dynamically import it within a function since imports are hoisted
|
For Vitest, this mocking worked for me. // vitest.setup.ts
global.HTMLCanvasElement.prototype.getContext = () => null;
global.URL.createObjectURL = () => ""; |
I tried, but it does't work, please show more |
Using jest-canvas-mock still throw this error when running jest:
Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
The text was updated successfully, but these errors were encountered: