IndexedDB testing with Jest & enzyme - ReferenceError: indexedDB is not defined
Asked Answered
A

5

15

I'm looking for help with unit tests for my app, where I'm using indexedDB. Before I implemented indexedDB functionality, tests were correct. But now, for all of them I see one error:

ReferenceError: indexedDB is not defined

Can someone give me an advice how to get rid of that error? I was searching information, and trying different ways to mock window, or indexedDB, but with no result.

Andromeda answered 22/12, 2017 at 0:12 Comment(0)
A
18

This issue is due to Dexie expecting window.indexedDB to be defined, this is not the case when running in a headless mode (using Jest) that does not have a true DOM or window scope.

Found a solution deep in the Dexie git issues which suggests:

const Dexie = require('dexie')

Dexie.dependencies.indexedDB = require('fake-indexeddb')
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange')

We have also had success with:

import Dexie from 'dexie';
import indexedDB from 'fake-indexeddb';

Dexie.dependencies.indexedDB = indexedDB;

Link to the original issue: https://github.com/dfahlander/Dexie.js/issues/495

Or according to the documentation, you can provide the indexedDB option like:

import Dexie from 'dexie';
import indexedDB from 'fake-indexeddb';

var db = new Dexie("MyDatabase", { indexedDB: indexedDB });

Link to documentation: http://dexie.org/docs/Dexie/Dexie

Armindaarming answered 12/2, 2018 at 15:54 Comment(1)
Does anyone know when one needs the FDBKeyRange dependency (versus just the indexedDB?Counterchange
A
10

when using jest, according to the fakeindexeddb docs, install,

npm install --save-dev fake-indexeddb

or

yarn add --dev fake-indexeddb

then add below code to the jestconfig file

"jest": {
    ...
    "setupFiles": [
        "fake-indexeddb/auto"
    ]
}
Asphaltite answered 1/4, 2022 at 6:18 Comment(1)
Thanks Oluwasegun! This was exactly what I was looking for. Best solution in the thread (for me)!Daimon
L
8

If You are using jest and enzyme for testing indexdb or you are using dexie which is a indexDB wrapper which is also used for implementing indexDB api you have to just add these three lines in you global-test.js file .

const Dexie = require('dexie');
Dexie.dependencies.indexedDB = require('fake-indexeddb');
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange');

Now you have to provide this file to jest, show that it can use fake-indexddb instead of original indexDB.

setupFiles: ['<rootDir>/src/test/globals-test.ts']
Lavenialaver answered 10/1, 2019 at 18:33 Comment(0)
G
1

I'm not using Dexie (but instead got here when Firebase was throwing an exception on import), the fix was simply adding require('fake-indexeddb/auto') into setupTests.ts for Jest to pick up.

Gobang answered 10/10, 2019 at 16:15 Comment(0)
H
0

For Angular 7.3+ with jest add this to your global-test.ts file:

const Dexie = require('dexie');
Dexie.dependencies.indexedDB = require('fake-indexeddb');
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange');

then load the file in jest.config.js:

module.exports = {
    preset: 'jest-preset-angular',
    transformIgnorePatterns: ['node_modules'],
    setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
    moduleNameMapper: {
        '\\.(jpg|jpeg|png)$': '<rootDir>/__mocks__/image.js',
        '@lib/(.*)': '<rootDir>/src/lib/$1'
    },
    globals: {
        'ts-jest': {
            tsConfigFile: 'src/tsconfig.spec.json'
        },
        __TRANSFORM_HTML__: true
    },
    setupFiles: ['<rootDir>/src/test/globals-test.ts']
};
Hardily answered 9/11, 2018 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.