profile
viewpoint

Ask questionsDetect if canvas exists before calling Browser canvas

v0.9.4 adds AppLauncher Tile component to the default export of the library. It uses the browser HTMLCanvasElement functionality. You may see errors in Jest tests now, since JSDOM does not implement Browser Canvas functionality.

Any calls to HTMLCanvasElement should be protected and only run if that object exists. Something like if (canvas.getContext) {}

https://github.com/salesforce/design-system-react/blob/master/components/utilities/truncate/index.jsx#L21

console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
          at module.exports (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
          at HTMLCanvasElementImpl.getContext (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js:42:5)
          at HTMLCanvasElement.getContext (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/HTMLCanvasElement.js:50:45)
          at Object.<anonymous> (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/@salesforce/design-system-react/lib/components/utilities/truncate/index.js:48:26)
          at Runtime._execModule (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-runtime/build/index.js:694:13)
          at Runtime.requireModule (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-runtime/build/index.js:376:14)
          at Runtime.requireModuleOrMock (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-runtime/build/index.js:463:19)
          at Object.<anonymous> (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/@salesforce/design-system-react/lib/components/app-launcher/tile.js:23:17)
          at Runtime._execModule (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-runtime/build/index.js:694:13)
          at Runtime.requireModule (/Users/tswardenski/salesforce/repos/mce-eef-ui/node_modules/jest-runtime/build/index.js:376:14) undefined
salesforce/design-system-react

Answer questions BenjaminWFox

Do you have any update on this or workaround for preventing the errors while running tests?

ETA: Sorry, should have done a little more digging. I've prevented the errors for now using jest-canvas-mock.

I'll follow this in case an update is made that would prevent these calls in the first place. Thanks!

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List