用笑话和酶窥探 React 功能组件方法;无法窥探原始值

Jim*_*mmy 17 unit-testing reactjs jestjs enzyme

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

错误:无法监视原始值;给定的未定义

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

示例组件.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;
Run Code Online (Sandbox Code Playgroud)

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();
});
Run Code Online (Sandbox Code Playgroud)

sli*_*wp2 16

错误意味着,sampleMethod您在功能组件中定义的函数SampleComponent未在SampleComponent.prototype. 那么SampleComponent.prototype.sampleMethodundefined,玩笑不能间谍undefined值。

所以测试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');
  });
});
Run Code Online (Sandbox Code Playgroud)

我们可以监视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
Run Code Online (Sandbox Code Playgroud)

依赖版本:

"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",
Run Code Online (Sandbox Code Playgroud)

  • 假设上面的sampleMethod 有一些其他代码而不是console.log 语句。谁能告诉我们我们应该如何编写spyOn 呢? (11认同)
  • 监视 `const logSpy = jest.spyOn(console, 'log');` 是对功能组件的黑客攻击吗,我们不能监视实际的函数名称 **sampleMethod** 吗?另外,如果我们使用**类组件**,我们可以通过函数名称进行监视,而不是使用“console.log”,对吧?我的意思是黑客很好,但我想知道我们在生产时无法保留控制台语句 (5认同)
  • @karthikreddy 您应该“监视”“sampleMethod”使用的依赖项。例如,如果在“sampleMethod”内部调用了“axois.get(...)”语句,您应该像这样监视它:“jest.spyOn(axois, 'get').mockResolvedValue({})” 。触发“sampleMethod”后,您应该断言“expect(axois.get).toBeCalled()”。`console.log` 仅用于演示目的 (2认同)

Mur*_*ifa 8

示例.js

import * as React from 'react';

export let util = {sampleMethod: null };

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

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

export default SampleComponent;
Run Code Online (Sandbox Code Playgroud)

示例.test.js

import { shallow } from 'enzyme';
import SampleComponent, {util} from './sample';

test('testing spy', () => {
  const spy = jest.spyOn( util, 'sampleMethod' );
  const wrapper = shallow(<SampleComponent />);
  wrapper.find('button').simulate('click');
  expect(spy).toHaveBeenCalled(1);
});
Run Code Online (Sandbox Code Playgroud)

我知道我回答晚了,但我认为这也会对其他一些开发人员有所帮助

  • 无法监视sampleMethod 属性,因为它不是函数;未定义给定代替 (2认同)