美文网首页
05|异步代码测试方法

05|异步代码测试方法

作者: 雪燃归来 | 来源:发表于2020-05-22 17:22 被阅读0次

1、示例一(返回值和返回promise的处理)

import axios from 'axios'
export const fetchData = (fn) => {
    axios.get("http://www.dell-lee.com/react/api/demo.json").then(response => {
        fn(response.data)
    })
}


import { fetchData } from './fetchData'
test('fetchData 返回结果为 {success: true}', (done) => {
    fetchData((data) => {
        expect(data).toEqual({
            success: true
        })
        done();
    })
})

2、示例二(正常处理和异常处理)

import axios from 'axios'
export const fetchData = () => {
    return axios.get("http://www.dell-lee.com/react/api/demo.json");
}

import { fetchData } from './fetchData'
test('fetchData 返回结果为 {success: true }', () => {
    return fetchData().then((response) => {
        expect(response.data).toEqual({
            success: true
        })
    })
})

test('fetchData 返回结果为 {success: true}', () => {
    expect.assertions(1)
    return fetchData().catch(e => {
        expect(e.toString().indexOf('404') > -1).toBe(true)
    })
})

3、示例三(resolves/rejects)

import axios from 'axios'
export const fetchData = () => {
    return axios.get("http://www.dell-lee.com/react/api/demo.json")
}

import { fetchData } from './fetchData'
test('fetchData 返回结果为 {success: true}', () => {
    return expect(fetchData()).resolves.toMatchObject({
        data: {
            success: true
        }
    })
})

import { fetchData } from './fetchData'
test('fetchData 返回结果为 404', () => {
    return expect(fetchData()).rejects.toThrow();
})

4、示例四(async/await)

import axios from 'axios'
export const fetchData = () => {
    return axios.get("http://www.dell-lee.com/react/api/demo.json")
}

import { fetchData } from './fetchData'
test('fetchData 返回结果为 {success: true}', async () => {
    await expect(fetchData()).resolves.toMatchObject({
        data:{
            success: true
        }
    })
})

test('fetchData 返回的结果 404', async () => {
    await expect(fetchData()).rejects.toThrow()
})

5、示例五(async/await 异步求值)

import axios from 'axios'
export const fetchData = () => {
    return axios.get("http://www.dell-lee.com/react/api/demo.json")
}

import { fetchData } from './fetchData'
test('fetchData 返回结果为{ success: true}', async () => {
    const response = await fetchData();
    expect(response.data).toEqual({
        success: true
    })
})

test('fetchData 返回结果为 404', async () => {
    expect.assertions(1);
    try {
        await fetchData()
    } catch (e) {
        expect(e.toString().indexOf('404') > -1).toBe(true)
    }
})

相关文章

  • 05|异步代码测试方法

    1、示例一(返回值和返回promise的处理) 2、示例二(正常处理和异常处理) 3、示例三(resolves/r...

  • Jest测试异步代码

    JavaScript中经常有异步运行的代码。如果你要测试异步的代码,Jest需要知道他测试的代码是否已经完成异步动...

  • 异步代码测试

    使用 Mocha 在测试异步函数的时候非常简单,只需要在回调结束的时候手动调用一下回调函数即可。向 it() 中添...

  • iOS单元测试常用的宏和测试方法

    单元测试常用的宏和测试方法 异步测试方法(定时器测试)

  • iOS 单元测试--异步测试

    单元测试分为3种: 逻辑测试:测试逻辑方法 异步测试:测试耗时方法(用来测试包含多线程的方法) 性能测试:测试某一...

  • iOS 单元测试--性能测试

    单元测试分为3种: 逻辑测试:测试逻辑方法 异步测试:测试耗时方法(用来测试包含多线程的方法) 性能测试:测试某一...

  • iOS 单元测试--逻辑测试

    单元测试分为3种: 逻辑测试:测试逻辑方法 异步测试:测试耗时方法(用来测试包含多线程的方法) 性能测试:测试某一...

  • Promise.all 方法的实现

    promise.all解决异步并发问题的,允许我们按照异步代码的调用顺序得到异步代码的执行结果。all方法是静态方法,

  • Jest - 测试异步代码

    代码异步运行在 JavaScript 里很常见。当你有异步代码运行时,Jest 需要知道它正在测试的代码何时完成,...

  • 测试异步方法

    深切体会到,测试异步方法,是整个单元测试的难点和重点,为什么这么说呢?问题很明显,当测试方法跑完了的时候,被测的异...

网友评论

      本文标题:05|异步代码测试方法

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