如何使用 Jest 和新的 React lazy 16.6 API 测试快照

新手上路,请多包涵

我必须使用新的 React lazy API (16.6) 导入组件。

 import React, {PureComponent, lazy} from 'react';

const Component1 = lazy(() => import('./Component1'));
const Component2 = lazy(() => import('./Component2'));

class CustomComponent extends PureComponent {
  ...
  render() {

  return (
    <div>
      <Component1 />
      <Component2 />
    </div>
  );
 }
}

在我的测试中,我正在做这个组件的快照。这是一个非常简单的测试:

 import { create } from 'react-test-renderer';

const tree = await create(<CustomComponent />).toJSON();

expect(tree).toMatchSnapshot();

在日志中,测试失败并出现以下错误:

 A React component suspended while rendering, but no fallback UI was specified.

Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.

我是否必须用 <Suspense>... 包装每个测试套件?

 it('should show the component', async () => {
  const component = await create(
    <React.Suspense fallback={<div>loading</div>}>
     <CustomComponent />
    </React.Suspense>
  );
  const tree = component.toJSON();

  expect(tree).toMatchSnapshot();

};

如果这样做,我只会在快照中看到 fallback 组件。

 + Array [ + <div> + loading + </div>, + ]

那么,哪种方法最好呢?

原文由 Albert Olivé Corbella 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 556
2 个回答

我是否必须用 <Suspense> 包装每个测试套件?

是的, Suspense 组件是延迟加载子组件所必需的,特别是在延迟组件可用时提供回退和协调。

导出 Component1Component2 CustomComponent 以便它们可以在测试中导入。

 import React, {PureComponent, lazy} from 'react';

export const Component1 = lazy(() => import('./Component1'));
export const Component2 = lazy(() => import('./Component2'));

export default class CustomComponent extends PureComponent {
  //...
}

请记住,延迟加载的组件是类似 promise 的。在测试中导入它们,并在检查快照是否匹配之前等待它们解析。

 import { create } from 'react-test-renderer';
import React, {Suspense} from 'react';
import CustomComponent, {Component1, Component2} from './LazyComponent';

describe('CustomComponent', () => {
  it('rendered lazily', async()=> {
    const root = create(
      <Suspense fallback={<div>loading...</div>}>
        <CustomComponent/>
      </Suspense>
    );

    await Component1;
    await Component2;
    expect(root).toMatchSnapshot();
  })
})

原文由 Oluwafemi Sule 发布,翻译遵循 CC BY-SA 4.0 许可协议

根据 github 中的这条评论,您可以使用 Jest 模拟惰性组件以返回实际组件,尽管您需要将惰性语句移动并导出到它们自己的文件中才能工作。

 // LazyComponent1.ts
import { lazy } from 'react';

export default lazy(() => import('./Component1'));

 // CustomComponent.tsx
import React, { PureComponent } from 'react';
import Component1 from './LazyComponent1';
import Component2 from './LazyComponent2';

class CustomComponent extends PureComponent {
  ...
  render() {

  return (
    <div>
      <Component1 />
      <Component2 />
    </div>
  );
 }
}

 // CustomComponent.spec.tsx
import React, { Suspense } from 'react';
import { create } from 'react-test-renderer';
import CustomComponent from './CustomComponent';

jest.mock('./LazyComponent1', () => require('./Component1'));
jest.mock('./LazyComponent2', () => require('./Component2'));

describe('CustomComponent', () => {
  it('should show the component', () => {
    const component = await create(
      <Suspense fallback={<div>loading</div>}>
       <CustomComponent />
      </Suspense>
    );
    const tree = component.toJSON();

    expect(tree).toMatchSnapshot();
  });
});

原文由 RecuencoJones 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题