https://github.com/starhoshi/rescue-fire
A test helper for Cloud Functions.
https://github.com/starhoshi/rescue-fire
cloud-function-for-firebase cloud-functions firebase firestore javascript npm typescript
Last synced: 11 months ago
JSON representation
A test helper for Cloud Functions.
- Host: GitHub
- URL: https://github.com/starhoshi/rescue-fire
- Owner: starhoshi
- License: mit
- Created: 2018-02-09T12:46:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T06:34:56.000Z (about 8 years ago)
- Last Synced: 2025-04-22T00:49:07.569Z (11 months ago)
- Topics: cloud-function-for-firebase, cloud-functions, firebase, firestore, javascript, npm, typescript
- Language: TypeScript
- Homepage:
- Size: 1.31 MB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rescue-fire
[](https://badge.fury.io/js/rescue-fire)
[](https://travis-ci.org/starhoshi/rescue-fire)
[](https://coveralls.io/github/starhoshi/rescue-fire?branch=master)
[](https://www.codacy.com/app/kensuke1751/rescue-fire?utm_source=github.com&utm_medium=referral&utm_content=starhoshi/rescue-fire&utm_campaign=Badge_Grade)
[](https://opensource.org/licenses/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](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.
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 `. 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](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](https://facebook.github.io/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.
```ts
const changeName = (event: functions.Event) => {
console.log('old name', event.data.data().name)
return event.data.ref.update({ name: 'new name' })
}
```
The test will be like this.
```ts
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](https://github.com/starhoshi/rescue-fire/blob/master/src/index.ts#L18).
### 4. Finally, create Functions
```ts
exports.updateUser = functions.firestore
.document('users/{userId}')
.onCreate(event => {
return changeName(event)
})
```
Let's take a coffee break while deploying.