Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using an object with "response" on replyWithError the output always returns undefined #2243

Open
Hugo01 opened this issue Nov 6, 2021 · 10 comments
Labels
awaiting response support General questions or support.

Comments

@Hugo01
Copy link

Hugo01 commented Nov 6, 2021

What is the expected behavior?
When using "nock('url').replyWithError({response: 'error"})"
console.log(error.response) should print ("error")

What is the actual behavior?
error.response is undefined

How to reproduce the issue

  1. Create a function
const function = () => {
  try {
    ....
  } catch (err) {
    console.log(err.status)
    console.log(err.response)
  }
}
  1. Create a test using that function
it('Test function', async () => {
    const wrapper = mount(Login)
    const { vm } = wrapper
    nock('http://localhost:3000')
      .post('/auth/login')
      .replyWithError({
        response: 'error',
        status: 400
      })
     await expect(vm.function()).rejects.toThrowError('error')
  })

 console.log(err.response) from the component above outputs "undefined" instead of "error"
 console.log(err.status) works correctly, the ouput shows "400"

Versions

Software Version(s)
Nock ^13.1.4
Node >= 12.22.1
@gr2m
Copy link
Member

gr2m commented Nov 8, 2021

could you share a code example with all code needed to run it? Here is a great example: https://runkit.com/fluggo/nock-nock-2197

@kmcdon10
Copy link

I just ran into this issue as well, but then I figured out that if you set the value of the response to contain an object with metadata, it was no longer an issue.

it('Test function', async () => {
    const wrapper = mount(Login)
    const { vm } = wrapper
    nock('http://localhost:3000')
      .post('/auth/login')
      .replyWithError({
        status: 400,
        response: { data: { detail: { error_reason: 'insert issue here' } } }
      })
  })

 console.log(err.response) is no longer undefined

@antowkabond
Copy link

antowkabond commented Oct 4, 2022

I have the same problem

@kmcdon10 is it working for you? Which version are you using?

@tduyng
Copy link

tduyng commented Dec 30, 2022

There is no update on this? I have same problem too.

@mikicho
Copy link
Contributor

mikicho commented Feb 10, 2024

This test passes. Do you still have this problem?
image

@mille-printemps
Copy link

@mikicho Would it be possible for you to tell us which version of nock and node you are using?

I had the same issue described here, and I am wondering what makes these differences.

@mikicho
Copy link
Contributor

mikicho commented Apr 4, 2024

@mille-printemps probably Node 20 and nock latest

@mille-printemps
Copy link

@mikicho
Thank you for the reply. Eventually, I found a workaround of this issue in my test case. I appreciate your response.

@mikicho
Copy link
Contributor

mikicho commented Apr 6, 2024

@mille-printemps What is the workaround? maybe it will help to understand the root cause.

@mille-printemps
Copy link

@mikicho
Okay, I am not really sure if this is helpful. I will try to explain my case.

I was using superagent as an http client and tried to mock it using nock. It seems that superagent throws ResponseError when the HTTP status is 4xx and 5xx. The definition of ResponseError is here.

In order to check if the object caught in catch clause is ResponseError, I implemented a type guard and I also needed to check if response and response.body in ResponseError were undefined in my case.

In order to test this, I tried to use replyWithError() with a parameter like {status: 400, response: {body: {...}}}. However, response was always undefined and that was the issue.

It turned out that I used reply() like reply(400, {...}) and it solved my issue. So I found that the data passed to replyWithError() as response.body was the same as the data passed to reply() as the second argument.

HTH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response support General questions or support.
Projects
None yet
Development

No branches or pull requests

7 participants