synyx / jest-canvas-snapshot-serializer

Jest snapshot serializer to generate comparable canvas snapshots
Apache License 2.0
9 stars 3 forks source link

Cannot read property 'test' of undefined #2

Open ghost opened 6 years ago

ghost commented 6 years ago

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 commented 5 years ago

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';