2 years ago
#71027
karns
Vuejs testing library - how to write a test to handle axios request in "mounted" hook
I have a vue component that fetches data from an api in the "created" function. I'm just getting up and running using the following testing stack:
"devDependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/vue": "^5.8.2",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^27.4.6",
"jest": "^27.4.7",
"vue-jest": "^3.0.7",
"webpack-dev-server": "^3.11.0"
}
My current test:
import BatchCalendar from '../src/components/batch_calendar.vue';
import { render } from '@testing-library/vue';
test('It renders correctly and loads initial data', done => {
const wrapper = await render(BatchCalendar);
getByText('Calendar Notices');
});
I know that vue testing library abstracts some methods away from the core vue testing utilities, so I'm left with using the "render" method of vue testing library.
My question is how should I go about handling the API call in the vue created hook.
- Should all api calls (and also async methods?) be mocked?
- How/where do I place await/async functions?
- If I have a plugin (fullcalendar) that is making API requests on its own when rendering, how would I go about mocking/handling that?
Let me know if you need more info, thanks.
vue.js
jestjs
vue-test-utils
vue-testing-library
0 Answers
Your Answer