To write tests for our React code, we need to first install some libraries for running tests and writing assertions. In this lesson we walk through setting up Mocha as our test runner and expect as our assertion library. We will also set up some React and JSX specific test tools (React Test Utils) to make writing our tests easier.

NOTE: There are many alternatives to Mocha, expect, and the other test tools we use in this course. Although we use these specific tools and libraries, the testing principles apply to all other tools.

package.json:

{
"name": "react-testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"expect": "1.13.4",
"mocha": "2.3.4",
"react-addons-test-utils": "0.14.5"
},
"dependencies": {
"react": "0.14.5",
"react-dom": "0.14.5"
}
}

we walk through how to setup our tests and run them. We write a quick empty first test and assertion, so we can run the tests. Using Mocha, we can do this manually each time with the Mocha CLI. We can also automate this using task runner features from tools like Grunt, Gulp, Webpack, or npm scripts. In this course, we will use the common npm test script setup to run our tests. We will also use the Babel compiler to write our tests with modern JavaScript syntax.

import expect from 'expect';

describe('empty', ()=>{

    it('should work', ()=>{
expect(true ).toEqual(true);
});
});

packjson.js: Install babel also

{
"name": "react-testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha ./src/**/*.spec.js --compilers js:babel-core/register",
"test:watch": "npm test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "6.1.4",
"babel-loader": "6.1.0",
"babel-preset-es2015": "6.1.4",
"babel-preset-react": "6.1.4",
"babel-preset-stage-2": "6.1.2",
"expect": "1.12.2",
"mocha": "2.3.3",
"react-addons-test-utils": "0.14.3",
},
"dependencies": {
"react": "0.14.3",
"react-dom": "0.14.3"
}
}

.babelrc

{
"presets": ["react", "stage-2", "es2015"]
}

[React Testing] Setting up dependencies && Running tests的更多相关文章

  1. 如何使用TDD和React Testing Library构建健壮的React应用程序

    如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...

  2. React Testing All in One

    React Testing All in One React 测试 https://reactjs.org/docs/testing.html jest 26.4 https://jestjs.io/ ...

  3. appium(3)-Running Tests

    Running Tests   Preparing your app for test (iOS) Test apps run on the simulator have to be compiled ...

  4. [Git] Automatically running tests before commits with ghooks

    Wouldn't it be nice if everyone ran the tests before committing code? With ghooks, you can automatic ...

  5. [React Testing] JSX error diffs -- expect-jsx library

    When writing React component tests, it can be hard to decipher the error diffs of broken tests, sinc ...

  6. [React Testing] Intro to Shallow Rendering

    In this lesson, we walk through how to use one of React's Test Utilities (from thereact-addons-test- ...

  7. Running tests on PyCharm using Robot Framework

    问题: I started using PyCharm with the robot framework, but i'm facing an issue. how can i run my test ...

  8. [React Testing] Redux Reducers

    Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we w ...

  9. [React Testing] The Redux Store - Multiple Actions

    When using Redux, we can test that our application state changes are working by testing that dispatc ...

随机推荐

  1. StrokeStart与StrokeEnd动画

    通过修改CAShapeLayer的StrokeStart与StrokeEnd的值来实现画图动画 效果图: 代码部分: #import "ViewController.h" @int ...

  2. c#geckofx文件流下载

    备注:内容仅提供参考. ⒈添加引用:using Gecko; ⒉然后根据自己的情况在某个方法内添加事件: LauncherDialog.Download += new EventHandler< ...

  3. mysql window下tmpdir空间耗尽

    修改my.ini或my-default.ini文件[mysqld]下方配置 tmpdir = D:/tmp

  4. js鼠标右键操作

    一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键: 在JS中设置oncontextmenu='return true'用window.document. ...

  5. 高放的c++学习笔记之类

    类的基本思想是数据抽象和封装1.this 成员函数通过一个名为this的额外隐式参数来访问调用它的对象,当我们调用一个函数的时候,用请求该函数的对象的初始化this. 如果某个类的名字为sale,某个 ...

  6. QT5-控件-QScrollArea-可以用于把一个窗口分割为多个-比如根据图片大小显示滚动条

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #incl ...

  7. linux初识-02常用命令

    文件目录操作命令 ls 现实文件和目录列表 ls -l 列出文件的详细信息 ls -a 列出当前目录所有文件 包括隐藏的文件 mkdir 创建目录 -p 父目录不存在的情况下先生成父目录 cd 切换目 ...

  8. Postman 安装及使用入门教程(转)

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  9. php实现冒泡排序

    冒泡排序是非常容易理解和实现,,以从小到大排序举例:设数组长度为N.1.比较相邻的前后二个数据,如果前面数据大于后面的数据,就将二个数据交换.2.这样对数组的第0个数据到N-1个数据进行一次遍历后,最 ...

  10. jquery1.9学习笔记 之选择器(基本元素一)

    所有选择器("*") 描述:选择所有元素 注意:大多数情况下,这个选择器极其的慢,尤其是在作用于自身时. 例子: 查找文档中的每个元素.然后追加一个<script>或& ...