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. java使用内部类的好处及其初始化

    java使用内部类的原因 每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响          java内部类初始化 ForeCatal ...

  2. mysql limit性能问题

    offset大的时候的比较 1. SELECT * FROM persons LIMIT 200000,10; 耗时0.109s 2. SELECT *FROM persons WHERE id> ...

  3. C/C++中unsigned char和char的区别

    代码: #include <cstdio> #include <iostream> using namespace std; int main(){ unsigned char ...

  4. M_LROOT,LD_LIBRARY_PATH, “Not all extension Dlls were loaded”问题原因及解决方法(持续更新)

    最近在需要在云主机上进行压力测试,所以需要Linux的Agent. 一.安装:教程可以百度,大概步骤如下: 1.Upload Linux.zip to 指定的机器 2.解压,chmod 777 $Li ...

  5. linux定时任务crond那些事!

    1.定时任务crond介绍 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件. 特殊需求:(秒级别)crond服务就无法搞定了,一般工作中写脚本守护 ...

  6. js 懒加载

    需要的js <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> < ...

  7. error on line 1 at column 6: XML declaration allowed only at the start of the document

    This page contains the following errors: error on line 1 at column 6: XML declaration allowed only a ...

  8. oracle数据泵之解决方案(用户)导入导出。

    看到网上有这样的介绍而且很多,但觉得都是大神才能一下子看的懂.自己总结下菜鸟能看懂的. 1.导出. 首先第一步: 操作系统—开始—运行输入“cmd”进入dos界面输入“sqlplus/nolog”按回 ...

  9. (摘)Chart Y轴设置为百分比

    放置一个Chart控件,未做任何设置:然后编写代码: //设置chart2.Legends[0].Enabled = false;//不显示图例 chart2.ChartAreas[0].BackCo ...

  10. Ubuntu下Git服务端搭建

    1安装git $ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install git 测 ...