question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot set property 'fillStyle' of null

See original GitHub issue

Hey guys

First of all, thanks for your job with this, is really amazing and easy to use

I’m having this issue when I’m trying to test a component where I’m using react-lottie, also, keep in mind that this is happening when jest is trying to import Lottie. This is my component:

MyComponent.js

import React from 'react';
import Lottie from 'react-lottie';
mport loaderAnimation from './loaderData.json';
import profileAnimation from './profileData.json';

class AnimatedImages extends PureComponent {
  getAnimationData = () => {
    const { image } = this.props;
    switch (image) {
      case 'loader':
        return loaderAnimation;

      case 'profile':
        return profileAnimation;

      default:
        return loaderAnimation;
    }
  };

  render() {
    const { loop } = this.props;
    return (
      <div className="loader-wrapper">
        <Lottie
          options={{
            animationData: this.getAnimationData(),
            loop,
            autoplay: true
          }}
        />
      </div>
    );
  }
}

And my test file

MyComponent.spec.js

import React from 'react';
import { shallow, mount } from 'enzyme';
import MyComponent from './MyComponent';

jest.mock('lottie-web'); // I have tried mock the animation data, nothing
jest.mock('react-lottie');

describe('Animated images component', () => {
  it('should render with crashing', () => {
    const component = shallow(<AnimatedImages />); // I have tried it with mount, but nothing
    expect(component).toBeDefined();
  });
});

Once I run the test I got the error mentioned above and I’m not able to test the component properly. It will be great if you guys mention, if I’m doing something wrong, in your documentation the proper way to do unit tests for the components made with react-lottie. I saw your code and I notice that you have a test file, but I think is not very accurate for this particular case.

Let me know, if I’m doing something wrong, if not, I think that this is probably a bug for the component.

Thank you guys in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

17reactions
eliasrodelosocommented, Nov 9, 2018

UPDATE: I solved the problem, it wasn’t a bug, is a bad configuration issue: there is to add the canvas element to Jest, remember that Jest uses jsdom. I realized that, once I stopped trying to test the component and try to find and workaround for the other components and the console showed me the actual error:

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
        at module.exports ...

So to solve it, what I did was install jest-canvas-mock library, once installed I went to my setupTest.js and imported it globally. Probably I will make a PR with this info for the documentation.

Hopefully this will help some one else in the future.

0reactions
afsanefdaacommented, Jan 19, 2020

UPDATE: I solved the problem, it wasn’t a bug, is a bad configuration issue: there is to add the canvas element to Jest, remember that Jest uses jsdom. I realized that, once I stopped trying to test the component and try to find and workaround for the other components and the console showed me the actual error:

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
        at module.exports ...

So to solve it, what I did was install jest-canvas-mock library, once installed I went to my setupTest.js and imported it globally. Probably I will make a PR with this info for the documentation.

Hopefully this will help some one else in the future.

I did the same with updating my setting to:

{
  "jest": {
    "setupFiles": ["./__setups__/other.js", "jest-canvas-mock"]
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest import statement: 'TypeError: Cannot set property 'fillStyle ...
1. It has nothing to do with the import itself since the actual error is inside the phaser module (implying it was in...
Read more >
TypeError: Cannot set property 'fillStyle' of null #21 - GitHub
Getting this error for all the test cases when they try to mount the component using enzyme. It shows error corresponding to the...
Read more >
TypeError: Cannot set property 'fillStyle' of null - Techy Things
TypeError : Cannot set property 'fillStyle' of null. Solution. https://www.npmjs.com/package/jest-canvas-mock. $ yarn add --dev jest-canvas- ...
Read more >
React Jest Tests Failing Using Lottie.js Solved | Rob Marshall
TypeError: Cannot set property 'fillStyle' of null. The error is caused by the import of the lottie-react package.
Read more >
TypeError: Cannot set property 'fillStyle' of null - Bountysource
TypeError : Cannot set property 'fillStyle' of null.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found