I am a newbie in Firebase. I have read that it is good to unit test as you go. So this is what I have been trying to do lately. I currently have a problem when trying to test render on a class that uses Firebase. This is the following code I have been trying to fix:
import 'react-native';
import React from 'react';
import MainTab from '../../components/MainTab';
import Enzyme from 'enzyme';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer.create(
<MainTab/>
).toJSON();
expect(tree).toMatchSnapshot();
});
However, I am getting the following error on the test when trying to obtain the current user's id from firebase:
Has anyone stumbled into this error before? P.S. Don't go hard on me if this is something really vacuous, just trying to learn.
1. signInWithEmailAndPassword
signInWithEmailAndPassword(email, password) returns
firebase.Promise containing non-null firebase.User
Asynchronously signs in using an email and password.
This method will be deprecated and will be updated to resolve with a
firebase.auth.UserCredential as is returned in
firebase.auth.Auth#signInAndRetrieveDataWithEmailAndPassword.
For now, the implementation for this method is in below:
firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
// user signed in
// - user.displayName
// - user.email
// - user.uid // <---- the user's unique ID
})
You can see firebase.User properties here:
https://firebase.google.com/docs/reference/js/firebase.User
You can read more details about this question here https://stackoverflow.com/a/39959002/3332734
2. For your case...
If you need to test the email/password authentication, I suggest you to use a firebase "dev" project and testing with user credentials for this dev firebase project.
Be careful, don't store your UID for other tests!
Firebase dont't provides a test for authentication yet.
3. Firebase beta to 1.0 version
Easier unit testing using a new firebase-functions-test npm module that simplifies writing unit tests.
See more about it in:
Launching Cloud Functions for Firebase v1.0
Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0
I hope I have been useful!
Related
I have a cloud function that changes a user's custom claim when a certain field changes.
I'm writing unit test for my app using firebase emulators. I need to manually change the user claim (since functions won't run in testing mode). I want to test that the database denies access to a user after his custom claims have been changed by the cloud function
thank you
#firebase/testing is deprecated and was last updated Oct 2020.
Use #firebase/rules-unit-testing instead:
For v8.10.0 of the Web SDK, use npm install #firebase/rules-unit-testing#1.3.15 (from Aug 2021, the last update before Firebase v9).
For v9.0.0 onwards, in both modular and namespaced modes, use npm install #firebase/rules-unit-testing.
Technically they used to be the same package, but one is getting new updates whereas the other isn't. Because they were built from the same code base, their APIs are similar when dealing with the legacy namespaced Firebase SDK. For all new code, you should use #firebase/rules-unit-testing alongside the modular SDK and modular test methods.
Based on the documentation:
import * as firebase from "#firebase/rules-unit-testing"; // note this "firebase" is not the normal firebase namespace!
const testApp = firebase.initializeTestApp({
projectId: "my-test-project",
auth: {
uid: "alice",
email: "alice#example.com",
isAdmin: true, // custom claim
isModerator: true // custom claim
}
});
If using the normal Firebase namespace in your code, import the test library with this instead:
import * as firebase from "firebase"; // for v8 or older.
import * as firebaseTest from "#firebase/rules-unit-testing";
I'm trying to use Firebase in my web project, and I can't understand how it works. I do:
import app from "firebase/app"; //1
import firebase from 'firebase'; //2
console.log(app === firebase); //3
it outputs true.
If I use 2 string my code to get data from firebase works and I get message about not using proper SDK in production
If I use 1 string I get an error from:
app.initializeApp(firebaseConfig);
const db = app.firestore();
Uncaught TypeError:
firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.firestore is not a
function
Please help me to sort out how to fix this.
I've got it, webpack is in charge. It wraps imported modules, that's why they are equal. Though, don't know how to optimize my module loads using webpack and firebase simultaneously.
Currently I'm trying to implement firebase into my Vue.js App. I set up Vue.js using the Vue CLI, installed firebase as well as vuefire and bootstrap-vue, now I tried to get some data I inserted into my db manually but somehow I don't get any data from firebase. I'm quite new to Vue.js so this might be a beginners error but I don't really know what to do right now.
I don't know if it matters but I plan on using vuex later on, so I installed that as well, I've followed a tutorial on how to set up Vue.js, in there she is using a local js file as a database, I got it all set up but now I want to use Firebase as my database.
I think it would be a bit to much if I post every file thats relevant for this in here so I created a GitHub Repository with all the files, but here is the part where I think the problem might be:
<b-table striped hover :items="clients"></b-table>
{{ clients }}
import { db } from '#/environment/firebase'
export default {
data () {
return {
clients: [],
systems: []
}
},
firestore: {
clients: db.collection('clients'),
systems: db.collection('systems')
}
}
somehow Vue.js doesn't pull the data correctly. I thought this might have something to do with an authentication error from firebase but my console doesn't show any errors neither does my console where I'm running the npm run serve and at this point I don't really know what to do anymore.
It's also possibly that my error is in my inegrationfile of firebase since I just copied the one from the Get started page from vuefire:
import firebase from 'firebase'
import firebaseConfig from './firebaseConfig'
// Get a Firestore instance
export const db = firebase.initializeApp(firebaseConfig).firestore()
// Export types that exists in Firestore
// This is not always necessary, but it's used in other examples
const { TimeStamp, GeoPoint } = firebase.firestore
export { TimeStamp, GeoPoint }
But, as I said, I don't really know what my error is here since from my understanding everything should work up to this point..
In your main.js you included twice:
import './plugins/bootstrap-vue'
without including the vuefire.js plugin file.
i want to setup firebase inapp messaging on my flutter apps.
first, it need FirebaseInstanceId as explained here, so i can test send message to my device.
However the document does not tell more detail how to get it on Android, especially on flutter.
any idea ?
Note : someone has claimed that he can use inapp-messaging in flutter, please see here
Thank you in Advance...
The Firebase Instance ID can be fetched using -instanceIDWithHandler on iOS or getInstanceId on Android.
At the time of writing, I am not aware of a standalone plugin that does this, meaning that you have two options:
Write your own plugin that wraps the native implementations of those methods
Use the firebase_messaging Flutter plugin that happens to expose the Instance ID via its getToken() method
If you go the firebase_messaging route (mind you, it means you're adding another dependency), you can do something like this:
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
and then, somewhere in your code:
var token = await _firebaseMessaging.getToken();
print("Instance ID: $token");
Update
When you call get token, you get something in the following format: NNNNNNN:MMMMMMMMMMMMMMMMMM. Take only the first part before the colon (NNNNNNNN) - this is the instance ID you need to test your In-App Messaging campaign.
You can run this in your terminal.
adb logcat | grep 'InAppMessaging'
02-26 23:47:44.835 28379 28379 I FIAM.Headless: Starting
InAppMessaging runtime with Installation ID dTEmOkEwRzC_xiO8YNPXs0
To retrieve the FirebaseInstanceId you need to implement the Firebase In-App messaging SDK and make sure your app connects to Firebase.
Once the app is connected to Firebase it will add the FirebaseInstanceId to the Android log once the App is run.
In the article you linked Google explains that you can find the FirebaseInstanceId by looking in the log for the string I/FIAM.Headless: Starting InAppMessaging runtime with Instance ID <YOUR_APP_ID>.
If you are using Android Studio you should be able to use the logcat window to browse the logs of the device (while debugging the App). The logcat window will also allow you to search and filter the logs so it should be relative easy to find the mentioned string.
So in short (in Android Studio):
Implement the Firebase In-App messaging SDK in your App;
Start debugging the App (preferably on a real Android device);
While debugging open the Logcat window;
Search the logs for the string I/FIAM.Headless: Starting InAppMessaging runtime with Instance ID;
The FirebaseInstanceId should be listed directly after the string.
Using flutterfire_installations, you can get:
Installation ID:
String id = await FirebaseAppInstallations.instance.getId();
Authentication token:
String token = await FirebaseAppInstallations.instance.getToken();
Im trying to implement authentication on my SPA, and it works fine.
The way im doing it is like in the docs:
import firebase from 'firebase/app';
import 'firebase/auth';
const config = {
// ...
};
const firebaseApp = firebase.initializeApp(config);
// and somewhere else in the code(with all the arguments and stuff)
firebase.auth().createUserWithEmailAndPassword();
firebase.auth().signInWithEmailAndPassword();
Now, my problem is that these imports add too much to the app. I honestly don't think that 180KB(minified, but not compressed) is acceptable for an SPA that aims to work on mobile.
Just for comparison, my whole app, Vuejs + Router + Vuex + other 3~ small libraries and the app logic weight 170KB(minified but not compressed).
So I wanted to know if there is another solution, or if im doing it wrong, or if there is an easy workaround. Ideally, I would be able to just make an HTTP request and get back a JWP.
Well, I contacted firebase to add this as a feature request, and they suggested me to use for now the REST API for auth. It requires a custom token, but it is a good solution.
The documentation for the REST API:
https://firebase.google.com/docs/reference/rest/auth/
As far as I know, the actual endpoints you hit for Firebase auth isn't publicly documented nor have I seen them around the Web. With that said, if you are set on reducing your bundle size, your best bet is to pick apart the SDK and figure out how and what you need to construct.
signInWithEmailAndPassword is defined here and as you can see, it just calls out to another private function and so on.