12

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Mateusz Bielak
  • 123
  • 1
  • 6

5 Answers5

17

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

Phillip Thomas
  • 1,450
  • 11
  • 21
  • Does anyone know when one needs the `FDBKeyRange` dependency (versus just the `indexedDB`? – ken Jan 04 '20 at 09:55
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']
Aakash
  • 21,375
  • 7
  • 100
  • 81
Alok Kumar
  • 81
  • 1
  • 2
7

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"
    ]
}
Oluwasegun Wahaab
  • 2,663
  • 3
  • 16
  • 19
  • 1
    Thanks Oluwasegun! This was exactly what I was looking for. Best solution in the thread (for me)! – beeman Apr 13 '22 at 20:33
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.

dain
  • 6,475
  • 1
  • 38
  • 47
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']
};
David Dehghan
  • 22,159
  • 10
  • 107
  • 95