Skip to content

Commit d6ea06d

Browse files
authored
Merge pull request #53 from CreateThrive/cloud-functions-unit-testing
Cloud functions unit testing
2 parents fd78fe1 + b081f7e commit d6ea06d

File tree

13 files changed

+945
-22
lines changed

13 files changed

+945
-22
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@
4949
"paths": ["src"]
5050
}
5151
}
52+
},
53+
"env": {
54+
"mocha": true
5255
}
5356
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@ yarn-debug.log*
8484
yarn-error.log*
8585

8686
.firebase
87+
88+
# service account key
89+
serviceAccountKey.json

README.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Boilerplate with React ⚛️ and Firebase 🔥designed to quickly spin up a ful
2323
- [Unit Testing](#unit-testing)
2424
- [Linting](#linting)
2525
- [Cloud functions](#cloud-functions)
26+
- [Unit Testing](#unit-testing-1)
2627
- [Prerequisites](#prerequisites)
2728
- [Getting started](#getting-started)
2829
- [Setting up the Firebase project locally](#setting-up-the-firebase-project-locally)
@@ -42,6 +43,7 @@ Boilerplate with React ⚛️ and Firebase 🔥designed to quickly spin up a ful
4243
- [Selecting an alias](#selecting-an-alias)
4344
- [Creating a new cloud function](#creating-a-new-cloud-function)
4445
- [Testing functions locally](#testing-functions-locally)
46+
- [Testing functions in online mode](#testing-functions-in-online-mode)
4547
- [Deployment](#deployment)
4648
- [Continuous integration/deployment](#continuous-integrationdeployment)
4749
- [Workflows folder structure](#workflows-folder-structure)
@@ -58,7 +60,7 @@ Boilerplate with React ⚛️ and Firebase 🔥designed to quickly spin up a ful
5860
- [How to internationalize a Date](#how-to-internationalize-a-date)
5961
- [How to add your language on DatePicker](#how-to-add-your-language-on-datepicker)
6062
- [File Upload](#file-upload)
61-
- [Image Resize](#image-resize)
63+
- [Image resize extension](#image-resize-extension)
6264
- [Storage Rules](#storage-rules)
6365
- [Contributors](#contributors)
6466
- [License](#license)
@@ -145,9 +147,13 @@ React Firebase Admin is our in-house admin dashboard boilerplate, used in many o
145147
- [Camelcase](https://github.com/sindresorhus/camelcase) (★ 423) convert a dash/dot/underscore/space separated string to camelCase.
146148
- [Resize Image](https://github.com/firebase/extensions/tree/master/storage-resize-images) (★ 372) Firebase Extension to create resized versions of images uploaded to Cloud Storage.
147149

148-
### Unit Testing
150+
#### Unit Testing
149151

150152
- [Firebase-functions-test](https://github.com/firebase/firebase-functions-test) (★ 117) unit testing library for Cloud Functions for Firebase.
153+
- [Mocha](https://github.com/mochajs/mocha) (★ 19.4k) simple, flexible, fun javascript test framework for node.js & the browser.
154+
- [Chai](https://github.com/chaijs/chai) (★ 6.8k) BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
155+
- [Chai-as-promised](https://github.com/domenic/chai-as-promised/) (★ 1.4k) Extends Chai with assertions about promises.
156+
- [Ts-node](https://github.com/TypeStrong/ts-node) (★ 7k) TypeScript execution and REPL for node.js, with source map support.
151157

152158
## Prerequisites
153159

@@ -324,15 +330,27 @@ functions/
324330
| │ └── onDelete.function.ts
325331
│ ├── db/
326332
│ │ ├── users/
327-
│ │ ├── onCreate.function.ts
328-
│ │ ├── onModify.function.ts
329-
│ │ └── ... other database functions ...
333+
│ │ ├── onCreate.function.ts
334+
│ │ ├── onModify.function.ts
335+
│ │ └── ... other database functions ...
330336
│ ├── storage/
331337
│ │ ... storage functions ...
332338
│ ├── https/
333339
│ │ ├── createUser.function.ts
334-
│ │ └── ... other https functions ...
340+
│ │ └── ... other https functions ...
335341
│ └── index.ts
342+
├── test/
343+
│ ├── db/
344+
│ │ ├── users/
345+
│ │ │ ├── onDelete.test.ts
346+
│ │ │ ├── onUpdate.test.ts
347+
│ │ │ └── ... other database tests ...
348+
│ ├── https/
349+
│ │ ├── createUser.test.ts
350+
│ │ └── ... other https tests ...
351+
│ └── util/
352+
│ ├── config.ts
353+
336354
```
337355

338356
### Installing dependencies
@@ -408,6 +426,24 @@ After it initializes, you should get your endpoints to test your HTTP functions:
408426
409427
More information about the [Firebase Emulator](https://firebase.google.com/docs/rules/emulator-setup).
410428
429+
### Testing functions in online mode
430+
431+
Testing your cloud functions online is very simple and easy.
432+
433+
For that, you only have to set the variables localted in the env.example.json inside /functions folder. (Remember to rename the file to env.json)
434+
435+
Follow these steps for setting up your env.json file:
436+
437+
- The first 3 properties **_"databaseURL"_**, **_"storageBucket"_** and **_"projectId"_** are the same ones previously added to the frontend .env file.
438+
- For **_"serviceAccountKey"_** you should do the following:
439+
- Go to your proyect in the Firebase dashboard, click on **_Project settings_** and then click on **_Service accounts_** tab.
440+
- After that you'll be able to click on **_Generate new private key_** button and a json file containing your service account's credentials will be downloaded.
441+
- Place that file in your project and include the location of it into the **_"serviceAccountKey"_** in your env.json file.
442+
443+
After that, open your terminal, navigate to the /functions folder and execute **npm test**.
444+
445+
_Warning: Use extra caution when handling service account credentials in your code. Do not commit them to a public repository, deploy them in a client app, or expose them in any way that could compromise the security of your Firebase project._
446+
411447
### Deployment
412448
413449
When a pull request gets merged into development, functions are deployed automatically to the staging project in Firebase. Likewise, when merging/pushing into master, they're deployed to production.
@@ -634,6 +670,7 @@ We'd like to thank these awesome people who made this whole thing happen:
634670
<li><a href="https://github.com/jbheber">Juan Heber</a></li>
635671
<li><a href="https://github.com/vikdiesel">Viktor Kuzhelny</a></li>
636672
<li><a href="https://github.com/TOPOFGR">Franco Galeano</a></li>
673+
<li><a href="https://github.com/jfocco">Juan Focco</a></li>
637674
</ul>
638675
639676
## License

functions/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ typings/
77

88
node_modules/
99

10-
lib/
10+
lib/
11+
12+
#Necessary config for testing
13+
env.json

functions/env.example.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"databaseURL": "",
3+
"storageBucket": "",
4+
"projectId": "",
5+
"serviceAccountKey": ""
6+
}

0 commit comments

Comments
 (0)