ava 类似jest snapshot 功能试用
ava也提供了类似jest 的snapshot 测试,可以用来方便的测试web 组件,以下是一个简单的试用,
同时包含了自己碰到问题,以及解决方法,以及一些参考链接
使用typescript 以及tsx,测试react 组件
环境准备
- 项目结构
├── package.json
├── src
│ ├── app.ts
│ ├── hello-component.tsx
│ └── userlogin.tsx
├── tests
│ ├── app.js
│ ├── hello-component.js
│ └── userlogin.js
├── tsconfig.json
└── yarn.lock
- 代码说明
src 为typescript 代码,tests 为集成ava 测试
package.json 包含ava 的一些配置,项目依赖,以及npm script
{
"name": "@rongfengliang/ava-test-learning",
"version": "1.0.1",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@babel/polyfill": "^7.6.0",
"@babel/preset-react": "^7.6.3",
"@babel/register": "^7.6.2",
"@types/react": "^16.9.11",
"ava": "^2.4.0",
"react": "^16.11.0",
"react-test-renderer": "^16.11.0",
"typescript": "^3.6.4",
"zen-observable": "^0.8.14"
},
"ava": {
"require": [
"@babel/register",
"@babel/polyfill"
]
},
"scripts": {
"test:live": "ava -v -w",
"test": "ava -v",
"build:live": "tsc --watch"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
}
tsconfig.json 文件定义
{
"compilerOptions": {
"target": "es5",
"module": "umd",
"declaration": true,
"outDir": "libs",
"rootDir": "src",
"jsx": "react",
"removeComments": false,
"strict": true,
"moduleResolution": "node"
}
}
src/app.js 可选 ,一个简单的module
// define user entity
let user = {
/** user name */
name:"dalong",
/** user age */
age:33
}
// for export default
export default {
user
}
export {
user
}
src/hello-component.tsx 一个测试组件
import React = require("react");
const HelloWorld = () => <h1>Hello World...!</h1>;
export default HelloWorld;
src/userlogin.tsx 另外一个测试组件
import React = require("react");
/**
* loginForm component
*/
const LoginForm = ()=>
<div>
<h1>userename </h1>
<input placeholder="please input your name" />
<h1>pasword</h1>
</div>
export default LoginForm;
tests/hello-component.js snapshot 测试,userlogin.js 类似
babel 配置,为了简单使用6的格式,进行react 的处理,vue 也类似的
{
"presets": ["@babel/preset-react"]
}
启动&&测试
- 启动实时编译
yarn build:live
- 启动测试
yarn test:live
- 效果

修改组件,查看snapshot
src/hello-component.tsx
import React = require("react");
const HelloWorld = () => <h1>dalong World...!</h1>;
export default HelloWorld;

几个问题
- 新版本的变动
新版本为了支持babel 7 ava 做了好多修改
比如package.json 中关于ava 的配置以及依赖的preset
"ava": {
"require": [
"@babel/register",
"@babel/polyfill"
]
}
- regeneratorRuntime is not defined
添加@babel/polyfill
说明
以上是一个简单的试用,还是得多看看github issue
参考资料
https://github.com/avajs/ava/issues/1640
https://github.com/avajs/ava/issues/1968
https://github.com/avajs/ava/issues/685
https://github.com/rongfengliang/ava-test-learning
ava 类似jest snapshot 功能试用的更多相关文章
- Jquery打造的类似新浪微博@提醒功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python面向对象进阶 反射 单例模式 以及python实现类似java接口功能
本篇将详细介绍Python 类的成员.成员修饰符.类的特殊成员. 类的成员 类的成员可以分为三大类:字段.方法和特性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存 ...
- HBase Snapshot功能介绍
HBase在0.94之后提供了Snapshot功能,一个snapshot其实就是一组metadata信息的集合,它可以让管理员将表恢复到以前的一个状态.snapshot并不是一份拷贝,它只是一个文件名 ...
- ios实现类似魔兽小地图功能 在
写了一个类似魔兽小地图功能的控件. 比如你有一个可以放大缩小的scrollView.会在里面进行一些放大缩小,点击里面的按钮呀,等操作. 这个小地图控件.就会和你的大scrollView同步.并有缩略 ...
- js/jQuery实现类似百度搜索功能
一.页面代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. ...
- pandas的筛选功能,跟excel的筛选功能类似,但是功能更强大。
Select rows from a DataFrame based on values in a column -pandas 筛选 https://stackoverflow.com/questi ...
- vue 使用watch监听实现类似百度搜索功能
watch监听方法,watch可以监听多个变量,具体使用方法看代码: HTML: <!doctype html> <html lang="en"> < ...
- 一个类似indexOf()的功能的函数
之前面试的时候遇到了这样的一道题,不过写的时候有些细节没注意到,现在重新写了一下. 写一个类似indexOf()的功能的函数 var str = "dafdfgvdahjfbhyuyvtur ...
- AVD Snapshot功能
写程序的时候,经常会碰到:The application has stopped unexpectly… 有时候,会想对原来软件增加新功能或者修改bug.在eclipse修改后保存代码(注意,要保存所 ...
随机推荐
- SAP: Smartform中存在渐变色图片的上传失真问题的解决
下载GIMP编辑软件,导入图像选择Image->Mode->Indexed 设置Color dithering然后通过File->Export as导出bmp文件.如果上传后不好用请 ...
- QT之类型转换
Qt在进行数据类型转换时,容易忘记如何使用,或者是早已厌倦了百度QString转QByteArray,QByteArray转char,QString转string....... 现在分享一篇QT数据类 ...
- English--动名词
English|动名词 开始动名词的学习,代表着在长难句的征途上又向前迈出了一步. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我感情色彩. 情感:用 ...
- MIME类型对应表:
MIME类型对应表: 常用MIME类型: 扩展名 MIME类型 .iso ISO File .rar application/x-rar-compressed .zip application/zip ...
- Codeforces Round #584 (Div. 1 + Div. 2)
Contest Page A sol 每次选最小的,然后把它的所有倍数都删掉. #include<bits/stdc++.h> using namespace std; int read( ...
- Python开发【第十五篇】模块的导入
的导入语句 import 语句 语法: import 模块名1 [as 模块别名] 作用: 将某模块整体导入到当前模块 示例: import math import sys,os 用法: 模块名.属性 ...
- mysql提示The server quit without updating PID file /usr/local/mysql/data/localhost.localdomain.pid
chown -R mysql:mysql /var/lib/mysql 解决方法 :给予权限,执行 “chown -R mysql:mysql /var/lib/mysql” “chmod -R 75 ...
- Maven:repositories、distributionManagement、pluginRepositories中repository的区别
本文链接:https://blog.csdn.net/netyeaxi/article/details/95804076 目录 一.repositories中的repository 二.distrib ...
- WCF学习笔记(一)---我的第一个WCF程序
一.创建WCF程序 1.创建一个控制台程序(WCFBlog) 2.添加wcf项目 3.将默认的IService1和Service1改成自己的名字 4.在ICalculateServic ...
- 配置 Log4j 以便查看 MyBatis 操作数据库的过程
在 src/main/resources中添加 log4j.properties 配置文件 ,输入如下内 容.#全局面已直 log4j .rootLogger=ERROR, stdout #MyBat ...