npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gp-firebase-emulator-unit-test

v2.1.0

Published

A unit test lib to use with the firebase emulator

Downloads

28

Readme

License node npm

gp-firebase-emulator-unit-test

A unit test lib to use with the Firebase emulator

About

This projects aims at providing a set of tools to unit test a Firebase project using the emulator suite.

It currently supports:

  • Authentication
  • Firebase
  • Functions
  • Storage

While there are a few alternative provided, this one used the Emulator Hub to gather Emulators config (which one are activated, and its hostname and port number). This allow to run the unit test separately from running the emulator, though it also works as running it as part of the emulator.

Requirements

  • Node LTS 18.x (not tested on 10.x, 12.x, 14.x and 16.x)
  • firebase-tools 11.x

Installation

At the moment, only NPM install is supported. To add this package to your project, simply run:

npm install --save-dev gp-firebase-emulator-unit-test

To add to your project using Javascript, you can can do something like:

const gpFirebaseEmulUT = require('gp-firebase-emulator-unit-test');

If you are using Typescript, you can get the whole package with something like:

import * as gpFirebaseEmulUT from 'gp-firebase-emulator-unit-test';

Example (Typescript)

Here's a simple example which would create a user, post document and check the document.

import * as firebaseAuth from 'firebase/auth';
import * as firebaseFirestore from 'firebase/firestore';
import {
  initAdminTestApp,
  initTestApp,
  assertSucceeds,
  sleep,
  firebaseAuth,
  firebaseFirestore
  } from 'gp-firebase-emulator-unit-test';

// Initialize the firebase test app, ssuming the Emulator Hub runs locally on
// the standard port
const firebaseTestApp = await initTestApp({ projectId: projectId, region: region });

// Get the Auth and Firebase
const auth = firebaseTestApp.auth;
const db = firebaseTestApp.firestore;

// Step 1: Create a user and immediatly sign out
const userCredential = await assertSucceeds(firebaseAuth.createUserWithEmailAndPassword(auth!, '[email protected]', 'Test+1234'));
await assertSucceeds(firebaseAuth.signOut(auth!));

// Step 2: Post the document
await firebaseTestApp.runAuthenticated('[email protected]', 'Test+1234', async (userCredential) => {
  const doc = { hello: "world !" };
  await assertSucceeds(firebaseFirestore.setDoc(firebaseFirestore.doc(db!, `/doc/${userCredential.user.uid}`), doc)));
}

// Give it a second (If using mocha, retrying the test is better)
sleep(1000);

// Step 3: Check the document posted (a framework like Mocha/Chai is recommended
// typically creating a function that run the control on the ducment, and using
// a call like:
// await assertSucceeds(checkDocument(postedDoc)));
await firebaseTestApp.runAuthenticated('[email protected]', 'Test+1234', async (userCredential) => {
  const postedDoc = await assertSucceeds(firebaseFirestore.getDoc(firebaseFirestore.doc(db!, `/doc/${userCredential.user.uid}`)));
  if (postedDoc.exists) {
    if (postedDoc.data().hello !== 'world!') {
      // Handle the error
    }
  } else {
    // Handle the error
  }
}

A more detail example (in Typescript, using Mocha/Chai will be provided later).

Documentation

The documentation to the api can be found here

Build and unit test

If you wish to build the source code and embed it in your app, or build and test a changes you would like to submit, please read the build instructions here.

Change log

For a full hostory of the changes, have a look at the change log.

Known issues

Please check the known issue list, which would provide also workarounds.

Acknowledgement

This is strongly inspired by the npm package @firebase/rules-unit-testing part of the firebase/firebase-js-sdk repository.

Contributions

See instructions here.

License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See license here.