Code Monkey home page Code Monkey logo

Comments (7)

dethstrobe avatar dethstrobe commented on August 16, 2024 4

I just implemented a workaround...which I've had to do a few times for JSDOM now.

In your jest.config you'll need to point the testEnvironment to local file.

Then you'll need to extends JSDOMEnvironment and add the node implementation for structuredClone to the JSDOM environment in the constructor.

Something like this:

import JSDOMEnvironment from "jest-environment-jsdom"

export default class FixJSDOMEnvironment extends JSDOMEnvironment {
  constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
    super(...args)

    this.global.structuredClone = structuredClone
  }
}

For a real world example, you can see my implementation in my project OMA3.

FixJSDOMEnvironment.ts
jest.config.js

from fakeindexeddb.

jamesgpearce avatar jamesgpearce commented on August 16, 2024 2

Yep, this just broke tests for me (Jest, jsdom, 5.0). Nice workaround though, thanks! Guessing you'll probably get a few more reports of this though.

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on August 16, 2024 2

I added the above workarounds to README.md, I think that is good enough to close this issue. Although feel free to comment if you think I'm wrong!

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on August 16, 2024 1

Hm I didn't realize that about jsdom. I figured since it's in all the browsers and Node.js, then it'd be everywhere.

Annoying jsdom users is bad. But it's also nice to have no dependencies.

I wonder how many people are using this with jsdom vs without, that would make it more clear what the best decision is. Maybe for now I will leave this issue open and see if other people are affected.

And to save people the trouble of reading the jsdom issue for workarounds, seems that you can just add this above your fake-indexeddb import:

import 'core-js/stable/structured-clone';

from fakeindexeddb.

j-mew-s avatar j-mew-s commented on August 16, 2024

This broke tests for us

from fakeindexeddb.

jamesgpearce avatar jamesgpearce commented on August 16, 2024

By the way this now seems to mean that objects returned are null-prototyped, which I don't think the real IndexedDB objects technically are. No big deal unless (like me!) your isObject code wasn't smart enough to allow those.

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on August 16, 2024

@jamesgpearce I tried this code:

import realisticStructuredClone from "realistic-structured-clone";

const x = {foo: 5};
const y = Object.create(null);
y.foo = 5;

console.log(Object.getPrototypeOf(x));
console.log(Object.getPrototypeOf(structuredClone(x)));
console.log(Object.getPrototypeOf(realisticStructuredClone(x)));
console.log(Object.getPrototypeOf(y));
console.log(Object.getPrototypeOf(structuredClone(y)));
console.log(Object.getPrototypeOf(realisticStructuredClone(y)));

And got this output:

[Object: null prototype] {}
[Object: null prototype] {}
[Object: null prototype] {}
null
[Object: null prototype] {}
[Object: null prototype] {}

So it seems the old (realisticStructuredClone) and new (structuredClone) are both doing the same thing to the prototype, which actually results in losing the null prototype when cloning.

If you think you're possibly seeing a bug, please create another issue with more info :)

from fakeindexeddb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.