'process.env.FIREBASE_CONFIG is undefined when running all unit tests together

When running all my tests together, the makeDocumentSnapshot call gives error SyntaxError: Unexpected token u in JSON at position 0 at firebase-functions-test/lib/app.js:41:65, which is basically that process.env.FIREBASE_CONFIG being undefined. However, if I just run the test individually, it passes.

My file structure

src
 |-- foo
      |-- index.ts
      |-- foo.ts
 |-- index.ts

test
 |-- test.ts
 |-- foo
      |-- foo.test.ts

package.json { "test": "mocha -r ts-node/register test/**/*.test.ts" }
// index.ts

import * as admin from 'firebase-admin';

admin.initializeApp();
admin.firestore().settings({ ignoreUndefinedProperties: true });

export * as foo from './foo/index';
// test.ts

import Test from 'firebase-functions-test';

export default Test({ databaseURL, storageBucket, projectId }, 'test/service-account-key.json');
// foo.test.ts

import test from '../test';
import * as myFunctions from '../../src/index';

describe('foo()', () => {
  const wrapped = test.wrap(myFunctions.foo.foo);

  it ('should do this', async () => {
    // This line gives error when running all tests together, 
    // but not when running this test individually with 
    // describe.only().
    const snapshot = test.firestore.makeDocumentSnapshot(...) 
    ...
  });
});


Solution 1:[1]

Follow the official guide for Initializing Firebase Test SDK for Cloud Functions, and see if this helps your use case:

// At the top of test/index.test.js
const test = require('firebase-functions-test')({
  databaseURL: 'https://my-project.firebaseio.com',
  storageBucket: 'my-project.appspot.com',
  projectId: 'my-project',
}, 'path/to/serviceAccountKey.json');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gourav B