首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jest“测试完成后无法登录。您忘记在测试中等待异步的东西了吗?”

Jest“测试完成后无法登录。您忘记在测试中等待异步的东西了吗?”
EN

Stack Overflow用户
提问于 2021-07-23 19:04:56
回答 1查看 7.1K关注 0票数 2

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

测试

代码语言:javascript
运行
复制
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)
    })
})

错误与测试通过

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-27 17:08:36

我不能百分之百确定原因。但是切换到shallowMount修复了错误。

从vue测试实用程序中添加shallowMount

代码语言:javascript
运行
复制
import {mount, shallowMount} from '@vue/test-utils';
import Home2 from '@/views/Home2';

然后将mount更改为shallowMount

代码语言:javascript
运行
复制
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运行您的测试

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68504020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档