Say I've got an expressjs app, app, setup and an endpoint like so:
app.get('/endpoint', endPointFunction)
The function is setup like this:
const axios = require('axios');
var endpointFunction = async(req, res) =>{
try{
const a = await get5LatestAnime();
console.log(a.titles);
const b = await get5LatestManga();
console.log(b.titles);
res.status(200).json({watch: a.titles, read:b.titles});
}catch(err){
console.log(err);
res.status(400).json({err:err.message});
}
};
async function get5LatestAnime(ballot){
const { data } = await axios.get('https://anime.website.com/');
return data;
}
async function get5LatestManga(confirmationNumber){
const { data } = await axios.get(`https://manga.website.com/`);
return data;
}
So let's say this all prints/works when you run it, but let's say you want to run some unit tests stubbing out ONLY that first axios request.
describe('Moxios', () => {
beforeEach(() => {
moxios.install();
moxios.stubRequest('https://anime.website.com', {
status: 200,
response: [{titles:[
"Naruto", "Bleach", "Fate/Stay Night", "Death Note", "Code Geass"]}]
});
});
afterEach(() => {
moxios.uninstall()
});
it('should return a 200 and confirmation status', function () {
return request(app)
.post('/endpoint')
.expect(200, {watch: [
"Naruto", "Bleach", "Fate/Stay Night", "Death Note", "Code Geass"], read: [...titles from the manga website]})
});
});
});
In a similar scenario (of code that I can't post) what happens is moxios stubs the request correctly but other axios requests have a timeout error regardless of how long I allow the timeout to go on for. (Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
).
If I don't use use moxios (if I comment out moxios related stuff) and I test just the function that's having the timeout everything but what the endpoint that needs requests stubbed works.
Does anyone have any clue how to fix this?