美文网首页
jest 入门笔记

jest 入门笔记

作者: 小生不才_ | 来源:发表于2020-05-24 00:16 被阅读0次

jest 入门笔记

Jest is a delightful JavaScript Testing Framework with a focus on simplicity.
It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more!

失去人性,失去很多;失去兽性,失去一切。 --三体

使用

  1. 新建项目
$ mkdir jestTest && cd ./jestTest
$ npm init -y
  1. 安装jest
# 初始化 jest 配置
$ npx jest --init
# 安装
$ npm install -D jest
  1. *配置babel
$ npm install -D @babel/core @babel/preset-env
$ touch babel.config.js

babel.config.js 内容

module.exports = {
  presets: [
    ["@babel/preset-env", {
      targets: {
        node: 'current'
      }
    }],
  ]
}
  1. 创建文件
$ mkdir src && cd ./src
$ touch index.js index.test.js

匹配器

expect(1 + 2).toBe(3); // 精确匹配
expect({one: 1}).toEqual({ one: 1 }); // 深度匹配
expect(null).toBeNull(); // Null匹配
expect(undefined).toBeUndefined(); // undefined 匹配
expect(var).toBeDefined(); // var是否定义
expect(true).toBeTruthy(); // true 匹配
expect(false).toBeFalsy(); // false 匹配
expect(false).not.toBe(true); // 取反匹配
expect(1).toBeGreaterThan(0); // 大于
expect(1).toBeLessThan(2); // 小于
expect(1).toBeGreaterThanOrEqual(1); // 大于等于
expect(1).toBeLessThanOrEqual(1); // 小于等于
expect(0.1 + 0.2).toBeCloseTo(0.3); // 浮点数
expect('toMatch').toMatch(/to/) // 是否包含
expect([1, 2, 3]).toContain(1) // 是否包含某些元素
expect(() => { throw new Error() }).toThrow(); // 异常匹配

异步

备用函数

function loadData(){
  return new Promise((resolve, reject)=>{
    resolve({
      code: 200,
      data: {}
    });
  })
}
// 通过返回
test('should loadData', async () => {
  // expect.assertions(1); // 必须执行一个expect
  return loadData().then(res => {
    expect(res.code).toBe(200)
  });
});
// 通过调用参数
test('should loadData', (deno) => {
  loadData().then(res => {
    expect(res.code).toBe(200)
    deno();
  });
});
// 通过resolves
test('should loadData', (deno) => {
  return expect(loadData()).resolves.toMatchObject({
    code: 200
  });
});

勾子

// 开始执行一次
beforeAll(() => {
  console.log('befaoreAll');
});
// 每个测试用例都会执行
beforeEach(() => {
  console.log('beforeEach');
});
// 每个测试用例执行后都会执行
afterEach(() => {
  console.log('afterEach');
});
// 勾子执行完毕
afterAll(() => {
  console.log('afterAll');
});

describe('describe', () => {});

test('should xxx', () => {});

mock

const func = jest.fn(()=>{});
func.mockReturnValueOnce('value1')
    .mockReturnValueOnce('value2');
func.mockReturnValue('value');

console.log(func.mock); // mock 返回数据

mock ajax

import ajax from './utils/ajax/ajax'
jest.mock('./utils/ajax/ajax');

test('should mock', async () => {
  ajax.get.mockResolvedValue({ data: { data: [], code: 200 } }); // 模拟数据
  const data = await loadTestData();
  expect(data.data).toEqual([]);
});

相关文章

  • jest 入门笔记

    jest 入门笔记 Jest is a delightful JavaScript Testing Framewo...

  • TS+Jest 最简开始

    Jest[https://jestjs.io/] 是当下最主流的前端测试框架;对于初学者,Jest 配置入门并不算...

  • Jest Mocks入门

    一、介绍 Mocks可以捕获对函数的调用(以下用法一)使用mock function,可以查看函数的调用次数,以及...

  • jest入门(一)

    1安装依赖包: 2修改package.json文件 3.在项目里创建需要测试的文件sum.js 4 创建sum.j...

  • Jest初识

    Jest入门 注意: 下载babel编译器核心文件 npm i @babel/core@7.4.5 @babel/...

  • 001 前端项目中加入jest , 并支持ts

    1 加入 jest ts-jest @types/jest jest 为测试框架tes-jest 用于转换t...

  • Jest前端测试入门

    使用匹配器 精确匹配toBe匹配使用===全等匹配 如果需要检查对象或者数组需要使用toEqual 真值匹配 to...

  • Jest初步入门

    一、语法 expect(实际变量).toBe(期望值);普通匹配器expect({a:1}).toBe({a:1}...

  • vue2 配置jest相关问题

    1、安装 jest, babel-jest,@vue/vue2-jest 2、安装部分插件:jest-raw-lo...

  • Kotlin入门笔记八:Lambda和序列

    Kotlin入门笔记一:方法、变量、类 Kotlin入门笔记二:when、for、in Kotlin入门笔记三:可...

网友评论

      本文标题:jest 入门笔记

      本文链接:https://www.haomeiwen.com/subject/hpnrahtx.html