### Install and build with pnpm Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/CONTRIBUTING.md Run these commands to install dependencies and build the project. Requires pnpm to be installed. ```sh pnpm i pnpm build ``` -------------------------------- ### Polyfill structuredClone with core-js Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Install core-js and import the polyfill before fake-indexeddb to resolve structuredClone issues in environments like jsdom. ```js import "core-js/stable/structured-clone"; import "fake-indexeddb/auto"; ``` -------------------------------- ### Install fake-indexeddb via npm Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Use this command to add fake-indexeddb as a development dependency to your project. ```sh npm install --save-dev fake-indexeddb ``` -------------------------------- ### Use fake-indexeddb with Dexie via auto-import Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Importing the auto-setup script before Dexie allows Dexie to use the fake-indexeddb implementation globally. ```js import "fake-indexeddb/auto"; import Dexie from "dexie"; const db = new Dexie("MyDatabase"); ``` -------------------------------- ### Polyfill old environments with core-js Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Import core-js before fake-indexeddb to provide necessary polyfills for legacy environments like PhantomJS. ```js import "core-js/stable"; import "fake-indexeddb/auto"; ``` -------------------------------- ### Configure Jest setupFiles Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Add fake-indexeddb/auto to the setupFiles array in your Jest configuration to enable it for all tests. ```json { "setupFiles": [ "fake-indexeddb/auto" ] } ``` -------------------------------- ### Run all WPT tests Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Executes the full suite of Web Platform Tests using pnpm. ```sh pnpm run test-w3c ``` -------------------------------- ### Lint and test commands Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/CONTRIBUTING.md Run linting and various test suites. The commands include running all tests, W3C tests, and Mocha tests. ```sh pnpm run lint pnpm test # run all the tests pnpm run test-w3c # run just the W3C tests pnpm run test-mocha # run just the Mocha tests ``` -------------------------------- ### Run conversion script Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Executes the conversion script to process the updated tests. ```sh node src/test/web-platform-tests/convert.js ``` -------------------------------- ### Use fake-indexeddb with auto-import Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Importing fake-indexeddb/auto populates the global scope with IndexedDB variables, allowing standard IndexedDB usage. ```js import "fake-indexeddb/auto"; var request = indexedDB.open("test", 3); request.onupgradeneeded = function () { var db = request.result; var store = db.createObjectStore("books", {keyPath: "isbn"}); store.createIndex("by_title", "title", {unique: true}); store.put({title: "Quarry Memories", author: "Fred", isbn: 123456}); store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567}); store.put({title: "Bedrock Nights", author: "Barney", isbn: 345678}); } request.onsuccess = function (event) { var db = event.target.result; var tx = db.transaction("books"); tx.objectStore("books").index("by_title").get("Quarry Memories").addEventListener("success", function (event) { console.log("From index:", event.target.result); }); tx.objectStore("books").openCursor(IDBKeyRange.lowerBound(200000)).onsuccess = function (event) { var cursor = event.target.result; if (cursor) { console.log("From cursor:", cursor.value); cursor.continue(); } }; tx.oncomplete = function () { console.log("All done!"); }; }; ``` -------------------------------- ### Release version command Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/CONTRIBUTING.md Run pnpm version (e.g., pnpm version patch) to trigger the update-wpt-results script, which updates browser WPT results and fake-indexeddb's results in the README. ```sh pnpm version patch ``` -------------------------------- ### Update IDL files Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Copies necessary IDL files from the WPT repository to the local idlharness directory. ```sh cp path/to/wpt/interfaces/{IndexedDB,html,dom}.idl \ path/to/fakeIndexedDB/src/test/web-platform-tests/idlharness ``` -------------------------------- ### Run a subset of WPT tests Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Runs specific tests by matching a name pattern using the Node.js test runner. ```sh node --test --test-name-pattern="name of test" \ ./src/test/web-platform-tests/run-all.js ``` -------------------------------- ### Update all manifest files Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Regenerates test expectation manifests based on current test results. ```sh GENERATE_MANIFESTS=1 pnpm run test-w3c ``` -------------------------------- ### Explicitly import IndexedDB variables Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Import specific IndexedDB classes directly from the package instead of modifying the global scope. ```js import { indexedDB, IDBCursor, IDBCursorWithValue, IDBDatabase, IDBFactory, IDBIndex, IDBKeyRange, IDBObjectStore, IDBOpenDBRequest, IDBRequest, IDBTransaction, IDBVersionChangeEvent, } from "fake-indexeddb"; // The rest is the same as above. ``` -------------------------------- ### Simulate close event with forceCloseDatabase Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Use the non-standard forceCloseDatabase function to trigger a close event on an IDBDatabase instance for testing purposes. ```js import { forceCloseDatabase } from "fake-indexeddb"; db.addEventListener("close", () => { console.log("Forcibly closed!"); }); forceCloseDatabase(db); // invokes the event listener ``` -------------------------------- ### Reset IndexedDB state Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Create a new instance of IDBFactory to obtain a fresh IndexedDB state for test isolation. ```js import "fake-indexeddb/auto"; import { IDBFactory } from "fake-indexeddb"; // Whenever you want a fresh indexedDB indexedDB = new IDBFactory(); ``` -------------------------------- ### Use fake-indexeddb with Dexie via explicit injection Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Pass the fake-indexeddb objects directly to the Dexie constructor to avoid modifying the global scope. ```js import Dexie from "dexie"; import { indexedDB, IDBKeyRange } from "fake-indexeddb"; const db = new Dexie("MyDatabase", { indexedDB: indexedDB, IDBKeyRange: IDBKeyRange }); ``` -------------------------------- ### Fix jsdom environment for Jest Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Create a custom JSDOM environment to inject the Node.js structuredClone implementation and configure it in jest.config.js. ```ts // 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) { 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; ``` -------------------------------- ### Update IndexedDB test files Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/src/test/web-platform-tests/README.md Removes existing tests and copies the latest IndexedDB tests from the WPT repository. ```sh rm -fr path/to/fakeIndexedDB/src/test/web-platform-tests/IndexedDB cp -R path/to/wpt/IndexedDB path/to/fakeIndexedDB/src/test/web-platform-tests/IndexedDB rm -fr path/to/fakeIndexedDB/src/test/web-platform-tests/converted ``` -------------------------------- ### Rename imported IndexedDB variables Source: https://github.com/dumbmatter/fakeindexeddb/blob/master/README.md Rename imports to avoid conflicts with built-in IndexedDB variables. ```js import { indexedDB as fakeIndexedDB, } from "fake-indexeddb"; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.