Every single time I made use of Firebase, I encountered the issue of just how to check Firebase’s data source as well as verification. Considering that I am making use of Jest as my default screening setting, I figured whatever I required currently includes Jest. In this tutorial, you will certainly find out just how to simulated Firebase’s functions. We will certainly make use of Firebase Admin SDK for the Firebase configuration, nevertheless, the exact same collaborate with the standard client-side Firebase making use of Firebase Real-Time Data source, Firebase Firestore, as well as Firebase Verification.
import * as firebaseAdmin from ' firebase-admin';
import firebaseServiceAccountKey from './ firebaseServiceAccountKey.json';
if (! firebaseAdmin applications size) {
firebaseAdmin initializeApp( {
credential: firebaseAdmin credential cert(
firebaseServiceAccountKey
),
databaseURL: ' https://my-firebase-application.firebaseio.com',
} );
}
export default firebaseAdmin;
After establishing Firebase, we have our initial data source feature which produces a document in Firebase’s data source:
import firebaseAdmin from './ firebase';
export const createCourse = async ( {
uid,
courseId,
bundleId,
quantity,
paymentType,
} ) =>> {
wait for firebaseAdmin
data source()
ref(' customers/$ { uid} / training courses')
press()
collection( {
courseId: courseId,
packageId: bundleId,
billing: {
createdAt: firebaseAdmin data source ServerValue TIMESTAMP,
quantity,
licensesCount: 1,
money: ' USD',
paymentType,
} ,
} );
return real;
}
In our examination documents, an examination with Jest can be comparable to this set for examining the Firebase feature:
import { createCourse } from './';
import firebaseAdmin from './ firebase';
explain(' createFreeCourse', () =>> {
it(' produces a program', async () =>> {
const collection = firebaseAdmin
data source()
ref()
press() collection;
const result = createCourse(
' 1',
' THE_ROAD_TO_GRAPHQL',
' TRAINEE',
0,
' FREE'
);
wait for anticipate( result) settles toEqual( real);
anticipate( collection) toHaveBeenCalledTimes( 1);
anticipate( collection) toHaveBeenCalledWith( {
courseId: ' THE_ROAD_TO_GRAPHQL',
packageId: ' TRAINEE',
billing: {
createdAt: ' TIMESTAMP',
quantity: 0,
licensesCount: 1,
money: ' USD',
paymentType: ' FREE',
} ,
} );
} );
} );
Prior to this examination can go through, we require to simulated Firebase in the examination documents to cover the bothersome lines (highlighted). As opposed to buffooning Firebase as collection, we simulated the configuration which takes place in an additional documents which I have actually revealed prior to:
import { createCourse } from './';
import firebaseAdmin from './ firebase';
jest simulated('./ firebase', () =>> {
const collection = jest fn();
return {
data source: jest fn(() =>> ( {
ref: jest fn(() =>> ( {
press: jest fn(() =>> ( {
collection,
} )),
} )),
} )),
} ;
} );
explain(' createFreeCourse', () =>> {
...
} );
Currently it’s feasible to call Jest’s
toHaveBeenCalledTimes()
as well astoHaveBeenCalledWith()
on the mocked feature. Nevertheless, we still really did not simulated the Firebase timestamp effectively. In our resource code, allow’s make use of the specific Firebase import instead of our Firebase configuration for the timestamp:import * as firebaseAdminVanilla from ' firebase-admin';
import firebaseAdmin from './ firebase';
export const createCourse = async ( {
uid,
courseId,
bundleId,
quantity,
paymentType,
} ) =>>
wait for firebaseAdmin
data source()
ref(' customers/$ { uid} / training courses')
press()
collection( {
courseId: courseId,
packageId: bundleId,
billing: {
createdAt: firebaseAdminVanilla data source ServerValue TIMESTAMP,
quantity,
licensesCount: 1,
money: ' USD',
paymentType,
} ,
} );
Currently, we can Banter simulated the Firebase import for the Firebase constant in our examination:
import { createCourse } from './';
import firebaseAdmin from './ firebase';
jest simulated(' firebase-admin', () =>> {
return {
data source: {
ServerValue: {
TIMESTAMP: ' TIMESTAMP',
} ,
} ,
} ;
} );
jest simulated('./ firebase', () =>> {
...
} );
explain(' createFreeCourse', () =>> {
...
} );
The Firebase constant must be alright in our examination assertion currently. Additionally you can think about relocating the last Firebase simulated right into your
jest.setup.js
documents, since it might be required for various other device as well as assimilation examinations also. Besides, you must have whatever at your hands to check Firebase applications currently.