https://github.com/gitbookio/expect-firestore
API client and Jasmine matchers for the Firestore Rules API
https://github.com/gitbookio/expect-firestore
firebase firestore
Last synced: about 2 months ago
JSON representation
API client and Jasmine matchers for the Firestore Rules API
- Host: GitHub
- URL: https://github.com/gitbookio/expect-firestore
- Owner: GitbookIO
- Created: 2017-12-22T16:45:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-21T12:07:21.000Z (about 7 years ago)
- Last Synced: 2025-04-21T08:46:30.510Z (2 months ago)
- Topics: firebase, firestore
- Language: JavaScript
- Size: 60.5 KB
- Stars: 22
- Watchers: 5
- Forks: 5
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `expect-firestore`
[](https://travis-ci.org/GitbookIO/expect-firestore)
Node module to easily unit tests Firestore rules. It abstract the Firestore Rules testing API to easily mock a dataset.
## Install
```
$ yarn add expect-firestore
```## Usage
```js
import assert from 'assert';
import * as firestore from 'expect-firestore';const database = new firestore.Database({
// Credentials from firebase console
credential: { project_id: '...', ... },
// Fake data to test assertions against
data: {
// See src/__tests__/fixtures/db.json for an example
users: [
{
key: 'userA',
fields: {
name: 'John Doe'
},
collections: {
favorites: [
...
]
}
},
...
]
},
// Firestore rules
rules: ''
});// Authorize the client database
await database.authorize();// Test a get
const result = await database.canGet({ uid: 'some_user' }, 'users/userA');
firestore.assert(result);
```## API
#### Global
- `new firestore.Database({ data, credentials, rules })`: Create a database instance to test rules
```js
const database = new firestore.Database({
credentials: require('credential.json')
});
```
- `firestore.assert(test: TestResult)`: Throw a human readable error if test failed, otherwise do nothing```js
const result = await database.canGet({}, 'users/userA');
firestore.assert(result);
```#### `firestore.Database`
- `database.authorize(): Promise`: Prepare the testing environment, it must be called at least once.
Run `get`, `set`, `update` and `commit` tests using:
- `database.canGet(auth: FirestoreAuth, document: string): Promise`
- `database.cannotGet(auth: FirestoreAuth, document: string): Promise`
- `database.canSet(auth: FirestoreAuth, document: string, value: any): Promise`
- `database.cannotSet(auth: FirestoreAuth, document: string, value: any): Promise`
- `database.canUpdate(auth: FirestoreAuth, document: string, values: Object): Promise`
- `database.cannotUpdate(auth: FirestoreAuth, document: string, values: Object): Promise`
- `database.canCommit(auth: FirestoreAuth, batch: BatchOperation[]): Promise`
- `database.cannotCommit(auth: FirestoreAuth, batch: firestore.BatchOperation[]): Promise`Control test testing environment using:
- `database.setData(data: FirestoreCollections)`: Update the dataset
- `database.setRules(rules: string)`: Update the rules being tested
- `database.setRulesFromFile(file: string)`: Read the rules from a file### `firestore.Batch`
- `firestore.Batch.set(document: string, value: any): BatchOperation`
- `firestore.Batch.update(document: string, values: Object): BatchOperation`
- `firestore.Batch.delete(document: string): BatchOperation`