I keep receiving
[DB] Uh-oh. Database failed to load, we're in big trouble TypeError: Cannot read property 'initialize' of undefined
When test the database with jest
I trace the error and find that
at node_modules/@nozbe/watermelondb/adapters/sqlite/index.js:95:35
This
var Native = _reactNative.NativeModules.DatabaseBridge;
Native is null
Iam using the following libraries
"react-native": "0.58.4",
"react": "16.8.2",
"prop-types": "^15.7.2",
And I have the right decorators defined in babel
What is the problem?
What is the problem?
You can't test with a SQLite database in Jest. As you can see in the error, it's a purely native thing. It's possible to do this if you run tests in the simulator, but it's quite complicated to set up.
Replace SQLiteAdapter with LokiJS adapter for tests only, as if you were setting up a web project. This should work without problem :)
Thanks @radex you save my day!
I wish there was a testing example (especially with React Native) because I'm struggling with it. I tried to use LokiJSAdapter instead of SQLiteAdapter when jest is running but got multiple modules not found error... (lokijs, lodash.clonedeep etc)
but got multiple modules not found error... (lokijs, lodash.clonedeep etc)
Just yarn add loci's, lodash.clonedeep :)
@radex Is it ok to import both LokiJSAdapter and SQLiteAdapter in production? Since I don't believe it's possible to conditionally import LokiJSAdapter. (I use it only for testing my react native app)
...
import SQLiteAdapter from "@nozbe/watermelondb/adapters/sqlite";
import LokiJSAdapter from "@nozbe/watermelondb/adapters/lokijs";
const watermelonAdapter = __TEST__ ? LokiJSAdapter : SQLiteAdapter
const adapter = new watermelonAdapter({
schema: mySchema
});
It is OK — there is no global behavior here. You can use require() to conditionally import an Adapter, and if __TEST__ is defined by the right babel plugin, it can strip unnecessary code during compilation :)
Unfortunately, I get
error: bundling failed: Error: Unable to resolve module fs from ~/MyApp/node_modules/lokijs/src/lokijs.js: Module fs does not exist in the Haste module map when I run the app on ios.
Even conditionally importing lokijs is not avoiding the error:
if (__TEST__) {
LokiJSAdapter = require("@nozbe/watermelondb/adapters/lokijs");
}
@cwagner22 hmm what if you make a separate index.js for testing setup?
Ok I've moved the database in database.js where I export it and have mocked __mocks__/database.js to use LokiJSAdapter
I am facing the same issue when I run react-native start . Can you please give more information on mocking the database object?
@YajanaRao I did it like that (I think that's all):
_App.js_
import database from "../Models/database";
_Models/database.js_
import { Database } from "@nozbe/watermelondb";
import SQLiteAdapter from "@nozbe/watermelondb/adapters/sqlite";
import { mySchema } from "./schema";
import Blog from "./Blog";
const adapter = new SQLiteAdapter({
dbName: "AppName",
schema: mySchema
});
export default new Database({
adapter,
modelClasses: [Blog]
});
_Models/__mocks__/database.js_
import { Database } from "@nozbe/watermelondb";
import LokiJSAdapter from "@nozbe/watermelondb/adapters/lokijs";
import { mySchema } from "../schema";
import Blog from "../Blog";
const adapter = new LokiJSAdapter({
dbName: "AppName",
schema: mySchema
});
export default new Database({
adapter,
modelClasses: [Blog]
});
_Jest setup file:_
jest.mock("../App/Models/database");
@cwagner22
Doing this apparently leaves us with some leaked handles, the database process isn't finished after the tests finish running. Were you able to handle that?
I don't remember having any similar issue. sorry...
Most helpful comment
@YajanaRao I did it like that (I think that's all):
_App.js_
_Models/database.js_
_Models/__mocks__/database.js_
_Jest setup file:_