I am having trouble figuring out how to mock an async method that is called from the mounted
lifecycle method. I am trying to mock UsersApi.getAllUsers()
.
//code.vue
mounted: async function () {
await UsersApi.getAllUsers().then(response => {
this.options = response.data
})
}
//code.test.js
import UsersApi from '@/api/users'
…
jest.mock('UsersApi')
UsersApi.getAllUsers.mockResolvedValue([
{
user_id: 123,
first_name: 'Abc',
last_name: 'Def'
}
])
…
Output: Cannot find module 'UsersApi' from 'code.spec.js'
The module exists and is being used in the component. Perhaps using a mock is not appropriate for this purpose?
Another option would be to somehow suppress the mounted method, which would work because I am setting this.options
from the test anyway.
Edit: this is where getAllUsers
is coming from.
//users.js
export default {
async getAllUsers() {
…
},
async getUsers(payload) {
…
}
…