简体   繁体   中英

Testing console.log in catch block

I have the following code:

loggerManager.js:

export default log(message) {
  try {
    axios.post(...).catch(error) { console.log(error); }
  }catch(error) {
    console.log(error);
  }
}

loggerManager.test.js:

test('', () => {
  jest.spyOn(global.console, 'log');
  jest.mock('axios');
  axios.post = jest.fn(() => Promise.reject('fail'));
  log('message');
  expect(console.log).toBeCalledWith('fail'); // The result said it has not been called even once
});

Where am I going wrong?

Two issues:

  1. A rejection handler converts rejection into fulfillment if it doesn't throw or return a promise that is rejected

  2. try / catch doesn't catch promise rejections except when the promise is consumed via await within the try block

So assuming the normal case, if your axios.post 's promise rejects, the rejection handler runs but the catch block does not.

In this code:

export default log(message) {
  try {
    axios.post(...).catch(error) { console.log(error); }
  }catch(error) {
    console.log(error);
  }
}

Execution will only go into the catch block at the end if:

  • axios is an undeclared identifier; or
  • axios doesn't have a post property; or
  • axios.post isn't a function; or
  • axios.post throws when called; or
  • axios.post doesn't return an object with a catch property; or
  • The catch property of the object axios.post returns isn't a function; or
  • The catch method of the object axios.post returns throws when called

It will not be entered if axios.post returns a promise that is rejected.

You may have wanted this:

export default async log(message) {
// −−−−−−−−−−−−^^^^^
  try {
    await axios.post(...);
// −^^^^^
  }catch(error) {
    console.log(error);
  }
}

That way, the catch block will be entered if there was a problem calling axios.post or if axios.post returns a promise that is rejected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM