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

rescue-fire

v0.2.5

Published

A test helper for Cloud Functions

Downloads

3

Readme

rescue-fire

npm version Build Status Coverage Status Codacy Badge License: MIT

The deployment of Cloud Functions is very slow. Deployment is normally completed in 30 seconds, but it sometimes take more than 10 minutes. It is a waste of time to wait a few minutes just by rewriting one line.

Cloud Functions Emulator is very useful. However, it is hard to create the test data json, and it is not possible to write tests.

Let's emulate functions locally with rescue-fire and do TDD.

TODO

  • [x] Firestore
  • [ ] Realtime Database
  • [ ] Analytics
  • [ ] Auth
  • [ ] Pub/Sub
  • [ ] Strage

Concept

rescue-fire can only create functions.Event <DeltaDocumentSnapshot>. But this is enough to simulate Cloud Functions.
Let's write a Cloud Functions test with this event.

Note

The event created by rescue-fire is not complete. We think that it is enough to write tests, but keep in mind that it is different from the actual event.

Installation

npm install rescue-fire --only=dev
yarn add --dev rescue-fire

Usage

orderable.ts uses rescue-fire to write tests. please refer. https://github.com/starhoshi/orderable.ts/blob/master/orderable/src/tests/orderable.test.ts

1. Prepare Google Cloud Account Credentials

Download the service account key json file.

https://firebase.google.com/docs/admin/setup?authuser=0#add_firebase_to_your_app

This json file is sensitive, be careful.

Important: This file contains sensitive information, including your service account's private encryption key. Keep it confidential and never store it in a public repository.

2. Install testing library

Please use your favorite testing library.

For example, in the case of Jest:

npm install jest --only=dev
yarn add --dev jest

3. Write a test

Let's create a function to update name when user is created. The code of the function is as follows.

This sample is written in TypeScript.

const changeName = (event: functions.Event<DeltaDocumentSnapshot>) => {
  console.log('old name', event.data.data().name)
  return event.data.ref.update({ name: 'new name' })
}

The test will be like this.

import 'jest'
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
import * as Rescue from 'rescue-fire'

// Set up to run firebase in local.
beforeAll(() => {
  const serviceAccount = require('./your-firebase-adminsdk.json')
  admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
  })
})

test('update name', async () => {
  // prepare
  const data = {name: 'name'}
  const user = await admin.firestore().collection('user').add(data)
  const event = Rescue.event(user, data)

  // start Cloud Functions
  await changeName(event)

  // expect name changed
  const updatedUser = await admin.firestore().collection('user').doc(user.id).get()
  expect(updatedUser.data()!.name).toBe('new name')
})

Cloud Functions can be developed with TDD. :tada: (strictly not TDD :smile:)
This is a very small function, but larger functions can also be developed in this way.

Optional Parameters defenitions is here.

4. Finally, create Functions

exports.updateUser = functions.firestore
  .document('users/{userId}')
  .onCreate(event => {
    return changeName(event)
})

Let's take a coffee break while deploying.