简体   繁体   English

React Functional 组件上的 jest.mock - render 没有返回任何内容

[英]jest.mock on React Functional component - nothing was returned from render

I use clean Create React App instance with React Testing library我使用干净的 Create React App 实例和 React Testing 库

$ npx create-react-app test-app
...
$ npm install --save @testing-library/react @testing-library/jest-dom

Then I create simple Functional Component然后我创建简单的功能组件

./Chleb.js: ./Chleb.js:

import React from 'react';

const Chleb = () => <div>chleb</div>;

export default Chleb;

In default App.js:在默认的 App.js 中:

import React from 'react';
import Chleb from "./Chleb";

function App() {
  return (
    <div className="App">
      <Chleb />
    </div>
  );
}

export default App;

then, in the App.test.js:然后,在 App.test.js 中:

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

jest.mock('./Chleb');

describe('<App /> tests', () => {
  it('Should render the component correctly', () => {
    const {container} = render(<App/>);
    expect(container).toMatchSnapshot();
  });
});

What yields:产生什么:

Error: Chleb(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

I think that either Jest or RTL is not supporting arrow functions?我认为 Jest 或 RTL 不支持箭头功能?

That's really strange, but when I convert Chleb.js to old React Class component, it works correctly.这真的很奇怪,但是当我将 Chleb.js 转换为旧的 React Class 组件时,它可以正常工作。 It also works, when I provide manual fn mock implementation in the jest.mock('...', fn) .当我在jest.mock('...', fn)中提供手动fn模拟实现时,它也有效。

Is it required to provide manual mock implementation for React Functional components, or is this some kind of bug?是否需要为 React Functional 组件提供手动模拟实现,或者这是某种错误?

try setting resetMocks as false in jest config尝试在玩笑配置中将 resetMocks 设置为 false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM