I've tried numerous solutions to similar issues, but none of them seem to work. (the comments are my attempted fixes, separated by blank lines. I've been stuck on this problem for nearly a week, focusing on improving the coverage by testing the .then part of the Axios request. I just can't seem to pinpoint where things are going wrong.
code snippet
__ mocks __/axios.js:
export default {
get: jest.fn(() => Promise.resolve({ data: {} })),
post: jest.fn(() => Promise.resolve())
}
getInfo method that needs testing:
getInfo () {
axios.get('/')
.then((response) => {
this.form.sid = response.data.data.basic_info.username
this.form.name = response.data.data.basic_info.name
this.form.tel = response.data.data.basic_info.mobile_number
this.form.email = response.data.data.basic_info.email
return response.data
}).catch(function (error) {
console.log(error)
})
}
test code snippet:
import { shallowMount } from '@vue/test-utils'
import InfoForm from '@/components/InfoForm'
//attempt 1
import axios from 'axios';
jest.mock('axios')
//...
//describe portion is omitted here
it('tests the getInfo method', async () => {
const mockAxiosGet = jest.spyOn(axios, "get")
mockAxiosGet.mockResolvedValue(() =>
Promise.resolve({
data: {
basic_info: {
username: 'aName',
name: 'aNAME',
mobile_number: '12222222222',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6979b979f9ab6939bd895999b">[email protected]</a>'
}
}
})
)
const response = await wrapper.vm.getInfo()
expect(response).toBeDefined() // error message below
})
Error Message:
Ensure that a variable is not undefined.
InfoForm.vue test > test method: getInfo
-----
Error: expect(received).toBeDefined()
Received: undefined Jest
Any assistance would be greatly appreciated!