Trying to test axios
calls and trying the moxios
package.
"axios": "^0.16.2",
"moxios": "^0.4.0",
Found here: https://github.com/axios/moxios
Following there example, but my test errors out on the moxios.install()
line:
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'
describe('mocking axios requests', function () {
describe('across entire suite', function () {
beforeEach(function () {
// import and pass your custom axios instance to this method
moxios.install()
})
import axios from 'axios';
import moxios from 'moxios';
import sinon from 'sinon';
import { equal } from 'assert';
const akamaiData = {
name: 'akamai'
};
describe('mocking axios requests', () => {
describe('across entire suite', () => {
beforeEach(() => {
// import and pass your custom axios instance to this method
moxios.install();
});
afterEach(() => {
// import and pass your custom axios instance to this method
moxios.uninstall();
});
it('should stub requests', (done) => {
moxios.stubRequest('/akamai', {
status: 200,
response: {
name: 'akamai'
}
});
// const onFulfilled = sinon.spy();
// axios.get('/akamai').then(onFulfilled);
//
// moxios.wait(() => {
// equal(onFulfilled.getCall(0).args[0], akamaiData);
// done();
// });
});
});
});
I did find this closed issue here, however the fix "passing axios
into the moxios.install(axios)
function did not work"
Turns out I did not need moxios
, in my test I did not want to make an actual API call... just needed to make sure that function was called. Fixed it with a test function.
import { makeRequest } from 'utils/services';
import { getImages } from './akamai';
global.console = { error: jest.fn() };
jest.mock('utils/services', () => ({
makeRequest: jest.fn(() => Promise.resolve({ data: { foo: 'bar' } }))
}));
describe('Akamai getImages', () => {
it('should make a request when we get images', () => {
getImages();
expect(makeRequest).toHaveBeenCalledWith('/akamai', 'GET');
});
});