javascript - Jest "Cannot log after tests are don

我在 Vue 框架中使用 jest 来编写联合测试。我的示例测试通过了,但我在记录请求时遇到错误。我该如何解决这个问题?我是在开 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)

最佳答案

我不是 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

运行你的测试

关于javascript - Jest "Cannot log after tests are done. Did you forget to wait for something async in your test?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68504020/

相关文章:

reactjs - React Signalr 访问状态变量

amazon-s3 - 从 Lambda 函数生成预签名 S3 URL 时为 "Access key

c++ - 什么是 list 文件?

vue.js - 如何配置 Vite 来解析嵌套的 SFC CSS url

android - 尝试在真实手机上启动应用程序时,Expo 卡在无休止的加载屏幕上

html - 如何强制 Chrome 在网站上显示最新的图像更改

javascript - Cypress :找不到模块:错误:无法解析 'cypress-wait-

react-native - 升级到 npm7 但库中断太多

rust - 为什么 Iterator::filter 方法接受一个可变引用作为 self?

python - 使用 gunicorn 和 Nginx 部署时,Django 应用程序未在生产日志