我正在用jest与Vue框架,编写联合测试。我的示例测试正在通过,但是我记录请求时出错了。我该如何解决这个问题?我是不是在开玩笑中不正确地使用异步?下面是测试的副本,下面是错误。
测试
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)
发布于 2021-07-27 17:08:36
我不能百分之百确定原因。但是切换到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
运行您的测试
https://stackoverflow.com/questions/68504020
复制相似问题