jest 自动化测试

概述

jest 是 facebook 开源的,用来进行单元测试的框架,可以测试 javascipt 和 react。 单元测试各种好处已经被说烂了,这里就不多扯了。重点要说的是,使用 jest, 可以降低写单元测试的难度。

单元测试做得好,能够极大提高软件的质量,加快软件迭代更新的速度, 但是,单元测试也不是银弹,单元测试做得好,并不是测试框架好就行,其实单元测试做的好不好,很大程度上取决于代码写的是否易于测试。 单元测试不仅仅是测试代码,通过编写单元测试 case,也可以反过来重构已有的代码,使之更加容易测试。

jest 的零配置思路是我最喜欢的特性。

安装方式

# yarn
yarn add --dev jest

# OR npm
npm install --save-dev jest

测试方式

随着 node.js 的发展,javascipt 语言的发展,前端可以胜任越来越多的工作,后端很多时候反而沦为数据库的 http API 所以前端不仅仅是测试看得见的页面,还有很多看不见的逻辑需要测试。

jest 提供了非常方便的 API,可以对下面的场景方便的测试

一般函数

对于一般的函数,参考附录中的 jest Expect API 可以很容易的写测试 case 待测试文件:joinPath.js

const separator = '/'
const replace = new RegExp(separator + '{1,}', 'g')
export default (...parts) => parts.join(separator).replace(replace, separator)

测试文件的命名:joinPath.test.js 采用这种命名,jest 会自动找到这个文件来运行

import joinPath from 'utils/joinPath'

test('join path 01', () => {
  const path1 = '/a/b/c'
  const path2 = '/d/e/f'

  expect(joinPath(path1, path2)).toBe('/a/b/c/d/e/f')
})

test('join path 02', () => {
  const path1 = '/a/b/c/'
  const path2 = '/d/e/f'

  expect(joinPath(path1, path2)).toBe('/a/b/c/d/e/f')
})

test('join path 03', () => {
  const path1 = '/a/b/c/'
  const path2 = 'd/e/f'

  expect(joinPath(path1, path2)).toBe('/a/b/c/d/e/f')
})

异步函数

上面的是普通函数,对于异步函数,比如 ajax 请求,测试写法同样容易 待测试文件:utils/client.js

export const get = (url, headers = {}) => {
  return fetch(url, {
    method: 'GET',
    headers: {
      ...getHeaders(),
      ...headers
    }
  }).then(parseResponse)
}

测试文件:client.test.js

import { get } from 'utils/client'

test('fetch by get method', async () => {
  expect.assertions(1)
  // 测试使用了一个免费的在线 JSON API
  const url = 'https://jsonip.com/'

  const data = await get(url)
  const { about } = data
  expect(about).toBe('/about')
})

测试的生命周期

jest 测试提供了一些测试的生命周期 API,可以辅助我们在每个 case 的开始和结束做一些处理。 这样,在进行一些和数据相关的测试时,可以在测试前准备一些数据,在测试后,清理测试数据。

4 个主要的生命周期函数:

  • afterAll(fn, timeout): 当前文件中的所有测试执行完成后执行 fn, 如果 fn 是 promise,jest 会等待 timeout 毫秒,默认 5000
  • afterEach(fn, timeout): 每个 test 执行完后执行 fn,timeout 含义同上
  • beforeAll(fn, timeout): 同 afterAll,不同之处在于在所有测试开始前执行
  • beforeEach(fn, timeout): 同 afterEach,不同之处在于在每个测试开始前执行
BeforeAll(() => {
  console.log('before all tests to excute !')
})

BeforeEach(() => {
  console.log('before each test !')
})

AfterAll(() => {
  console.log('after all tests to excute !')
})

AfterEach(() => {
  console.log('after each test !')
})

Test('test lifecycle 01', () => {
  expect(1 + 2).toBe(3)
})

Test('test lifecycle 03', () => {
  expect(2 + 2).toBe(4)
})

mock

我自己觉得能不 mock,还是尽量不要 mock,很多时候觉得代码不好测试而使用 mock,还不如看看如何重构代码,使之不用 mock 也能测试。 对于某些函数中包含的一些用时过长,或者调用第三方库的地方,而这些地方并不是函数的主要功能, 那么,可以用 mock 来模拟,从而提高测试执行的速度。

对于上面异步函数的例子,我们改造成 mock 的方式:

const funcUseGet = async url => {
  return await get(url)
}

test('mock fetch get method', async () => {
  const client = require('utils/client')
  client.get = jest.fn(url => ({ mock: 'test' }))

  const url = 'http://mock.test'
  const data = await funcUseGet(url)

  expect(data).toEqual({ mock: 'test' })
})

react 测试

jest 可以测试 react component,但是我们用了状态分离的方式开发前端, 大部分功能都在 action 中,这部分测试基本没有做。

附录

posted @ 2018-04-19 23:18  wang_yb  阅读(7221)  评论(1编辑  收藏  举报