开玩笑“测试完成后无法登录。您是否忘记等待测试中的异步内容?”

时间:2021-07-23 19:04:56

标签: javascript vue.js unit-testing jestjs

我在 Vue 框架中使用 jest 来编写联合测试。我的示例测试正在通过,但我在记录请求时出错。我该如何解决这个问题?我在开玩笑中错误地使用了异步吗?测试的副本在下方,错误在下方。

测试

describe('Home', () => {
    test('Example Test', async () => {
        // Arrange - Mounts the component so we can test it. And chooses what we are going to test in the component
        const wrapper = mount(Home2)
        // Act - Does something to the component

        // Assert - Checks to see if what we did to the component matches what we want it to do
        expect(wrapper.html()).toContain(0)
    })
})

测试通过时出错

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.324 s
Ran all test suites.

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "Get Error:  Error: Network Error
        at createError (/home/user/Documents/Code/RRMEC/src/frontend/node_modules/axios/lib/core/createError.js:16:15)
        at XMLHttpRequest.handleError (/home/user/Documents/Code/RRMEC/src/frontend/node_modules/axios/lib/adapters/xhr.js:84:14)

1 个答案:

答案 0 :(得分:0)

我不是 100% 确定为什么。但切换到 shallowMount 修复了错误。

从 vue 测试工具中添加 shallowMount

import {mount, shallowMount} from '@vue/test-utils';
import Home2 from '@/views/Home2';

然后将 mount 更改为 shallowMount

describe('Home', () => {
    test('Example Test', async () => {
        // Arrange - Mounts the component so we can test it. And chooses what we are going to test in the component
        const wrapper = shallowMount(Home2)
        // Act - Does something to the component

        // Assert - Checks to see if what we did to the component matches what we want it to do
        expect(wrapper.html()).toContain(0)
    })
})

您也可以通过运行 --silent 来关闭日志记录。因此,使用命令 yarn:test --silent

运行您的测试