knee-cola / jest-mock-axios

Axios mock for Jest
252 stars 42 forks source link

mockError object fails instanceof test #14

Closed j-mew-s closed 5 years ago

j-mew-s commented 6 years ago

I have some code in an axios interceptor which returns an instance of a specific error in some error conditions. I have some code to test which tests for this error using instanceof.

I want to use jest-mock-axios to mock out this error condition and return this error object, but the implementation for mockError uses object.assign({}, error) which results in the instanceof test failing.

I was wondering if you could explain what the object.assign call achieves over just passing the supplied object through? Sorry, I'm fairly new to JavaScript - I'm sure I'll learn something by asking the question one way or another!

paulsherwinang commented 5 years ago

I think Im experiencing the same issue, I need the mockError call to pass the instanceof test and due to the Object.assign logic, it doesn't, is there any way we can have the function pass down the error object directly and not have it assigned?

kingjan1999 commented 5 years ago

Hi,

frankly, I can't answer why we make a copy of the object with Object.assign, which of course has the side effect described. I will have a look at it again on occasion (probably at the weekend) and then either pass it directly or replace it with a better clone mechanism.