[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 ...
随机推荐
- GUID的广泛使用
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- tips [终端]
pbcopy 命令:Place standard output in the clipboard. $ pbcopy < ~/.ssh/id_rsa.pub
- share js 分享代码
(function(){ var $doc = $(document); var shareHandlers = { 'twitter': function(prop,shareUrl){ var D ...
- 解决“Word无法访问您试图使用的功能所在的网络位置”问题
解决“Word无法访问您试图使用的功能所在的网络位置”问题 打开Word时出现现现在的对话框,按取消,又可以打开word文档 按取消时,仍然可以打开word文档.为了解决这个问题,我借助网络,知道这是 ...
- 拿起cl.exe,放下IDE
笔者在这里介绍一种使用cl.exe编译源文件的方法,可以手动执行编译过程而不再依赖IDE,此外,笔者还介绍一些使用cl.exe编译简单源代码的方式. cl.exe是windows平台下的编译连接程序, ...
- win7下搭建nginx+php的开发环境
本来在win7下用的是IIS做web服务器,但近来因项目需求的原因,需要在服务器遇到404错误的时候自动做转向(不是在客户端的跳转,而是在服务器收到客户端请求去某目录下读取文件返回时,如果发现目录或目 ...
- C#中HashTable和快速排序的用法
题目主要是写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来. 自从周四拿到题目以后,发现又要用到万恶的数据结构了,不得不说这是我的短板,所有上周20号到 ...
- iOS开发网络篇—网络请求(HTTP协议)小结
iOS开发网络篇—网络请求(HTTP协议)小结 iOS开发网络篇—网络请求(HTTP协议)小结 1. 聊一下HTTP协议(协议的完整的通信过程) 2.通信过程 1> 请求 * 客户端 --> ...
- codevs 1061 重复子串
题目描述 Description 某电视台在每一个星期天都有一个福利彩票节目,在该节目中有一个考察幸运观众记忆力的节目.节目的安排是这样的:首先由节目主持人说出一串诸如“左1右2左2左3右4左1”的数 ...
- 一次java程序的重构
// com口操作类 package xyz.game; class ComOpera { public void openPort() throws Exception {...} // 打开com ...