Skip to content

Commit

Permalink
Update README for jsdom compatibility with fake-indexeddb 5
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Oct 25, 2023
1 parent 26b6d8c commit cdcb99c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 5.0.0 (2023-10-13)

- Dropped support for Node.js 16, which allows me to get rid of the structuredClone polyfill, which reduces the package size by roughly 50%.
- Dropped support for Node.js 16, which allows me to get rid of the `structuredClone` polyfill, which reduces the package size by roughly 50%.

# 4.0.2 (2023-07-14)

Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,48 @@ To use it on all Jest tests without having to include it in each file, add the a
}
```

### jsdom (often used with Jest)

As of version 5, fake-indexeddb no longer includes a `structuredClone` polyfill. This mostly affects old environments like unsupported versions of Node.js, but [it also affects jsdom](https://github.com/dumbmatter/fakeIndexedDB/issues/88), which is often used with Jest and other testing frameworks.

There are a few ways you could work around this. You could include your own `structuredClone` polyfill by installing core-js and importing its polyfill before you use fake-indexeddb:

```js
import "core-js/stable/structured-clone";
import "fake-indexeddb/auto";
```

Or, [you could manually include the Node.js `structuredClone` implementation in a jsdom environment](https://github.com/jsdom/jsdom/issues/3363#issuecomment-1467894943):

```js
// FixJSDOMEnvironment.ts

import JSDOMEnvironment from 'jest-environment-jsdom';

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args);

// FIXME https://github.com/jsdom/jsdom/issues/3363
this.global.structuredClone = structuredClone;
}
}
```

```js
// jest.config.js

/** @type {import('jest').Config} */
const config = {
testEnvironment: './FixJSDOMEnvironment.ts',
};

module.exports = config;
```

Hopefully a future version of jsdom will no longer require these workarounds.

### Wiping/resetting the indexedDB for a fresh state

If you are keeping your tests completely isolated you might want to "reset" the state of the mocked indexedDB. You can do this by creating a new instance of `IDBFactory`, which lets you have a totally fresh start.
Expand Down

0 comments on commit cdcb99c

Please sign in to comment.