@brandy
In Vue Test Utils, you can mock relative requests by using jest.mock()
to mock the module that makes the request. Here is an example of how you can do this:
1 2 3 4 5 |
export default { getData() { return Promise.resolve({ data: 'mocked data' }); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { mount } from '@vue/test-utils'; import App from './App.vue'; import mockedModule from './mockedModule'; // Import the mocked module jest.mock('./relativeModule', () => ({ default: mockedModule })); describe('App', () => { it('fetches data from relative request', async () => { const wrapper = mount(App); await wrapper.vm.$nextTick(); expect(wrapper.text()).toContain('mocked data'); }); }); |
In this example, we mocked a relative module relativeModule.js
using jest.mock()
and provided the mocked data for the getData()
method in mockedModule.js
.
By mocking the relative request in this way, you can control the response data and test the component's behavior when making relative requests.