I have Stencil component which uses axios.get to get some data from a server (e.g. localhost:3000 during development). Now I have an e2e.ts test which tests this component in conjunction with several other components. I would like to mock the axios.get function to isolate my tests from the server. In a spec.ts test I would mock axios using jest with the following code:
import axios from 'axios';
import {myComponent} from './my-component';
const mock = jest.spyOn(axios, 'get');
test('get data', async () => {
...
mock.mockResolvedValue('hello');
...
});
But this does not work in e2e tests. I have tried installing jest-puppeteer but I cannot find any examples of how you would mock a function with jest mocking API with jest-puppeteer.
Any example code would be greatly appreciated.
P.S. Note: if I use Puppeteer to intercept a request and respond to it I get a "Request is already handled" error. Here's example code:
const page = await newE2EPage();
await page.setRequestInterception(true);
page.on('request', req => {
if(req.url() === 'http://localhost:3000/') {
request.respond({
contentType: 'text/plain',
headers: {'Access-Control-Allow-Origin': '*'},
body: 'hello'
})
}
else {
request.continue({});
}
});
await page.setContent('<my-component></my-component>');