reactjs - 用jest和enzyme窥探React功能组件方法;无法监视原始值

标签 reactjs unit-testing jestjs enzyme

我正在尝试测试 React 组件,并确保当单击其按钮时,会调用正确的方法。但是,当我尝试运行测试并尝试监视该方法时,我收到以下消息:

Error: Cannot spyOn on a primitive value; undefined given

如何测试单击按钮时是否调用正确的方法?谢谢!

sampleComponent.jsx:

import * as React from 'react';

const SampleComponent = () => {
  const sampleMethod = () => {
    console.log('hello world');
  };

  return <button onClick={sampleMethod} type="button">Click Me</button>;
};

export default SampleComponent;

sampleComponent.test.jsx:

import * as React from 'react';
import { shallow } from 'enzyme';
import SampleComponent from './sample';

test('testing spy', () => {
  const spy = jest.spyOn(SampleComponent.prototype, 'sampleMethod');
  const wrapper = shallow(<SampleComponent />);
  wrapper.find('button').simulate('click');
  expect(spy).toHaveBeenCalled();
});

最佳答案

该错误意味着,您在功能组件 SampleComponent 中定义的函数 sampleMethod 未在 SampleComponent.prototype 中定义。因此,SampleComponent.prototype.sampleMethod未定义,jest 无法监视未定义 值。

所以测试 sampleMethod 事件处理程序的正确方法是这样的:

index.spec.tsx:

import React from 'react';
import SampleComponent from './';
import { shallow } from 'enzyme';

describe('SampleComponent', () => {
  test('should handle click correctly', () => {
    const logSpy = jest.spyOn(console, 'log');
    const wrapper = shallow(<SampleComponent></SampleComponent>);
    const button = wrapper.find('button');
    expect(button.text()).toBe('Click Me');
    button.simulate('click');
    expect(logSpy).toBeCalledWith('hello world');
  });
});

我们可以监视console.log,断言它是否被调用。

100%覆盖率的单元测试结果:

 PASS  src/react-enzyme-examples/02-react-hooks/index.spec.tsx
  SampleComponent
    ✓ should handle click correctly (19ms)

  console.log node_modules/jest-mock/build/index.js:860
    hello world

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |      100 |      100 |      100 |      100 |                   |
 index.tsx |      100 |      100 |      100 |      100 |                   |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.036s

依赖版本:

"react": "^16.11.0",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.15.1",
"jest": "^24.9.0",
"jest-environment-enzyme": "^7.1.1",
"jest-enzyme": "^7.1.1",

关于reactjs - 用jest和enzyme窥探React功能组件方法;无法监视原始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58598995/

相关文章:

javascript - 仅包含导出的文件会降低测试覆盖率 Jest

python - 使用 mock 模拟嵌套属性

Django 测试安装固定装置时出错 - 没有这样的表

node.js - Jest 单元测试不会等待 axios 响应

javascript - 我可以在 React 类中包含 React 类吗?

javascript - 在 React 中发出自定义事件

reactjs - React的 `scryRenderedDOMComponentsWithClass`方法名从何而来?

django - 有没有办法将 Django 与 Next.js 集成?

javascript - JS/JestJS : How to test for a async mocked function?

angular - 如何用 Jasmine 大理石测试受试者