React 16 Jest ES6级模拟 - 监视并监视模拟情况

简介: 转载地址React 16 Jest ES6级模拟 - 监视并监视模拟情况项目初始化git clone https://github.

转载地址

React 16 Jest ES6级模拟 - 监视并监视模拟情况

项目初始化

git clone https://github.com/durban89/webpack4-react16-reactrouter-demo.git 
cd webpack4-react16-reactrouter-demo
git fetch origin
git checkout v_1.0.32
npm install

ES6 Class Mocks(使用ES6语法类的模拟)

Jest可用于模拟导入到要测试的文件中的ES6语法的类。

ES6语法的类是具有一些语法糖的构造函数。因此,ES6语法的类的任何模拟都必须是函数或实际的ES6语法的类(这也是另一个函数)。
所以可以使用模拟函数来模拟它们。如下

跟踪使用情况(监视模拟)(Keeping track of usage (spying on the mock))

注入测试实现很有帮助,但我们可能还想测试是否使用正确的参数调用类构造函数和方法。

监视构造函数(Spying on the constructor)

为了跟踪对构造函数的调用,用一个Jest模拟函数替换HOF返回的函数。
使用jest.fn()创建它,然后使用mockImplementation()指定它的实现。如下

import SoundPlayer from '../lib/sound-player';
jest.mock('../lib/sound-player', () => {
  // 检查构造函数的调用
  return jest.fn().mockImplementation(() => {
    return {
      choicePlaySoundFile: () => {},
      playSoundFile: () => {},
    };
  });
});


我们使用SoundPlayer.mock.calls来检查我们的模拟类的用法:expect(SoundPlayer).toHaveBeenCalled();
或接近等价的:expect(SoundPlayer.mock.calls.length).toEqual(1);

监视类的方法(Spying on methods of our class)

我们的模拟类需要提供将在测试期间调用的任何成员函数(示例中为playSoundFile),否则我们将因调用不存在的函数而出错。
但是我们可能也希望监视对这些方法的调用,以确保使用预期的参数调用它们。

每次在测试期间调用模拟构造函数时,都会创建一个新对象。
为了监视所有这些对象中的方法调用,我们使用另一个mock函数填充playSoundFile,并在我们的测试文件中存储对同一个mock函数的引用,因此它在测试期间可用。

import SoundPlayer from '../lib/sound-player';
const mockPlaySoundFile = jest.fn();
const mockChoicePlaySoundFile = jest.fn();
jest.mock('../lib/sound-player', () => {
  return jest.fn().mockImplementation(() => {
    return {
      choicePlaySoundFile: mockChoicePlaySoundFile,
      playSoundFile: mockPlaySoundFile,
    };
  });
});

手动模拟等效于此:

export const mockChoicePlaySoundFile = jest.fn();
const mockPlaySoundFile = jest.fn();

const mock = jest.fn().mockImplementation(() => {
  const data = {
    choicePlaySoundFile: mockChoicePlaySoundFile,
    playSoundFile: mockPlaySoundFile,
  };

  return data;
});

export default mock;


用法类似于模块工厂函数,除了可以省略jest.mock()中的第二个参数,并且必须将模拟方法导入到测试文件中,因为它不再在那里定义。
使用原始模块路径;
不包括__mocks__

在测试之间进行清理(Cleaning up between tests)

要清除对mock构造函数及其方法的调用记录,我们在beforeEach()函数中调用mockClear():

前面的文章分别做了4个实例,分别是下面四个文件,可以自己打开项目去具体看下,这里就不在展示了

src/__tests/jest_sound_player.test.js
src/__tests/jest_sound_player_2.test.js
src/__tests/jest_sound_player_3.test.js
src/__tests/jest_sound_player_4.test.js

实践项目地址

git clone https://github.com/durban89/webpack4-react16-reactrouter-demo.git
git checkout v_1.0.32
目录
相关文章
|
4月前
|
JavaScript 前端开发
Vue 或者 React 中,什么情况下会用 Es6 的 Class 类
Vue 或者 React 中,什么情况下会用 Es6 的 Class 类
|
9月前
|
资源调度 JavaScript 前端开发
react+jest+enzyme配置及编写前端单元测试UT
react+jest+enzyme配置及编写前端单元测试UT
80 0
|
11月前
|
前端开发 JavaScript 测试技术
全网最细:Jest+Enzyme测试React组件(包含交互、DOM、样式测试)
Jest是目前前端工程化下单元测试火热的技术栈,而Enzyme的支持提供了Jest测试React业务、组件的能力,下面来介绍一下React组件测试的一些实际场景。
88 1
|
Web App开发 前端开发 JavaScript
大型React项目需要使用ES6解决方案以及对JSX的使用【React高级技术】
大型React项目需要使用ES6解决方案以及对JSX的使用【React高级技术】
|
资源调度 前端开发 JavaScript
react生态下jest单元测试
react生态下jest单元测试
1821 1
react生态下jest单元测试
|
Java
写给Java后端的ReactJS快速入门教程-ES6中的class类关键字
写给Java后端的ReactJS快速入门教程-ES6中的class类关键字
122 0
写给Java后端的ReactJS快速入门教程-ES6中的class类关键字
|
存储 Web App开发 前端开发
ES6, Angular,React和ABAP中的String Template(字符串模板)
ES6, Angular,React和ABAP中的String Template(字符串模板)
171 0
ES6, Angular,React和ABAP中的String Template(字符串模板)
|
前端开发 测试技术 API
React 16 Jest单元测试 之 Mock Functions (Mocking Modules 和 Mock Implementations)
转载 React16 Jest单元测试 之 Mock Functions(Mocking Modules 和 Mock Implementations) 项目初始化【这里使用之前的项目,节省时间】 项目初始化地址 https://github.
1822 0
|
前端开发 测试技术 开发工具
React 16 Jest单元测试 之 Mock Functions(Mock Return Values)
转载地址 React16 Jest单元测试 之 Mock Functions(Mock Return Values) 项目初始化【这里使用之前的项目,节省时间】 项目初始化地址 https://github.
1342 0