axios icon indicating copy to clipboard operation
axios copied to clipboard

AxiosError.prototype.toJSON cannot be stringify in JEST

Open zWingz opened this issue 2 years ago • 3 comments

Describe the bug

I get the error when run Jest for test. image

It is because the toJSON function is a property at the prototype of AxiosError https://github.com/axios/axios/blob/v1.x/lib/core/AxiosError.js#L33

and the jest will remove the prototype at https://github.com/facebook/jest/blob/main/packages/jest-util/src/deepCyclicCopy.ts#L31.

so the AxiosError cannot be stringify because the __currentRequest.req is a circular structure

but [email protected] is work fine, because the toJSON is the property of AxiosError https://github.com/axios/axios/blob/v0.26.1/lib/core/enhanceError.js#L13

To Reproduce

  1. make a http request error use axios
  2. run test with --watch flag

Code snippet

No response

Expected behavior

No response

Axios Version

latest

Adapter Version

http

Browser

No response

Browser Version

No response

Node.js Version

16.x

OS

No response

Additional Library Versions

jest@29

Additional context/Screenshots

No response

zWingz avatar Apr 06 '23 09:04 zWingz

😊 "I'd be delighted to work on this issue ! Could you please assign it to me?"

TechWizard9999 avatar Sep 30 '23 20:09 TechWizard9999

I'm seeing a related error with the latest version of axios ...

  • axios v1.6.8
  • axios-retry v4.1.0
  • agentkeepalive v4.5.0
  • Jest v29.7.0
  • Node.js v18.19.1
node:internal/child_process/serialization:159
node:internal/child_process/serialization:159
node:internal/child_process/serialization:159
    const string = JSONStringify(message) + '\n';
                   ^

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'socket' -> object with constructor 'Object'
    --- property '_httpMessage' closes the circle
    at stringify (<anonymous>)
    at writeChannelMessage (node:internal/child_process/serialization:159:20)
    at process.target._send (node:internal/child_process:845:17)
    at process.target.send (node:internal/child_process:745:19)
    at reportSuccess (<project>\node_modules\jest-worker\build\workers\processChild.js:82:11)

I see that axios added a AxiosError.toJSON() method some time ago to fix this issue so it may be a Jest problem.

Here's the related Jest issue

  • https://github.com/jestjs/jest/issues/11958

tohagan avatar Apr 11 '24 03:04 tohagan