jeudi 30 juin 2016

Having trouble Jest testing ajax modules with axios

I have a module called ajax.js that exports a bunch of ajax functions for me. I'm trying to test the following function with Jest:

export const status = ({loginUrl='/', logoutUrl='/'}) => {
  return axios.get(`/auth?login=${loginUrl}&logout=${logoutUrl}`);
};

My test looks like this:

jest.unmock('../ajax');

import { status } from '../ajax';

describe('ajax', () => {
  let urls;

  beforeEach(() => {
    urls = {
      loginUrl: '/',
      logoutUrl: '/'
    };
  });

  it('calls status with a login and logout url', () => {
    const { loginUrl, logoutUrl } = urls;

    status(urls);
    expect(axios.get).toBeCalledWith(`/auth?login=${loginUrl}&logout=${logoutUrl}`);
  });
});

I keep getting a runtime error in my test:

runtime jest error

If I unmock axios, the error goes away, but that doesn't help me because I want axios to be mocked!

Also, the error appears to be happening because of this line:

import { status } from '../ajax';

Why is Jest struggling with axios so much. Why is simply importing my status function causing this error? Am I doing something wrong here?

Aucun commentaire:

Enregistrer un commentaire