firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

DocumentReference not compatible with @firebase/testing #44

Open xXCalleManXx opened 5 years ago

xXCalleManXx commented 5 years ago

Version info

firebase-functions-test: 0.1.6

firebase-functions: 3.2.0

firebase-admin: 8.3.0

@firebase/testing: 0.11.8

firebase-tools: 7.2.1

Test case

const firebase = require("@firebase/testing");
const test = require('firebase-functions-test')();
const admin = require('firebase-admin');

/**
 * @returns {firebase.firestore.Firestore}
 */
function adminApp() {
    return firebase.initializeAdminApp({projectId: "MyEmulatedProject"}).firestore()
}

const ref = adminApp().collection('users').doc('user1');

let data = { ref };

if (ref instanceof admin.firestore.DocumentReference) {
    console.log('DocumentRefrence of firebase-admin');
} else if (ref instanceof firebase.firestore.DocumentReference) {
    console.log('DocumentRefrence of @firebase/testing')
}

// Throws "Cannot encode [object Object]to a Firestore Value. Local testing does not yet support Firestore geo points."
test.firestore.makeDocumentSnapshot(data, '/collection/doc');

Steps to reproduce

  1. Start firestore emulator "firebase emulators:start --only firestore"
  2. Run the example above

Expected behavior

Should work the same way as with a DocumentReference from firebase-admin.

Actual behavior

Throws error

Error: Cannot encode [object Object]to a Firestore Value. Local testing does not yet support Firestore geo points.
    at encodeHelper (/node_modules/firebase-functions-test/lib/providers/firestore.js:165:15)
    at /node_modules/lodash/lodash.js:13401:38
    at /node_modules/lodash/lodash.js:4905:15
    at baseForOwn (node_modules/lodash/lodash.js:2990:24)
    at Function.mapValues (/node_modules/lodash/lodash.js:13400:7)
    at objectToValueProto (/node_modules/firebase-functions-test/lib/providers/firestore.js:168:21)
    at Object.makeDocumentSnapshot (/node_modules/firebase-functions-test/lib/providers/firestore.js:49:17)
    at Object.<anonymous> (/test/test2/test.js:22:16)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)

And it outputs DocumentRefrence of @firebase/testing as the DocumentReference is initialized with @firebase/testing

This error is resolved in issue #24, but only for DocumentReference and Timestamp initialized with firebase-admin.

laurenzlong commented 4 years ago

Sorry for the very late response! Using this SDK with @firebase/testing is not supported. I'm marking this as a feature request.