[React Testing] Setting up dependencies && Running tests
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的更多相关文章
- 如何使用TDD和React Testing Library构建健壮的React应用程序
如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...
- React Testing All in One
React Testing All in One React 测试 https://reactjs.org/docs/testing.html jest 26.4 https://jestjs.io/ ...
- appium(3)-Running Tests
Running Tests Preparing your app for test (iOS) Test apps run on the simulator have to be compiled ...
- [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 ...
- [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 ...
- [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- ...
- 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 ...
- [React Testing] Redux Reducers
Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we w ...
- [React Testing] The Redux Store - Multiple Actions
When using Redux, we can test that our application state changes are working by testing that dispatc ...
随机推荐
- css 实现文字过长变成省略号(包含单行的and多行的)
单行的比较简单 但是必须条件同时满足 <DIV STYLE="width: 120px; height: 50px; border: 1px solid blue;overflow: ...
- DNS负载均衡
1)DNS负载均衡的介绍 对于负载均衡的一个典型应用就是DNS负载均衡.庞大的网络地址和网络域名绝对是负载均衡体现优势的地方.那么它的具体原理是如何的呢?本文就将为大家详细介绍一下相关内容. DNS负 ...
- 如何分析apache日志[access_log(访问日志)和error_log(错误日志)]
如何分析apache日志[access_log(访问日志)和error_log(错误日志)] 发布时间: 2013-12-17 浏览次数:205 分类: 服务器 默认Apache运行会access_l ...
- ubuntu学习笔记--不断更新中
1.rpm软件包相关: rpm软件包安装命令: rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm rpm软件默认安装路径查询: rpm -ql *.rpm ubuntu如何 ...
- JS中window.showModalDialog()详解 HTML DOM open() 方法
window.showModalDialog()方法用来创建一个显示HTML内容的模态对话框. window.showModelessDialog()方法用来创建一个显示HTML内容的非模态对话框. ...
- Spring4.0学习笔记(2) —— 自动装配
Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...
- JQUERY1.9学习笔记 之基本过滤器(四) 首元素选择器
首元素选择器:jQuery( ":first" ) <!DOCTYPE html><html lang="zh-cn"><head ...
- Linux启动网卡时出现RTNETLINK answers: File exists错误解决方法
这里说一下,如果复制了虚拟机,设置新的MAC地址为什么? 在虚拟机的网络设置中--->高级.然后找到如下窗口,生成新的MAC地址即可. ----------------------------- ...
- iOS推送 再备
这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...
- Android 把从网络获取的图片缓存到内存中
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...