jest + express + axios单元测试(node接口单元测试)

装包:
 

yarn add jest

user.test.js:

const { userSum } = require('./user')
const { redisClient } = require('../../redis/index')
const axios = require('axios')
const { Api } = require('../../api/index')

describe('user model tests', () => {
  beforeAll(async () => {})
  afterAll(async () => {
    redisClient.quit()
  })

  it('求和', async () => {
    expect(userSum(1, 2)).toBe(3)
  })

  it('登录', async () => {
    const response = await axios.post(
      'http://localhost:85/api/light/user/login',
      {
        username: 'admin',
        password: '123456'
      }
    )
    expect(response.data.code).toEqual(200)
  })

  it('前台登录-正常登录', async () => {
    const response = await Api.h5.login({
      username: 'admin',
      password: '123456'
    })
    expect(response.code).toEqual(200)
  })  
})

package.json:

  "scripts": {
    "test": "jest"
  }

posted @ 2022-08-29 18:33  徐同保  阅读(7)  评论(0)    收藏  举报  来源