5、单元测试 - lenvonsam/vue-jest-test GitHub Wiki

测试单个测试用例

import { helloWorld } from '@/utils/common'

it('hello world func test', () => {
  expect(helloWorld()).toEqual('hello world')
})

测试多个测试用例

import { isEven, isOdd } from '@/utils/common'

describe('judge number test', () => {
  it('isEven number', () => {
    expect(isEven(8)).toBeTruthy()
  })
  it('isOdd number', () => {
    expect(isOdd(9)).toBeTruthy()
  })
  it('is not even number', () => {
    expect(isEven(7)).toBeFalsy()
  })
  it('is not odd number', () => {
    expect(isOdd(6)).toBeFalsy()
  })
})

测试异步测试用例

import { asyncGetNumber } from '@/utils/common'

it('async number func', (done) => {
  asyncGetNumber(5, (result) => {
    expect(result).toBe(15)
    done()
  })
})

测试组件测试用例

<template>
<div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
<!--...-->
</div>
</template>
import helloWorld from '@/components/HelloWorld.vue'
import { mount } from '@vue/test-utils'

describe('hello world contain h2 dom', () => {
  test('render Essential Links dom value', () => {
    const wrapper = mount(helloWorld)
    expect(wrapper.find('h2').text()).toBe('Essential Links')
  })
})

测试API测试用例

import { ironRequest } from '@/utils/common'

describe('iron http get test', () => {
  it('getBanner.html', async () => {
    let { data } = await ironRequest('getBanner.shtml', {}, 'get')
    console.log(data)
    if (data.returncode === '0') {
      expect(typeof data.banners).toBe('object')
    } else {
      expect(data.returncode).toBe('-1')
    }
  })
})
⚠️ **GitHub.com Fallback** ⚠️