jsdom: [Bug]: structuredClone is not defined
Basic info:
- Node.js version: 18.0.0
- jsdom version: 19.0.0
Minimal reproduction case
Running a jest test that calls the structuredClone global using the jsdom environment
structuredClone();
I first opened this issue in the jest repo and was instructed to open it here.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 85
- Comments: 22 (1 by maintainers)
Commits related to this issue
- [hack] use @ungap/structured-clone to fix jsdom/jsdom#3363 — committed to brianrodri/obsidian-bujo by brianrodri 9 months ago
- Ensure structuredClone is available to jsdom This is a deficiency in jsdom -- https://github.com/jsdom/jsdom/issues/3363 -- structured clone is well supported. — committed to bitwarden/clients by MGibson1 7 months ago
- Ps/pm 2910/handle switch messaging (#6823) * Handle switch messaging TODO: handle loading state for account switcher * Async updates required for state * Fallback to email for current accoun... — committed to bitwarden/clients by MGibson1 7 months ago
- Ps/pm 2910/handle switch messaging (#6823) * Handle switch messaging TODO: handle loading state for account switcher * Async updates required for state * Fallback to email for current accoun... — committed to bitwarden/clients by MGibson1 7 months ago
- [PM-194] Browser Account Switcher UI (#6772) * Handle switch messaging TODO: handle loading state for account switcher * Async updates required for state * Fallback to email for current acco... — committed to bitwarden/clients by rr-bw 7 months ago
- use native structuredClone on node, cloudflare workers, and in tests (#3166) Currently, we only use native `structuredClone` in the browser, falling back to `JSON.parse(JSON.stringify(...))` elsewher... — committed to tldraw/tldraw by si14 3 months ago
How to force Jest/jsdom to use Node.js
structuredClone
implementation (requires Node.js >= 17.0.0):See also https://github.com/jsdom/jsdom/issues/1724#issuecomment-1446858041, https://github.com/jsdom/jsdom/issues/1721#issuecomment-1484202038
This method is good for objects that can be converted to JSON. This doesn’t work for regular expressions, for example.
A smarter solution seems to be installing a polyfill.
2. In the test file, add the lineThis is suitable if you are using the
jsdom
environment ("testEnvironment": "jsdom"
). For other cases, the exact method may be slightly different, but the basic meaning is the same. This is the simplest one I’ve found. You can also try setting up Babel to automate this.is what worked for me. But is this going to be fixed?
@Aleksandr-JS-Developer
Use
import 'core-js/stable/structured-clone';
since this module has some dependencies.https://twitter.com/slicknet/status/782274190451671040
I can vouch that this is still an issue using
jest-environment-jsdom
v28.1.3@tkrotoff Is there a way to do this without ejecting in create-react-app? I have a few React components that are calling structuredClone and they’re erroring out when I try to render them in my unit tests.
This won’t work for copying functions as values of the keys. For the rest, no problem.
In case anybody needs it, I found out how to do the same in
vitest
:And in the vitest.config.ts file:
Docs: https://vitest.dev/guide/environment#custom-environment
were you able to work this out without ejecting?
structuredClone() has polyfills that can be used.
The solution I chose was:
Install ungap/structured-clone
npm i -D @ungap/structured-clone @types/ungap__structured-clone
Update
jest.config.ts
and import the library and make it available asstructuredClone
:@Aleksandr-JS-Developer No, this is wrong. The core-js polyfill of the structuredClone() method is incomplete in comparison to the native Node.js implementation.
Instead, Node.js has its own built-in structuredClone() method since version 17.0.0. The method can be reflected in the VM context, as introduced in PR https://github.com/jsdom/jsdom/pull/3459.
any update on this?
This is still not working and it affects React app tests as well.