Files
iNaturalistReactNative/tests/realm.setup.js
Ryan Stelly b78be9243d lint rule & autofix for "trailing comma" (#3299)
* (lint) MOB-1063 enforce trailing commas

* autofix trailing commas

* manually fix newly introduced maxlen violations

* add trailing comma convention to i18n build
2025-12-22 20:17:13 -06:00

54 lines
1.7 KiB
JavaScript

import mockOs from "os";
import mockPath from "path";
import Realm from "realm";
// eslint-disable-next-line import/extensions
import realmConfig from "realmModels/index";
// Mock the realm config so it uses an in-memory database. This means data is
// only persisted until realm.close() gets called, so if the code under test
// needs data to persist in Realm across opening/closing events, we will need
// to take a different approach, e.g. writing to Realm to disk and erasing
// those files after each test run
jest.mock( "realmModels/index", ( ) => {
const originalModule = jest.requireActual( "realmModels/index" );
// Mock the default export and named export 'foo'
return {
__esModule: true,
...originalModule,
default: {
schema: originalModule.default.schema,
schemaVersion: originalModule.default.schemaVersion,
inMemory: true,
path: mockPath.join( mockOs.tmpdir( ), "testArtifacts.realm" ),
},
};
} );
// Mock the contexts so the useRealm hook will provide a single, custom copy
// of the Realm connection
jest.mock( "providers/contexts", ( ) => {
const originalModule = jest.requireActual( "providers/contexts" );
return {
__esModule: true,
...originalModule,
RealmContext: {
...originalModule.RealmContext,
useRealm: ( ) => global.realm,
},
};
} );
// Open a realm connection and stuff it in global. If there's a better way to
// do this without having jest.mock complain about referring to variables out
// of scope, please propose it.
beforeAll( async ( ) => {
global.realm = await Realm.open( realmConfig );
} );
// Ensure the realm connection gets closed
afterAll( ( ) => {
global.realm?.close( );
Realm.shutdown();
} );