{"id":20926174,"url":"https://github.com/starhoshi/rescue-fire","last_synced_at":"2025-05-13T17:33:26.970Z","repository":{"id":57354508,"uuid":"120904822","full_name":"starhoshi/rescue-fire","owner":"starhoshi","description":"A test helper for Cloud Functions.","archived":false,"fork":false,"pushed_at":"2018-03-22T06:34:56.000Z","size":1373,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T00:49:07.569Z","etag":null,"topics":["cloud-function-for-firebase","cloud-functions","firebase","firestore","javascript","npm","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starhoshi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-09T12:46:35.000Z","updated_at":"2021-05-26T15:05:07.000Z","dependencies_parsed_at":"2022-09-12T03:20:33.522Z","dependency_job_id":null,"html_url":"https://github.com/starhoshi/rescue-fire","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Frescue-fire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Frescue-fire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Frescue-fire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhoshi%2Frescue-fire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starhoshi","download_url":"https://codeload.github.com/starhoshi/rescue-fire/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253993640,"owners_count":21996354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-function-for-firebase","cloud-functions","firebase","firestore","javascript","npm","typescript"],"created_at":"2024-11-18T20:37:08.645Z","updated_at":"2025-05-13T17:33:26.618Z","avatar_url":"https://github.com/starhoshi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/starhoshi/rescue-fire/master/docs/logo.png\" /\u003e\n\u003c/p\u003e\n\n# rescue-fire\n\n[![npm version](https://badge.fury.io/js/rescue-fire.svg)](https://badge.fury.io/js/rescue-fire)\n[![Build Status](https://travis-ci.org/starhoshi/rescue-fire.svg?branch=master)](https://travis-ci.org/starhoshi/rescue-fire)\n[![Coverage Status](https://coveralls.io/repos/github/starhoshi/rescue-fire/badge.svg?branch=master)](https://coveralls.io/github/starhoshi/rescue-fire?branch=master)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d78ab5f70d834668af4afa66421fe1ed)](https://www.codacy.com/app/kensuke1751/rescue-fire?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=starhoshi/rescue-fire\u0026amp;utm_campaign=Badge_Grade)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nThe 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.\n\n[Cloud Functions Emulator](https://firebase.google.com/docs/functions/local-emulator) is very useful. However, it is hard to create the test data json, and it is not possible to write tests.\n\nLet's emulate functions locally with rescue-fire and do TDD.\n\n## TODO\n\n* [x] Firestore\n* [ ] Realtime Database\n* [ ] Analytics\n* [ ] Auth\n* [ ] Pub/Sub\n* [ ] Strage\n\n## Concept\n\n\nrescue-fire can only create `functions.Event \u003cDeltaDocumentSnapshot\u003e`. But this is enough to simulate Cloud Functions.  \nLet's write a Cloud Functions test with this event.\n\n## Note\n\nThe 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.\n\n## Installation\n\n```\nnpm install rescue-fire --only=dev\nyarn add --dev rescue-fire\n```\n\n## Usage\n\norderable.ts uses rescue-fire to write tests. please refer.\nhttps://github.com/starhoshi/orderable.ts/blob/master/orderable/src/__tests__/orderable.test.ts\n\n### 1. Prepare Google Cloud Account Credentials\n\nDownload the service account key json file.\n\n[https://firebase.google.com/docs/admin/setup?authuser=0#add_firebase_to_your_app](https://firebase.google.com/docs/admin/setup?authuser=0#add_firebase_to_your_app)\n\nThis json file is sensitive, be careful.\n\n\u003e 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.\n\n### 2. Install testing library\n\nPlease use your favorite testing library.\n\nFor example, in the case of [Jest](https://facebook.github.io/jest/):\n\n```\nnpm install jest --only=dev\nyarn add --dev jest\n```\n\n### 3. Write a test\n\nLet's create a function to update name when user is created. The code of the function is as follows.\n\nThis sample is written in TypeScript.\n\n```ts\nconst changeName = (event: functions.Event\u003cDeltaDocumentSnapshot\u003e) =\u003e {\n  console.log('old name', event.data.data().name)\n  return event.data.ref.update({ name: 'new name' })\n}\n```\n\nThe test will be like this.\n\n```ts\nimport 'jest'\nimport * as admin from 'firebase-admin'\nimport * as functions from 'firebase-functions'\nimport * as Rescue from 'rescue-fire'\n\n// Set up to run firebase in local.\nbeforeAll(() =\u003e {\n  const serviceAccount = require('./your-firebase-adminsdk.json')\n  admin.initializeApp({\n    credential: admin.credential.cert(serviceAccount)\n  })\n})\n\ntest('update name', async () =\u003e {\n  // prepare\n  const data = {name: 'name'}\n  const user = await admin.firestore().collection('user').add(data)\n  const event = Rescue.event(user, data)\n\n  // start Cloud Functions\n  await changeName(event)\n\n  // expect name changed\n  const updatedUser = await admin.firestore().collection('user').doc(user.id).get()\n  expect(updatedUser.data()!.name).toBe('new name')\n})\n```\n\nCloud Functions can be developed with TDD. :tada: (strictly not TDD :smile:)  \nThis is a very small function, but larger functions can also be developed in this way.\n\nOptional Parameters defenitions is [here](https://github.com/starhoshi/rescue-fire/blob/master/src/index.ts#L18).\n\n### 4. Finally, create Functions\n\n```ts\nexports.updateUser = functions.firestore\n  .document('users/{userId}')\n  .onCreate(event =\u003e {\n    return changeName(event)\n})\n```\n\nLet's take a coffee break while deploying.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhoshi%2Frescue-fire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarhoshi%2Frescue-fire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhoshi%2Frescue-fire/lists"}