dimanche 27 septembre 2020

Is there a quick way to automock react components

I have a large amount of react component that require a simple mock version.

Currently for each component I have to create a new mock folder then create a file for that component and then a new simplified version of the component.

├── models
│   ├── __mocks__
│   │   └── componentA.js
│   │   └── componentB.js
│   │   └── componentC.js
│   └── componentA.js
│   └── componentB.js
│   └── componentC.js

Each of my mocked files just looks like this:

import React from 'react';

const ComponentA = ({
}) => (
  <div>
    Mocked ComponentA
  </div>
);
export default ComponentA;

It results in a lot of extra work and repeated code.

Is there a way to use the Jest automock tool to automatically implement the above.

I tried using the following but it return a babel error:

const ReactCMock = (name) => ({
  __esModule: true,
  default: (props) => (
    <div className={name}>
      {name}
    </div>
  ),
});

jest.mock('./componentA', ReactCMock('Component A'));

error: babel-plugin-jest-hoist: The second argument of jest.mock must be an inline function.

Aucun commentaire:

Enregistrer un commentaire