fake-indexeddb 5.0.0 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/build/cjs/lib/valueToKey.js +16 -5
- package/build/esm/lib/valueToKey.js +16 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -118,6 +118,48 @@ To use it on all Jest tests without having to include it in each file, add the a
|
|
|
118
118
|
}
|
|
119
119
|
```
|
|
120
120
|
|
|
121
|
+
### jsdom (often used with Jest)
|
|
122
|
+
|
|
123
|
+
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.
|
|
124
|
+
|
|
125
|
+
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:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import "core-js/stable/structured-clone";
|
|
129
|
+
import "fake-indexeddb/auto";
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Or, [you could manually include the Node.js `structuredClone` implementation in a jsdom environment](https://github.com/jsdom/jsdom/issues/3363#issuecomment-1467894943):
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
// FixJSDOMEnvironment.ts
|
|
136
|
+
|
|
137
|
+
import JSDOMEnvironment from 'jest-environment-jsdom';
|
|
138
|
+
|
|
139
|
+
// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
|
|
140
|
+
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
|
|
141
|
+
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
|
|
142
|
+
super(...args);
|
|
143
|
+
|
|
144
|
+
// FIXME https://github.com/jsdom/jsdom/issues/3363
|
|
145
|
+
this.global.structuredClone = structuredClone;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
// jest.config.js
|
|
152
|
+
|
|
153
|
+
/** @type {import('jest').Config} */
|
|
154
|
+
const config = {
|
|
155
|
+
testEnvironment: './FixJSDOMEnvironment.ts',
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
module.exports = config;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Hopefully a future version of jsdom will no longer require these workarounds.
|
|
162
|
+
|
|
121
163
|
### Wiping/resetting the indexedDB for a fresh state
|
|
122
164
|
|
|
123
165
|
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.
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _errors = require("./errors.js");
|
|
8
|
-
// https://w3c.github.io/IndexedDB/#convert-
|
|
8
|
+
// https://w3c.github.io/IndexedDB/#convert-value-to-key
|
|
9
9
|
const valueToKey = (input, seen) => {
|
|
10
10
|
if (typeof input === "number") {
|
|
11
11
|
if (isNaN(input)) {
|
|
@@ -20,11 +20,22 @@ const valueToKey = (input, seen) => {
|
|
|
20
20
|
return new Date(ms);
|
|
21
21
|
} else if (typeof input === "string") {
|
|
22
22
|
return input;
|
|
23
|
-
} else if (input instanceof ArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
} else if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
|
|
24
|
+
let arrayBuffer;
|
|
25
|
+
let offset = 0;
|
|
26
|
+
let length = 0;
|
|
27
|
+
if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer) {
|
|
28
|
+
arrayBuffer = input;
|
|
29
|
+
length = input.byteLength;
|
|
30
|
+
} else {
|
|
31
|
+
arrayBuffer = input.buffer;
|
|
32
|
+
offset = input.byteOffset;
|
|
33
|
+
length = input.byteLength;
|
|
26
34
|
}
|
|
27
|
-
|
|
35
|
+
if (arrayBuffer.detached) {
|
|
36
|
+
return new ArrayBuffer(0);
|
|
37
|
+
}
|
|
38
|
+
return arrayBuffer.slice(offset, offset + length);
|
|
28
39
|
} else if (Array.isArray(input)) {
|
|
29
40
|
if (seen === undefined) {
|
|
30
41
|
seen = new Set();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataError } from "./errors.js";
|
|
2
|
-
// https://w3c.github.io/IndexedDB/#convert-
|
|
2
|
+
// https://w3c.github.io/IndexedDB/#convert-value-to-key
|
|
3
3
|
const valueToKey = (input, seen) => {
|
|
4
4
|
if (typeof input === "number") {
|
|
5
5
|
if (isNaN(input)) {
|
|
@@ -14,11 +14,22 @@ const valueToKey = (input, seen) => {
|
|
|
14
14
|
return new Date(ms);
|
|
15
15
|
} else if (typeof input === "string") {
|
|
16
16
|
return input;
|
|
17
|
-
} else if (input instanceof ArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
} else if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
|
|
18
|
+
let arrayBuffer;
|
|
19
|
+
let offset = 0;
|
|
20
|
+
let length = 0;
|
|
21
|
+
if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer) {
|
|
22
|
+
arrayBuffer = input;
|
|
23
|
+
length = input.byteLength;
|
|
24
|
+
} else {
|
|
25
|
+
arrayBuffer = input.buffer;
|
|
26
|
+
offset = input.byteOffset;
|
|
27
|
+
length = input.byteLength;
|
|
20
28
|
}
|
|
21
|
-
|
|
29
|
+
if (arrayBuffer.detached) {
|
|
30
|
+
return new ArrayBuffer(0);
|
|
31
|
+
}
|
|
32
|
+
return arrayBuffer.slice(offset, offset + length);
|
|
22
33
|
} else if (Array.isArray(input)) {
|
|
23
34
|
if (seen === undefined) {
|
|
24
35
|
seen = new Set();
|
package/package.json
CHANGED