In my Jest unit test, I present a component with ColorPicker . The ColorPicker creates a canvas object and a 2d context, but returns 'undefined' , which throws the error "Cannot set property 'fillStyle' of undefined"
if (typeof document == 'undefined') return null; // Dont Render On Server var canvas = document.createElement('canvas'); canvas.width = canvas.height = size * 2; var ctx = canvas.getContext('2d'); // returns 'undefined' ctx.fillStyle = c1; // "Cannot set property 'fillStyle' of undefined"
I am having problems figuring out why I cannot get a 2d context. Maybe there is a problem with my test configuration?
"jest": { "scriptPreprocessor": "<rootDir>/node_modules/babel-jest", "unmockedModulePathPatterns": [ "<rootDir>/node_modules/react", "<rootDir>/node_modules/react-dom", "<rootDir>/node_modules/react-addons-test-utils", "<rootDir>/node_modules/react-tools" ], "moduleFileExtensions": [ "jsx", "js", "json", "es6" ], "testFileExtensions": [ "jsx" ], "collectCoverage": true }
html5-canvas reactjs jestjs jsdom
Adgezaza
source share