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

Cannot read property 'test' of undefined #2

Open
ghost opened this issue Sep 25, 2018 · 1 comment
Open

Cannot read property 'test' of undefined #2

ghost opened this issue Sep 25, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Sep 25, 2018

I've created a chart.js plugin that I now want to test. I'm running this as documented I get the following error:

TypeError: Cannot read property 'test' of undefined

Here is my test:

  let canvas: any;
  beforeEach( () => {
    canvas = document.createElement('canvas');
    canvas.setAttribute('width', '100');
    canvas.setAttribute('height', '100');
  });
  describe( 'plugin', () => {
    it('renders canvas', () => {
      plugins.plugin.beforeDatasetDraw( {
        config: {
          type: 'doughnut'
        },
        outerRadius: 100,
        innerRadius: 80,
        chartArea: {
         right: 100,
         bottom: 100
        },
        ctx: canvas.getContext('2d')
      });
      expect( canvas ).toMatchSnapshot();
    });
  });

The plugin I'm testing:

const plugin = {
  id: 'plugin',
  beforeDatasetDraw: (chart: any) => {
    if (chart.config.type === 'doughnut') {
      const ctx = chart.ctx;
      ctx.beginPath();
      ctx.strokeStyle = '#D2D2D2';
      ctx.arc(
        chart.chartArea.right / 2,
        chart.chartArea.bottom / 2,
        (chart.outerRadius + chart.innerRadius) / 2,
        0,
        2 * Math.PI
      );
      ctx.stroke();
    }
  }
};
@StephanBijzitter
Copy link

The README is a bit wrong here.

If not using TypeScript, Babel will allow this (even though there is no default export).

import canvasSerializer from 'jest-canvas-snapshot-serializer';

TypeScript requires you to do it properly:

import * as canvasSerializer from 'jest-canvas-snapshot-serializer';

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

1 participant