New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

firestore-jest-mock

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firestore-jest-mock - npm Package Compare versions

Comparing version

to
0.2.1

.editorconfig

36

__tests__/full-setup.test.js

@@ -5,5 +5,8 @@ const { mockFirebase } = require('firestore-jest-mock');

const {
mockGet,
mockAdd,
mockSet,
mockUpdate,
mockWhere,
mockCollectionGroup,
mockBatch,

@@ -50,3 +53,3 @@ mockBatchCommit,

// https://firebase.google.com/docs/firestore/quickstart#add_data
db.collection('users').add({

@@ -67,3 +70,3 @@ first: 'Ada',

// https://firebase.google.com/docs/firestore/quickstart#read_data
db.collection('users').get().then((querySnapshot) => {

@@ -80,2 +83,25 @@ expect(querySnapshot.forEach).toBeTruthy();

test('collectionGroup', () => {
const db = this.firebase.firestore();
// Example from documentation:
// https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query
db.collectionGroup('cities')
.where('type', '==', 'museum')
.get()
.then(querySnapshot => {
expect(mockCollectionGroup).toHaveBeenCalledWith('cities');
expect(mockGet).toHaveBeenCalled();
expect(mockWhere).toHaveBeenCalledWith('type', '==', 'museum');
expect(querySnapshot.forEach).toBeTruthy();
expect(querySnapshot.docs.length).toBe(2);
querySnapshot.forEach((doc) => {
expect(doc.exists).toBe(true);
expect(doc.data()).toBeTruthy();
});
});
});
test('set a city', () => {

@@ -85,3 +111,3 @@ const db = this.firebase.firestore();

// https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document\
db.collection('cities').doc('LA').set({

@@ -109,3 +135,3 @@ name: 'Los Angeles',

});
test('batch writes', () => {

@@ -141,2 +167,2 @@ const db = this.firebase.firestore();

});
});
});

4

index.js

@@ -8,3 +8,3 @@ const { FakeFirestore } = require('./mocks/firestore');

FakeFirestore,
FakeAuth
};
FakeAuth,
};

@@ -42,3 +42,3 @@ const mockCreateUserWithEmailAndPassword = jest.fn();

}
};
}

@@ -53,2 +53,2 @@ module.exports = {

mockVerifyIdToken,
};
};
const mockInitializeApp = jest.fn();
const mockCert = jest.fn();
const firebaseStub = (overrides) => {
const firebaseStub = overrides => {
const { FakeFirestore, FakeAuth } = require('firestore-jest-mock');
return {
initializeApp: mockInitializeApp,
credential: {
cert: mockCert,
},
auth() {
return new FakeAuth(overrides.currentUser);
},
firestore() {
return new FakeFirestore(overrides.database);
}
}
},
};
};
const mockFirebase = (overrides= {}) => {
jest.mock('firebase', () => firebaseStub(overrides)) && jest.mock('firebase-admin', () => firebaseStub(overrides));
const mockFirebase = (overrides = {}) => {
jest.mock('firebase', () => firebaseStub(overrides)) &&
jest.mock('firebase-admin', () => firebaseStub(overrides));
};

@@ -31,3 +32,3 @@

mockInitializeApp,
mockCert
};
mockCert,
};
const mockCollection = jest.fn();
const mockCollectionGroup = jest.fn();
const mockDoc = jest.fn();

@@ -40,3 +41,3 @@ const mockWhere = jest.fn();

return docs.forEach(callback);
}
},
};

@@ -59,2 +60,10 @@ }

collectionGroup(collectionName) {
this.recordToFetch = null;
this.isFetchingSingle = false;
this.collectionName = collectionName;
mockCollectionGroup(...arguments);
return this;
}
where() {

@@ -157,3 +166,3 @@ this.isFetchingSingle = false;

}
};
}

@@ -165,2 +174,3 @@ module.exports = {

mockCollection,
mockCollectionGroup,
mockDelete,

@@ -177,3 +187,3 @@ mockDoc,

mockBatchUpdate,
mockBatchSet
mockBatchSet,
};
{
"name": "firestore-jest-mock",
"version": "0.1.0",
"version": "0.2.1",
"description": "Jest helper for mocking Google Cloud Firestore",
"author": "",
"license": "MIT",
"keywords": [
"Jest",
"Firestore"
],
"main": "index.js",

@@ -11,17 +17,33 @@ "scripts": {

},
"keywords": [
"Jest",
"Firestore"
],
"author": "",
"license": "MIT",
"lint-staged": {
"*.{js,css,json,md}": [
"prettier --write"
],
"*.js": [
"eslint --fix"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@upstatement/eslint-config": "^0.4.3",
"@upstatement/prettier-config": "^0.3.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.8.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-jest": "^23.6.0",
"firebase": "^6.3.1",
"firebase-admin": "^8.3.0",
"husky": "^4.2.1",
"jest": "^24.8.0",
"jest-watch-typeahead": "^0.3.1",
"firebase": "^6.3.1",
"firebase-admin": "^8.3.0"
"lint-staged": "^10.0.2",
"prettier": "^1.19.1"
}
}

@@ -1,8 +0,8 @@

## Mock Firestore
# Mock Firestore
> Jest Mock for testing Google Cloud Firestore
A simple way to mock calls to Cloud Firestore, allowing you to asser that you are requesting data correctly.
A simple way to mock calls to Cloud Firestore, allowing you to assert that you are requesting data correctly.
This is <strong>not</strong> a pseudo-database -- it is only for testing you are interfacing with firebase/firestore the way you expect.
This is _not_ a pseudo-database -- it is only for testing you are interfacing with firebase/firestore the way you expect.

@@ -14,3 +14,5 @@ ## Table of Contents

- [What's in the Box](#whats-in-the-box)
- [`mockFirebase`](#mockfirebase)
- [Installation](#installation)
- [Usage](#usage)
- [`mockFirebase`](#mockfirebase)
- [What would you want to test?](#what-would-you-want-to-test)

@@ -21,3 +23,2 @@ - [I wrote a where clause, but all the records were returned!](#i-wrote-a-where-clause-but-all-the-records-were-returned)

- [Auth](#auth)
- [Installation](#installation)
- [Contributing](#contributing)

@@ -31,5 +32,22 @@ - [Code of Conduct](#code-of-conduct)

## `mockFirebase`
## Installation
With [npm](https://www.npmjs.com):
```shell
npm install --save-dev firestore-jest-mock
```
With [yarn](https://yarnpkg.com/):
```shell
yarn add --dev firestore-jest-mock
```
## Usage
### `mockFirebase`
The default method to use is `mockFirebase`, which returns a jest mock, overwriting `firebase` and `firebase-admin`. It accepts an object with two pieces:
- `database` -- A mock of your collections

@@ -46,9 +64,7 @@ - `currentUser` -- (optional) overwrites the currently logged in user

users: [
{ id: 'abc123', name: 'Homer Simpson'},
{ id: 'abc456', name: 'Lisa Simpson' }
{ id: 'abc123', name: 'Homer Simpson' },
{ id: 'abc456', name: 'Lisa Simpson' },
],
posts: [
{ id: '123abc', title: 'Really cool title' }
]
}
posts: [{ id: '123abc', title: 'Really cool title' }],
},
});

@@ -65,6 +81,8 @@ ```

const db = firebase.firestore();
db.collection('users').get().then((userDocs) => {
// write assertions here
});
db.collection('users')
.get()
.then(userDocs => {
// write assertions here
});
});

@@ -81,3 +99,3 @@ ```

const query = firestore.collection('users');
if (state) {

@@ -102,6 +120,6 @@ query = query.where('state', '==', state);

users: [
{ id: 'abc123', name: 'Homer Simpson'},
{ id: 'abc456', name: 'Lisa Simpson' }
]
}
{ id: 'abc123', name: 'Homer Simpson' },
{ id: 'abc456', name: 'Lisa Simpson' },
],
},
});

@@ -128,2 +146,3 @@

In this test, we don't necessarily care what gets returned from firestore (it's not our job to test firestore), but instead we try to assert that we built our query correctly.
> If I pass a state to this function, does it properly query the `users` collection?

@@ -144,44 +163,31 @@

| Method | Use | Method in Firestore |
| --- | --- | --- |
| mockCollection | Assert the correct collection is being queried | [collection](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#collection) |
| mockDoc | Assert the correct record is being fetched by id. Tells the mock you are fetching a single record | [doc](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#doc) |
| mockWhere | Assert the correct query is written. Tells the mock you are fetching multiple records | [where](https://googleapis.dev/nodejs/firestore/latest/Query.html#where) |
| mockBatch | Assert batch was called | [batch](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#batch) |
| mockBatchDelete | Assert correct refs are passed | [batch delete](https://googleapis.dev/nodejs/firestore/latest/WriteBatch.html#delete) |
| mockBatchCommit | Assert commit is called. Returns a promise | [batch commit](https://googleapis.dev/nodejs/firestore/latest/WriteBatch.html#commit) |
| mockGet | Assert get is called. Returns a promise resolving either to a single doc or querySnapshot | [get](https://googleapis.dev/nodejs/firestore/latest/Query.html#get) |
| mockGetAll | Assert correct refs are passed. Returns a promise resolving to array of docs. | [getAll](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#getAll) |
| mockUpdate | Assert correct params are passed to update. Returns a promise | [update](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#update) |
| mockAdd | Assert correct params are passed to add. Returns a promise resolving to the doc with new id | [add](https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#add) |
| mockSet | Assert correct params are passed to set. Returns a promise | [set](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#set) |
| mockDelete | Assert delete is called on ref. Returns a promise | [delete](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#delete) |
| mockOrderBy | Assert correct field is passed to orderBy | [orderBy](https://googleapis.dev/nodejs/firestore/latest/Query.html#orderBy) |
| mockLimit | Assert limit is set properly | [limit](https://googleapis.dev/nodejs/firestore/latest/Query.html#limit) |
| Method | Use | Method in Firestore |
| --------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| `mockCollection` | Assert the correct collection is being queried | [collection](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#collection) |
| `mockCollectionGroup` | Assert the correct collectionGroup is being queried | [collectionGroup](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#collectionGroup) |
| `mockDoc` | Assert the correct record is being fetched by id. Tells the mock you are fetching a single record | [doc](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#doc) |
| `mockWhere` | Assert the correct query is written. Tells the mock you are fetching multiple records | [where](https://googleapis.dev/nodejs/firestore/latest/Query.html#where) |
| `mockBatch` | Assert batch was called | [batch](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#batch) |
| `mockBatchDelete` | Assert correct refs are passed | [batch delete](https://googleapis.dev/nodejs/firestore/latest/WriteBatch.html#delete) |
| `mockBatchCommit` | Assert commit is called. Returns a promise | [batch commit](https://googleapis.dev/nodejs/firestore/latest/WriteBatch.html#commit) |
| `mockGet` | Assert get is called. Returns a promise resolving either to a single doc or querySnapshot | [get](https://googleapis.dev/nodejs/firestore/latest/Query.html#get) |
| `mockGetAll` | Assert correct refs are passed. Returns a promise resolving to array of docs. | [getAll](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#getAll) |
| `mockUpdate` | Assert correct params are passed to update. Returns a promise | [update](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#update) |
| `mockAdd` | Assert correct params are passed to add. Returns a promise resolving to the doc with new id | [add](https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#add) |
| `mockSet` | Assert correct params are passed to set. Returns a promise | [set](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#set) |
| `mockDelete` | Assert delete is called on ref. Returns a promise | [delete](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#delete) |
| `mockOrderBy` | Assert correct field is passed to orderBy | [orderBy](https://googleapis.dev/nodejs/firestore/latest/Query.html#orderBy) |
| `mockLimit` | Assert limit is set properly | [limit](https://googleapis.dev/nodejs/firestore/latest/Query.html#limit) |
#### Auth
| Method | Use | Method in Firebase |
| --- | --- | --- |
| mockCreateUserWithEmailAndPassword | Assert correct email and password are passed. Returns a promise | [createUserWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#createuserwithemailandpassword) |
| mockDeleteUser | Assert correct id is passed to delete method. Returns a promise | [deleteUser](https://firebase.google.com/docs/auth/admin/manage-users) |
| mockSendVerificationEmail | Assert request for verification email was sent. Lives on the `currentUser` | [sendVerificationEmail](https://firebase.google.com/docs/reference/js/firebase.User#send-email-verification) |
| mockSignInWithEmailAndPassword | Assert correct email and password were passed. Returns a promise | [signInWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#signinwithemailandpassword) |
| mockSendPasswordResetEmail | Assert correct email was passed. | [sendPasswordResetEmail](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#send-password-reset-email) |
| mockVerifyIdToken | Assert correct token is passed. Returns a promise | [verifyIdToken](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth.html#verifyidtoken) |
| Method | Use | Method in Firebase |
| ------------------------------------ | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `mockCreateUserWithEmailAndPassword` | Assert correct email and password are passed. Returns a promise | [createUserWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#createuserwithemailandpassword) |
| `mockDeleteUser` | Assert correct id is passed to delete method. Returns a promise | [deleteUser](https://firebase.google.com/docs/auth/admin/manage-users) |
| `mockSendVerificationEmail` | Assert request for verification email was sent. Lives on the `currentUser` | [sendVerificationEmail](https://firebase.google.com/docs/reference/js/firebase.User#send-email-verification) |
| `mockSignInWithEmailAndPassword` | Assert correct email and password were passed. Returns a promise | [signInWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#signinwithemailandpassword) |
| `mockSendPasswordResetEmail` | Assert correct email was passed. | [sendPasswordResetEmail](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#send-password-reset-email) |
| `mockVerifyIdToken` | Assert correct token is passed. Returns a promise | [verifyIdToken](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth.html#verifyidtoken) |
## Installation
With [npm](https://www.npmjs.com):
```shell
$ npm install firestore-jest-mock --save-dev
```
With [yarn](https://yarnpkg.com/):
```shell
$ yarn add firestore-jest-mock --dev
```
## Contributing

@@ -191,2 +197,4 @@

To get set up, simply clone this repository and `npm install`!
## Code of Conduct

@@ -198,2 +206,2 @@

[Upstatement](https://www.upstatement.com/) is a digital transformation studio headquartered in Boston, MA that imagines and builds exceptional digital experiences. Make sure to check out our [services](https://www.upstatement.com/services/), [work](https://www.upstatement.com/work/), and [open positions](https://www.upstatement.com/jobs/)!
[Upstatement](https://www.upstatement.com/) is a digital transformation studio headquartered in Boston, MA that imagines and builds exceptional digital experiences. Make sure to check out our [services](https://www.upstatement.com/services/), [work](https://www.upstatement.com/work/), and [open positions](https://www.upstatement.com/jobs/)!