简体   繁体   中英

How to test callable functions using the @firebase/testing library - getting errors

I am trying to test onCall functions locally using the firebase emulators and the @firebase/testing package. When I try to call the function I get the following error message...

Error: Response is not valid JSON object.
      at new HttpsErrorImpl (node_modules/@firebase/functions/dist/index.node.cjs.js:60:28)
      at Service.<anonymous> (node_modules/@firebase/functions/dist/index.node.cjs.js:534:35)
      at step (node_modules/tslib/tslib.js:136:27)
      at Object.next (node_modules/tslib/tslib.js:117:57)
      at fulfilled (node_modules/tslib/tslib.js:107:62)
      at process._tickCallback (internal/process/next_tick.js:68:7)

From what I can tell it is not even calling the function so this looks like a package issue instead of my code - I am making this assumption as the console.log entries in the callable function are not appearing before the crash. I've also tested by calling the endpoint directly using curl and this is working correctly, so I can confirm the function is loaded and operating correctly. It is just the firebase

Is @firebase/testing able to test callable functions or is that that a work in progress?

The function definition is...

export const demoCallable = functions.region('europe-west1').https.onCall((data: any, context: CallableContext): any => {

    console.log(data);

    return {
        test: true
    };

});

The test script is...

const firebase = require('@firebase/testing');
const expect = require('chai').expect;
const adminApp = firebase.initializeAdminApp({projectId: 'XXXXX'});

const fn = adminApp.functions('europe-west1').httpsCallable('demoCallable');

console.log('Running callable...');
const response = await fn({test: true});

console.log('response...');
console.log(response);
console.log('...response');

expect(response).to.eql({test: true});

firebase usually returns this error when cannot find the cloud function.. make sure you are giving the correct name and location of the function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM