jeudi 28 décembre 2017

Jest mock function's constants doesnt work

i have little experience with jest as i am trying to change variable in my function as we expect to have error thrown if the function's inner variable(baseUrl) changed to null

my function :

buildUrl.js

  export function buildUrl(contentId, data, options = {}) {
  let baseUrl = config.has('imgBaseUrl') && config.get('imgBaseUrl');
  if (!baseUrl) {
    throw new Error('some error');
  }
...

i need to mock the baseUrl to value of null for example and test it

buildUrl.test.js

import {buildUrl} from "./buildURL";
....
 it('throw an error when "baseUrl" is not configured', () => {

   let mockBaseUrl = {baseUrl: null};
   jest.mock('./buildURL', ()=> mockBaseUrl);
   // jest.mock('../../../config/test', ()=>mockImgBaseUrl); // or mock the config to be used by the function?

   expect(()=> buildUrl('1.44444', data[0], defaultOptions)).toThrow(
     'some error'
   );
   });

another approach using jest.fn() didnt work as expected , maybe i am missing something here...

Aucun commentaire:

Enregistrer un commentaire