Test Firebase RealtimeDatabase using Jest
up vote
2
down vote
favorite
My purpose is simply to test one function. I cannot figure out how to mock firebase properly. I try to keep the example with axios mocking from Jest docs. I have the following code:
MusicService.js
import initializeApp from "firebase/app";
import "firebase/database";
const firebase = initializeApp(
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
);
export class MusicService
static getAlbums()
return firebase.database().ref("albums").once("value")
.then(snapshot => Object.values(snapshot.val()));
MusicService.test.js
import firebase from 'firebase/app';
import 'firebase/database';
import MusicService from './MusicService';
jest.mock('firebase/app');
jest.mock('firebase/database');
test("test", () =>
firebase.initializeApp.mockImplementation(() =>
database: jest.fn(() =>
return
ref: jest.fn()
)
);
MusicService.getAlbums();
);
The problem is that I get the following error:
I tried to mock firebase.database.
test("test", () =>
firebase.mockImplementation(() =>
return
database:
);
MusicService.getAlbums();
);
But in this case I get the error that says:
TypeError: _app.default.mockImplementation is not a function.
I don't expect the working example will be given, but rather could you tell please, what exactly should I mock? The whole firebase library or maybe the part where my function starts - return firebase.database()
.
javascript firebase unit-testing firebase-realtime-database jestjs
add a comment |
up vote
2
down vote
favorite
My purpose is simply to test one function. I cannot figure out how to mock firebase properly. I try to keep the example with axios mocking from Jest docs. I have the following code:
MusicService.js
import initializeApp from "firebase/app";
import "firebase/database";
const firebase = initializeApp(
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
);
export class MusicService
static getAlbums()
return firebase.database().ref("albums").once("value")
.then(snapshot => Object.values(snapshot.val()));
MusicService.test.js
import firebase from 'firebase/app';
import 'firebase/database';
import MusicService from './MusicService';
jest.mock('firebase/app');
jest.mock('firebase/database');
test("test", () =>
firebase.initializeApp.mockImplementation(() =>
database: jest.fn(() =>
return
ref: jest.fn()
)
);
MusicService.getAlbums();
);
The problem is that I get the following error:
I tried to mock firebase.database.
test("test", () =>
firebase.mockImplementation(() =>
return
database:
);
MusicService.getAlbums();
);
But in this case I get the error that says:
TypeError: _app.default.mockImplementation is not a function.
I don't expect the working example will be given, but rather could you tell please, what exactly should I mock? The whole firebase library or maybe the part where my function starts - return firebase.database()
.
javascript firebase unit-testing firebase-realtime-database jestjs
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My purpose is simply to test one function. I cannot figure out how to mock firebase properly. I try to keep the example with axios mocking from Jest docs. I have the following code:
MusicService.js
import initializeApp from "firebase/app";
import "firebase/database";
const firebase = initializeApp(
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
);
export class MusicService
static getAlbums()
return firebase.database().ref("albums").once("value")
.then(snapshot => Object.values(snapshot.val()));
MusicService.test.js
import firebase from 'firebase/app';
import 'firebase/database';
import MusicService from './MusicService';
jest.mock('firebase/app');
jest.mock('firebase/database');
test("test", () =>
firebase.initializeApp.mockImplementation(() =>
database: jest.fn(() =>
return
ref: jest.fn()
)
);
MusicService.getAlbums();
);
The problem is that I get the following error:
I tried to mock firebase.database.
test("test", () =>
firebase.mockImplementation(() =>
return
database:
);
MusicService.getAlbums();
);
But in this case I get the error that says:
TypeError: _app.default.mockImplementation is not a function.
I don't expect the working example will be given, but rather could you tell please, what exactly should I mock? The whole firebase library or maybe the part where my function starts - return firebase.database()
.
javascript firebase unit-testing firebase-realtime-database jestjs
My purpose is simply to test one function. I cannot figure out how to mock firebase properly. I try to keep the example with axios mocking from Jest docs. I have the following code:
MusicService.js
import initializeApp from "firebase/app";
import "firebase/database";
const firebase = initializeApp(
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
);
export class MusicService
static getAlbums()
return firebase.database().ref("albums").once("value")
.then(snapshot => Object.values(snapshot.val()));
MusicService.test.js
import firebase from 'firebase/app';
import 'firebase/database';
import MusicService from './MusicService';
jest.mock('firebase/app');
jest.mock('firebase/database');
test("test", () =>
firebase.initializeApp.mockImplementation(() =>
database: jest.fn(() =>
return
ref: jest.fn()
)
);
MusicService.getAlbums();
);
The problem is that I get the following error:
I tried to mock firebase.database.
test("test", () =>
firebase.mockImplementation(() =>
return
database:
);
MusicService.getAlbums();
);
But in this case I get the error that says:
TypeError: _app.default.mockImplementation is not a function.
I don't expect the working example will be given, but rather could you tell please, what exactly should I mock? The whole firebase library or maybe the part where my function starts - return firebase.database()
.
javascript firebase unit-testing firebase-realtime-database jestjs
javascript firebase unit-testing firebase-realtime-database jestjs
edited Nov 10 at 23:15
LazerBass
1,22631021
1,22631021
asked Nov 10 at 12:39
Lucknee
226
226
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239045%2ftest-firebase-realtimedatabase-using-jest%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown