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

AxiosError.prototype.toJSON cannot be stringify in JEST #5635

Open
zWingz opened this issue Apr 6, 2023 · 2 comments
Open

AxiosError.prototype.toJSON cannot be stringify in JEST #5635

zWingz opened this issue Apr 6, 2023 · 2 comments
Labels
type::bug Used when reporting a bug

Comments

@zWingz
Copy link

zWingz commented Apr 6, 2023

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 Axios@0.26.x 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

@DavidJDallas DavidJDallas added the type::bug Used when reporting a bug label Sep 5, 2023
@TechWizard9999
Copy link

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

@tohagan
Copy link

tohagan commented Apr 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type::bug Used when reporting a bug
Projects
None yet
Development

No branches or pull requests

4 participants