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

Jest Snapshot Test Failure: Cannot read property 'getContext' of null #15

Open
joncursi opened this issue Dec 20, 2017 · 5 comments
Open

Comments

@joncursi
Copy link

// tests.js

import * as React from 'react';
import renderer from 'react-test-renderer';

import QrCode from '.';

it('renders correctly', () => {
  const tree = renderer
    .create(<QrCode />)
    .toJSON();
  expect(tree).toMatchSnapshot();
});

Error:

    TypeError: Cannot read property 'getContext' of null

       6 | it('renders correctly', () => {
       7 |   const tree = renderer
    >  8 |     .create(<QrCode />)
@danielcooperxyz
Copy link

danielcooperxyz commented Jun 15, 2018

I get a similar issue when attempting to render my app.

TypeError: Cannot read property 'webkitBackingStorePixelRatio' of null

	15 |      };
	16 |
	> 17 |     ReactDOM.render(
		|                        ^
	18 |         <Provider store={mockStore(state)}>
	19 |             <App />
	20 |         </Provider>,

	at getBackingStorePixelRatio (node_modules/qrcode-react/dist/index.js:17:15)
	at QRCode.update (node_modules/qrcode-react/dist/index.js:90:58)
	at QRCode.componentDidMount (node_modules/qrcode-react/dist/index.js:51:18)
	at commitLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:14248:10)

I think this is related to a need for mocking the canvas, as Jest uses JSDom to "render" the components.

@danielcooperxyz
Copy link

danielcooperxyz commented Jun 15, 2018

Doing a little more research I found that jsdom does have support for canvas via an external package.

Installing canvas or canvas-prebuilt as a dependency allowed it to work for me.

@codeepic
Copy link

@danielcooperxyz I added canvas-prebuilt package to devDependencies and moved it to dependencies but in both cases I am still getting an error TypeError: Cannot read property 'backingStorePixelRatio' of null when running Jest tests.

@zhuangya
Copy link

Using https:/hustcc/jest-canvas-mock. If you are using Create React App just add import 'jest-canvas-mock' in your setupTests.js

credit: https://stackoverflow.com/a/53478780/553523

@PatilAkshay-1
Copy link

@danielcooperxyz I added canvas-prebuilt package to devDependencies and moved it to dependencies but in both cases I am still getting an error TypeError: Cannot read property 'backingStorePixelRatio' of null when running Jest tests.

Did you get your issue resolved ?? If yes , please help me. I am getting the same error working on React testing using Jest.

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

No branches or pull requests

5 participants