Jest React - 未写入新快照。必须显式传递更新标志才能写入新快照

Auo*_*Auo 8 javascript reactjs jestjs react-native asp.net-core

我有一个用 React 模板创建的 asp.net 核心项目,尝试使用 Jest 快照对一个简单的组件进行单元测试,我收到以下错误。有人建议如何修复它。

索引.js

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { Button } from 'reactstrap'

const CloseHistoryButton = ({ onClick }) =>
    <div className="d-flex justify-content-end">
        <Button color="danger" size="sm" onClick={onClick}>
            <FontAwesomeIcon icon="times" /> Close History
        </Button>
    </div>

export default CloseHistoryButton  
Run Code Online (Sandbox Code Playgroud)

CloseHistoryButton.test

import ReactDOM from 'react-dom';
import { shallow, mount, render } from 'enzyme';
import { cleanup } from '@testing-library/react';
import renderer from 'react-test-renderer';
import CloseHistoryButton from '../CloseHistoryButton/index';
import registerIcons from './../../../../icons/registerIcons';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

// automatically unmount and cleanup DOM after the test is finished.
afterEach(cleanup);
registerIcons();
test('renders correctly', () => {
    const tree = renderer.create(
                                <CloseHistoryButton />
                                ).toJSON();
    expect(tree).toMatchSnapshot();
});

Run Code Online (Sandbox Code Playgroud)

错误日志:

期望(收到)。toMatchSnapshot()

New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.

This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.

Received value
<div
  className="d-flex justify-content-end"
>
  <button
    aria-label={null}
    className="btn btn-danger btn-sm"
    onClick={[Function]}
  >
    <svg
      aria-hidden="true"
      className="svg-inline--fa fa-times fa-w-11 "
      data-icon="times"
      data-prefix="fas"
      focusable="false"
      role="img"
      style={Object {}}
      viewBox="0 0 352 512"
      xmlns="http://www.w3.org/2000/svg"
    >
      <path
        d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
        fill="currentColor"
        style={Object {}}
      />
    </svg>
     Close History
  </button>
</div>

  27 |                                 <CloseHistoryButton />
  28 |                                 ).toJSON();
> 29 |     expect(tree).toMatchSnapshot();
     |                  ^
  30 | });
  31 |
  32 | describe('Test Button component', () => {

  at Object.toMatchSnapshot (src/features/FleetImport/Results/CloseHistoryButton/CloseHistoryButton.test.js:29:18)
Run Code Online (Sandbox Code Playgroud)

› 1 个快照失败。快照摘要 › 1 个测试套件中的 1 个快照失败。检查您的代码更改或重新运行 jest-u以更新它们。

测试套件:1 个失败,1 个通过,总共 2 个测试:1 个失败,3 个通过,总共 4 个快照:1 个失败,总共 1 个时间:4.338 秒运行所有测试套件。npm 错误!测试失败。有关更多详细信息,请参见上文。

akh*_*ary 8

在运行测试的 package.json 脚本中,附加-u以在运行测试时更新快照,例如react-scripts test -u. 这应该修复 CI 中的快照测试。

  • 虽然让“npm run test”自动更新快照听起来是个好主意,但它可以很容易地覆盖损坏的组件而不被注意到。最好将其保留为单独的命令,这样如果测试失败,您就有机会检查错误。 (3认同)
  • ^ 正如@Yarik 所提到的,你所做的基本上是隐藏问题。CI 负责测试,而不是动态操作快照。您也可以删除快照。 (3认同)

ImL*_*Leo 7

只需在您的 CI 配置中明确传递它,例如:

- yarn test -- --coverage --updateSnapshot
Run Code Online (Sandbox Code Playgroud)

  • 在没有监督的情况下修复快照(即通过自动 CI 作业)确实是糟糕的设计。手动更新它们是关键。这里的评论是正确的:/sf/ask/4135477251/?noredirect= 1#评论107908883_59083491 (5认同)
  • 为什么要在 CI 中更新快照,这是非常错误的。失败的测试正是 CI 测试构建的目的。 (3认同)
  • 新快照未写入。必须显式传递更新标志才能写入新快照。这可能是因为此测试是在持续集成 (CI) 环境中运行,默认情况下不写入快照。 (2认同)