jest-canvas-mock icon indicating copy to clipboard operation
jest-canvas-mock copied to clipboard

Be able to use with create-react-scripts

Open hutber opened this issue 5 years ago • 5 comments

It would be great to be able to use this with create-react-app

$ react-scripts test --watchAll=false

Out of the box, Create React App only supports overriding these Jest options:

  • clearMocks
  • collectCoverageFrom
  • coveragePathIgnorePatterns
  • coverageReporters
  • coverageThreshold
  • displayName
  • extraGlobals
  • globalSetup
  • globalTeardown
  • moduleNameMapper
  • resetMocks
  • resetModules
  • restoreMocks
  • snapshotSerializers
  • transform
  • transformIgnorePatterns
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • setupFiles

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

hutber avatar Feb 14 '20 16:02 hutber

i do not know how to use jest-canvas-mock without configure setupFiles.

hustcc avatar May 03 '20 02:05 hustcc

I met this problem, too.

I have a react app created by create-react-app contains some canvas element. Without this package I get somthing like this.

// App.test.js
import React from 'react';
import { render } from '@testing-library/react';
import App from '../App';

describe('App', () => {
  test('renders App component', () => {
    render(<App />);
  });
});
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
  Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
...
...
 ● App › renders App component

    TypeError: Cannot read property 'beginPath' of null

If I change jest setting in package.json I got the same error as hutber

// package.json
{
  ...
  "jest": {
    "setupFiles": ["jest-canvas-mock"]
  }
}

import this package in src/setupTest.js didn't work as well

Finally, I import it in each of the test files that were invoking canvas element like App.test.js before. By far it works for me

wispamulet avatar Jun 24 '20 06:06 wispamulet

@wispamulet I have the same problem, despite import in each test file doesn't work for me. I try it like this:

import {} from 'jest-canvas-mock'

But I still get a very similar error message as you did:

Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)

How did you import it?

Juuro avatar Aug 11 '20 01:08 Juuro

@Juuro it's been a while so I hope this would work 😯

// __test__/App.test.tsx
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, screen, RenderResult } from '@testing-library/react';
import 'jest-canvas-mock';
import App from '../App';

const setup = (): RenderResult =>
  render(
    <MemoryRouter>
      <App />
    </MemoryRouter>
  );

describe('App', () => {
  setup();

  it('should ...', () => {
    // do your job here
  });
});

wispamulet avatar Aug 20 '20 08:08 wispamulet

You can also do the import in your CRA app's setupFiles.js so that you don't need to manually import in every test file.

derrxb avatar Oct 22 '20 12:10 derrxb