I have the following code for those 2 module the goal is to test this v2 cloud function, and i am getting the following 3 errors. any clue of what is causing this.
FAIL lib/functions/functions.test.js
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules/@jest/core/build/TestScheduler.js:133:18)
at node_modules/@jest/core/build/TestScheduler.js:254:19
at node_modules/emittery/index.js:363:13
at Array.map (<anonymous>)
at Emittery.emit (node_modules/emittery/index.js:361:23)
FAIL lib/index.test.js
● Test suite failed to run
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
2 | import * as admin from 'firebase-admin';
3 | // connect the firestore DB
> 4 | const firestore = admin.firestore();
| ^
5 | // External Libraries
6 | import * as CryptoJs from 'crypto-js';
7 | // Types
at AppStore.getApp (node_modules/firebase-admin/lib/app/lifecycle.js:65:19)
at FirebaseNamespaceInternals.app (node_modules/firebase-admin/lib/app/firebase-namespace.js:54:35)
at FirebaseNamespace.app (node_modules/firebase-admin/lib/app/firebase-namespace.js:256:30)
at FirebaseNamespace.ensureApp (node_modules/firebase-admin/lib/app/firebase-namespace.js:266:24)
at FirebaseNamespace.fn (node_modules/firebase-admin/lib/app/firebase-namespace.js:136:25)
at Object.<anonymous> (src/functions/onCallFunctions.ts:4:25)
at Object.<anonymous> (index.test.ts:4:1)
FAIL src/test/online.test.ts
● Test suite failed to run
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
1 | import * as admin from 'firebase-admin';
> 2 | const firestore = admin.firestore();
| ^
3 |
4 | // import * as functionsHttps from 'firebase-functions/v2/https';
5 | import { onDocumentCreated } from 'firebase-functions/v2/firestore';
at AppStore.getApp (node_modules/firebase-admin/lib/app/lifecycle.js:65:19)
at FirebaseNamespaceInternals.app (node_modules/firebase-admin/lib/app/firebase-namespace.js:54:35)
at FirebaseNamespace.app (node_modules/firebase-admin/lib/app/firebase-namespace.js:256:30)
at FirebaseNamespace.ensureApp (node_modules/firebase-admin/lib/app/firebase-namespace.js:266:24)
at FirebaseNamespace.fn (node_modules/firebase-admin/lib/app/firebase-namespace.js:136:25)
at Object.<anonymous> (src/functions/v2cloudFunctions.ts:2:25)
at Object.<anonymous> (src/test/online.test.ts:3:1)
Test Suites: 3 failed, 3 total
Tests: 0 total
Snapshots: 0 total
Time: 2.984 s
Ran all test suites.
// fucntion module
import * as admin from 'firebase-admin';
const firestore = admin.firestore();
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
export const lowercaseData = onDocumentCreated('testDoc/{docId}', async (event) => {
if (!event.data) {
return;
}
const data = event.data.data();
const bio = data.bio.toLowerCase();
await firestore.doc(`testDoc/${event.params.docId}`).update({ bio });
});
// Test module
import 'jest';
import * as functions from 'firebase-functions-test';
import { lowercaseData } from '../functions/v2cloudFunctions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const firestore = admin.firestore();
const testEnv = functions(
{
databaseURL: 'xxxx',
projectId: 'xxx',
},
'./service-account-key.json'
);
describe('lowercaseData', () => {
let wrapped: any | null = null;
// Applies only to tests in this describe block
beforeAll(() => {
wrapped = testEnv.wrap(lowercaseData as any);
});
it('converts the bio to lowercaseData', async () => {
const path = 'testDoc/data1';
const data = { bio: 'BIG' };
// Create a Firestore snapshot
const snap = testEnv.firestore.makeDocumentSnapshot(data, path);
// Execute the function
await wrapped(snap);
const after = await firestore.doc(path).get();
expect(after?.data()?.bio).toBe('big');
});
});