Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Unit test your Angular/Firebase applications using a mocked-out Firebase provider.
So you're writing your amazing new web application using Angular and Firebase. Because you're a good developer, you want to write unit tests that don't need to connect to a real Firebase instance to run correctly. So you need a mock for Firebase. Luckily, one already exists: MockFirebase.
But how do you seamlessly switch between using MockFirebase in your tests, and real Firebase in production? Simple. Use Angular's own dependency injection (DI) framework.
Now you can inject the $firebaseRef and $firebaseUser objects anywhere in Angular. $firebaseRef is a proxy for new Firebase(url);
, and $firebaseUser is a proxy for new FirebaseSimpleLogin(method, options);
.
$firebaseRefProvider.domain('myinstance.firebaseio.com');
...
$firebaseRef('/users/fred/favorites').push('Maria');
This way you can easily change which Firebase instance your app points to for purposes of integration testing or migration.
Depending on configuration, $firebaseRef is either a proxy to Firebase or a mock object like MockFirebase. An example of how to do this is provided in examples/myApp.js and examples/myApp_test.js.
Although a function, $firebaseRef has three read-only properties for introspection:
protocol
: either http
or https
.domain
: the domain the firebaseRef points to.mocked
: boolean. Tells whether the $firebaseRef is mocked out.You can configure $firebaseRef either by setting values in your Angular module or by configuring $firebaseRefProvider. Settings on the provider override the values.
firebaseDomain
: the domain name of the Firebase instance to point to.firebaseProtocol
: Either http
or https
. Defaults to https.firebaseMock
: a mock constructor to substitute for Firebase.firebaseMockData
: A plain old Javascript object that Fingular will interrogate and use to provision the Firebase mock object with data.Example use:
angular.module('myApp', ['fingular'])
.value('firebaseDomain', 'metropolis.firebaseio.com')
.value('firebaseProtocol', 'http') // Don't ever do this. Have you learned nothing, Fritz?
.value('firebaseMock', MockFirebase)
.value('firebaseMockData', {
'users': {
'fred': {
'name': 'Freder Frederson',
'hometown': 'Metropolis'
}
}
});
domain(domainName)
: the domain name of the Firebase instance to point to.protocol(protocolName)
: Either http
or https
. Defaults to https.mockWith(constructorFunction)
: a mock constructor to substitute for Firebase.mockOut(keyPath, value)
: Have the mock Firebase object supply the given data for the given path. Currently only works with MockFirebase. (If you wish to use your own mock, value
will be supplied as a second argument to the mock constructor function.Example use:
angular.module('myApp', ['fingular'])
.config(function($firebaseRefProvider) {
$firebaseRefProvider
.domain('test.firebaseio.com')
.mockWith(MockFirebase)
.mockOut('/users/fred', {
name: 'Freder Frederson',
hometown: 'Metropolis'
});
});
You can use $firebaseUser in place of FirebaseSimpleLogin to obtain some advantages vis-a-vis testing and some bonus Angular integration to boot.
angular.module('myModule', ['fingular'])
.controller('myController', function($firebaseUser, $rootScope) {
if ($rootScope.firebaseUser.$anonymous) {
console.log('Logging in...');
$firebaseUser.login.then(function(user) {
console.log('user logged in!');
console.log(user);
}, function(err) {
console.error('login failed!');
console.error(err);
});
} else {
console.log($rootScope.firebaseUser);
$rootScope.firebaseUserRef.on('value', function(userInfo) {
console.log(userInfo.val());
});
}
});
You can configure $firebaseUser either by setting values in your Angular module or by configuring $firebaseUserProvider. Settings on the provider override the values.
firebaseUserMock
: The mock user constructor to substitute for FirebaseSingleLogin. Ordinarily you'll want to use MockFirebaseSimpleLogin.firebaseUserMockData
: user data to be handed to the mocking framework.Thanks to firebase-debug, on npm test
you'll see this error:
Error loading resource fingular/test/deps.js (203). Details: Error opening fingular/test/deps.js: No such file or directory
You can just ignore that.
MIT
FAQs
A Firebase service provider for AngularJS designed for testability.
The npm package fingular receives a total of 0 weekly downloads. As such, fingular popularity was classified as not popular.
We found that fingular demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.