Skip to content

Commit

Permalink
test(firebase): use emulator to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jun 12, 2021
1 parent 3327666 commit 64ec3ac
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ node_modules/
# Files
.DS_Store

.firebase
.firebase
*.log
12 changes: 8 additions & 4 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "docs",
"predeploy": "npm run docs && cp -a ./examples/ ./docs/"
},
"emulators": {
"firestore": {
"port": 8080
},
"ui": {
"enabled": true
}
}
}
17 changes: 1 addition & 16 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
{
"indexes": [
{
"collectionGroup": "tests",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "count",
"order": "ASCENDING"
},
{
"fieldPath": "g.geohash",
"order": "ASCENDING"
}
]
}
],
"indexes": [],
"fieldOverrides": []
}
6 changes: 0 additions & 6 deletions firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,5 @@ service cloud.firestore {
&& request.resource.data.coordinates is latlng
&& request.resource.data.count is number;
}
match /geofirestore-tests/{key} {
allow read, write: if true;
}
match /geofirestore-core-tests/{key} {
allow read, write: if true;
}
}
}
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"docs": "typedoc --out docs/ src/",
"lint": "gts lint",
"test": "nyc --reporter=html --reporter=text mocha --package package.json --exit",
"start": "firebase emulators:start",
"test": "firebase emulators:exec 'npm run test:nyc'",
"test:nyc": "nyc --reporter=html --reporter=text mocha --package package.json --exit",
"release:major": "changelog -M && git add . && git commit -m 'chore(release): major version release' && npm version major",
"release:minor": "changelog -m && git add . && git commit -m 'chore(release): minor version release' && npm version minor",
"release:patch": "changelog -p && git add . && git commit -m 'chore(release): patch version release' && npm version patch",
Expand Down Expand Up @@ -59,6 +61,7 @@
"@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/node": "^14.11.2",
"axios": "^0.21.1",
"chai": "^4.3.4",
"coveralls": "^3.1.0",
"firebase-tools": "^9.12.1",
Expand Down
57 changes: 12 additions & 45 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import firebase from 'firebase/app';
import 'firebase/firestore';
import {encodeDocumentAdd} from 'geofirestore-core';
import {distance} from 'geokit';
import axios from 'axios';

import {GeoCollectionReference, GeoFirestore, GeoFirestoreTypes} from '../src';

Expand Down Expand Up @@ -129,6 +130,7 @@ export const invalidObjects: any[] = [
NaN,
];
export const testCollectionName = 'geofirestore-tests';
const projectId = 'geofirestore';
export const validGeohashes: string[] = ['4', 'd62dtu', '000000000000'];
export const validLocations: firebase.firestore.GeoPoint[] = [
new firebase.firestore.GeoPoint(0, 0),
Expand Down Expand Up @@ -160,16 +162,19 @@ export let geocollection: GeoCollectionReference;
firebase.initializeApp({
apiKey: 'AIzaSyDFnedGL4qr_jenIpWYpbvot8s7Vuay_88',
databaseURL: 'https://geofirestore.firebaseio.com',
projectId: 'geofirestore',
projectId,
});

/**********************/
/* HELPER FUNCTIONS */
/**********************/
/* Helper functions which runs before each Jasmine test has started */
export function beforeEachHelper(done: any): void {
// Create a new Firebase database ref at a random node
if (firestore) {
firestore.terminate();
}
firestore = firebase.firestore();
firestore.useEmulator('localhost', 8080);
collection = firestore.collection(testCollectionName);
geofirestore = new GeoFirestore(firestore);
geocollection = new GeoCollectionReference(collection);
Expand All @@ -178,12 +183,11 @@ export function beforeEachHelper(done: any): void {

/* Helper functions which runs after each Jasmine test has completed */
export function afterEachHelper(done: any): void {
deleteCollection()
.then(() => {
// Wait for 50 milliseconds after each test to give enough time for old query events to expire
return wait(50);
})
.then(done);
axios
.delete(
`http://${process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS}/emulator/v1/projects/${projectId}/databases/(default)/documents`
)
.then(() => done());
}

/* Helper function designed to create docs within a certain range of coordinates */
Expand Down Expand Up @@ -225,43 +229,6 @@ export function wait(milliseconds = 100): Promise<void> {
});
}

/* Used to purge Firestore collection. Used by afterEachHelperFirestore. */
function deleteCollection(): Promise<any> {
return new Promise((resolve, reject) =>
deleteQueryBatch(collection.limit(500), resolve, reject)
);
}

/* Actually purges Firestore collection recursively through batch function. */
function deleteQueryBatch(
query: firebase.firestore.Query,
resolve: Function,
reject: Function
): void {
query
.get()
.then(snapshot => {
// When there are no documents left, we are done
if (snapshot.size === 0) {
return 0;
}

// Delete documents in a batch
const batch = firestore.batch();
snapshot.docs.forEach(doc => batch.delete(doc.ref));

return batch.commit().then(() => snapshot.size);
})
.then(numDeleted => {
if (numDeleted === 0) {
resolve();
return;
}
process.nextTick(() => deleteQueryBatch(query, resolve, reject));
})
.catch(err => reject(err));
}

export function stubDatabase(
docs: Array<{[key: string]: any}> = validDocumentData()
): Promise<any> {
Expand Down

0 comments on commit 64ec3ac

Please sign in to comment.