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. Arcgis Android 基本概念 - 浅谈

    MapView     MapView 是 Android 中 ViewGroup的子类,也是 ArcGIS Runtime SDK for Android 中的地图容器,与很多 ArcGIS API ...

  2. raw和字符串的转换。

    hextoraw():十六进制字符串转换为raw: rawtohex():将raw串转换为十六进制: select hextoraw('gggggg') from dual

  3. 运维监控利器smokeping 500报错处理

    检查apache日志发现有以下错误: No such file or directory: exec of '/usr/local/smokeping/htdocs/smokeping.cgi' fa ...

  4. oracle 数据库开发面试题,当时笔试的时候一个没做出来,现附原题及答案

    1. ID123567810111215 表名tt,用sql找出ID列中不连续的ID,例如其中没有的4: --创建表及数据 CREATE TABLE tt(ID INTEGER); INSERT IN ...

  5. C/C++中的&&和||运算符

    代码: #include <iostream> #include <cstdio> using namespace std; int main(){ ,b=,c=-,d=; d ...

  6. osg三维重建的两种方法剖析:三角面片(osgUtil::DelaunayTriangulator)和四角面片(osg::HeightField) (2)

    // perform very basic sanity-check validation on a heightfield.bool validateHeightField(osg::HeightF ...

  7. CSS的力量

    CSS(Cascading Style Sheet)级联样式表,是一种美观网页设计的解决方案,也是W3C推荐的标准,他可以是我们的网页设计更灵活,更美观,使设计人员对内容的设计和样式的设计分离,使设计 ...

  8. 1.3.4 try-with-resources (TWR)

    其基本设想是把资源(比如文件或类似的东西)的作用域限定在代码块内,当程序离开这个代码块时,资源会被自动关闭: 要确保try-with-resources生效,正确的用法是为各个资源声明独立变量: 目前 ...

  9. jQuery 分割按钮(Split Button)

    代码写多了,有些使用过的方法和技巧会一时半会想不起来,平日记录下来,方便自己和有需要的人日后查阅. <html> <head> <style type="tex ...

  10. 【Ecstore2.0】导出问题解决(未导出或导出文件为0字节)

    如果导出队列能成功执行(队列不执行看这里)但是并未生成文件,那么原因大部份可能是出在FTP上. ECSTORE2.0采用了PHP的FTP模块,所以先确认你的环境是否安装了FTP模块,如果没有,安装并在 ...