Reputation: 153
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.
Upvotes: 15
Views: 12802
Reputation: 2853
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"
]
}
Upvotes: 10
Reputation: 81
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']
Upvotes: 8
Reputation: 6699
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.
Upvotes: 1
Reputation: 24825
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']
};
Upvotes: 0
Reputation: 1479
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
Upvotes: 18