firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

TypeError: Cannot read property 'storageBucket' of null #77

Closed Ridermansb closed 3 years ago

Ridermansb commented 3 years ago

I'm trying to create test for storage but.. TypeError: Cannot read property 'storageBucket' of null is throw

Version info

"firebase-admin": "^9.1.1".
"firebase-functions": "^3.11.0" "firebase-functions-test": "^0.2.2"

Test case

Creating new trigger for storage onFinalize and try to test it

Steps to reproduce

Create trigger function that will be trigger on onFinalize

processMenuImages: functions
    .runWith(runtimeStorageOpts)
    .storage.object()
    .onFinalize(processMenuImages),

The processMenuImages function it's just function..

const processMenuImages = async (object) => { .. code.. }

And try to test...

const functionsTest = require('firebase-functions-test')(
    FIREBASE_CONFIG,
    process.env.GOOGLE_APPLICATION_CREDENTIALS
);

describe('firebase triggers', function () {
    let firebaseFunctions;
    beforeEach(() => {
        firebaseFunctions = require('.');
    });
    afterEach(() => {
        functionsTest.cleanup();
    });

    test('should be able to call the function', () => {
        const storageData = functionsTest.storage.makeObjectMetadata({
            name: 'unit_tests/Amaze.png',
            contentType: 'image/png',
        });
        const wrapped = functionsTest.wrap(
            firebaseFunctions.processMenuImages
        );
        return wrapped(storageData);
    });
});

Actual behavior

Once I start the test, I received this...

    TypeError: Cannot read property 'storageBucket' of null

      42 |             contentType: 'image/png',
      43 |         });
    > 44 |         const wrapped = functionsTest.wrap(
         |                                       ^
      45 |             firebaseFunctions.processMenuImages
      46 |         );
      47 |         return wrapped(storageData);

      at resourceGetter (functions/node_modules/firebase-functions/lib/providers/storage.js:56:53)
      at Function.get (functions/node_modules/firebase-functions/lib/cloud-functions.js:151:17)
      at hasPath (functions/node_modules/lodash/lodash.js:6153:24)
      at Function.has (functions/node_modules/lodash/lodash.js:13185:32)
      at Object.wrap (functions/node_modules/firebase-functions-test/lib/main.js:29:19)
      at Suite._context4 (functions/firebase-triggers.test.js:44:39)

Expected behavior

Should all test working..

inlined commented 3 years ago

You need to either pass a bucket explicitly, e.g.

processMenuImages: functions
    .runWith(runtimeStorageOpts)
    .storage.bucket("SOME_BUCKET").object()
    .onFinalize(processMenuImages),

or you need to set the FIREBASE_CONFIG environment variable, which includes a default bucket name.