Parse SDK Mocked Database
parse-mockdb provides a mocked RESTController for the Parse JavaScript SDK, specifically compatible with version `2.0+`. It is primarily designed for unit testing Parse-dependent applications by allowing developers to simulate Parse backend operations (CRUD, queries, relations) locally without requiring an actual Parse Server instance. The current stable version is 0.4.0, which targets Parse JS SDK 2.x. While it supports many core Parse features like basic CRUD, various query operators (e.g., $exists, $in, $regex), and Parse.Relation, it currently lacks support for Parse class-level permissions, ACLs, and special classes like Parse.User or Parse.Role. The release cadence appears to be driven by breaking changes and updates to align with the Parse SDK, with significant breaking changes in versions 0.3.0 and 0.4.0.
Common errors
-
TypeError: ParseMockDB.mockDB is not a function
cause Attempting to call `mockDB` without passing the Parse SDK instance after v0.3.0, or `ParseMockDB` was accessed incorrectly.fixEnsure `const Parse = require('parse-shim');` and then call `ParseMockDB.mockDB(Parse);`. -
ReferenceError: Parse is not defined
cause The Parse SDK (or `parse-shim`) was not imported or initialized before being passed to `ParseMockDB.mockDB()`.fixAdd `const Parse = require('parse-shim');` at the top of your test file or ensure Parse is globally available if that's your setup. -
Error: "undefined" is not a valid class name.
cause This error often occurs when trying to use `Parse.Object.extend` or `new Parse.Query` without a properly initialized Parse environment, which `parse-mockdb` hooks into.fixEnsure `Parse.initialize()` is called with placeholder values (appId, jsKey, masterKey) before mocking, as the Parse SDK sometimes requires this even when mocking.
Warnings
- breaking Version 0.4.0 is a breaking change, specifically targeting the 2.x series of the Parse JS SDK. If you are using Parse JS SDK 1.6+, you must pin your `parse-mockdb` dependency to the `v0.3.x` release to maintain compatibility.
- breaking With v0.3.0, the `mockDB()` function now requires you to pass a reference to the Parse SDK instance you wish to mock. This was a significant change from previous versions.
- breaking As of v0.3.0, the `ParseMockDB` object is no longer patched onto the `Parse` module itself. You can no longer access it via `Parse.MockDB`.
- breaking The `ParseMockDB.promiseResultSync` method was removed in v0.3.0, impacting tests or code that relied on synchronous promise resolution within the mock.
- gotcha The library's completeness section indicates several unimplemented features, including Parse class-level permissions, ACLs (row-level permissions), and special classes like `Parse.User` or `Parse.Role`. Tests relying on these features will not behave correctly with `parse-mockdb`.
Install
-
npm install parse-mockdb -
yarn add parse-mockdb -
pnpm add parse-mockdb
Imports
- ParseMockDB
import ParseMockDB from 'parse-mockdb';
const ParseMockDB = require('parse-mockdb'); - mockDB
const { mockDB } = require('parse-mockdb'); mockDB(Parse);ParseMockDB.mockDB(Parse);
- Parse
const Parse = require('parse');const Parse = require('parse-shim');
Quickstart
'use strict';
const Parse = require('parse-shim');
const ParseMockDB = require('parse-mockdb');
// Initialize Parse (e.g., required for some SDK operations)
Parse.initialize('appId', 'jsKey', 'masterKey');
Parse.serverURL = 'http://localhost:1337/parse'; // Or any placeholder
ParseMockDB.mockDB(Parse); // Mock the Parse RESTController
// Perform saves, queries, updates, deletes, etc... using the Parse JS SDK
async function testParseOperations() {
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.set('key', 'value');
await myObject.save();
console.log('Object saved with id:', myObject.id);
const query = new Parse.Query(MyObject);
query.equalTo('key', 'value');
const results = await query.find();
console.log('Found objects:', results.length);
}
testParseOperations().then(() => {
ParseMockDB.cleanUp(); // Clear the Database
ParseMockDB.unMockDB(); // Un-mock the Parse RESTController
console.log('MockDB cleaned up and un-mocked.');
});