Jest "Cannot log after tests are done. Did you forget to wait for something async in your test?"

Viewed 5629

I am using jest with the Vue framework, to write unite tests. My example test is passing but I am having an error logging the request. How do I fix this issue? Am I using async incorrectly in jest? A copy of the test is below with the error farther down below.

Test

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)
    })
})

Error with test passing

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 Answers

Im not 100% sure why. but switching to shallowMount fixed the error.

Add shallowMount from vue test utils:

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

Then change mount to 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)
    })
})

You can also just turn off logging with by running --silent. So run your test with the command yarn:test --silent

Related