Slide 1

Slide 1 text

Practical Testing of Firebase Projects

Slide 2

Slide 2 text

github.com/ariya/hello-firebase-experiment

Slide 3

Slide 3 text

Hosting

Slide 4

Slide 4 text

$ npm run firebase -- emulators:start i hosting: Serving hosting files from: public/ ✔ hosting: Local server: http://localhost:5000 ✔ hosting: hosting emulator started at http://localhost:5000

Slide 5

Slide 5 text

Functions

Slide 6

Slide 6 text

Health Check exports.ping = functions.https.onRequest((request, response) => { response.send(`OK`); });

Slide 7

Slide 7 text

Health Check exports.ping = functions.https.onRequest((request, response) => { response.send(`OK ${Date.now()}`); });

Slide 8

Slide 8 text

it('should have a working ping function', async function () { const res = await axios.get('http://localhost:5000/ping'); const status = res.data.substr(0, 2); const timestamp = res.data.substr(3); expect(status).toEqual('OK'); expect(timestamp).toMatch(/[0-9]+/); });

Slide 9

Slide 9 text

$ npm run firebase -- emulators:exec "npm test" i hosting: Serving hosting files from: public/ ✔ hosting: Local server: http://localhost:5000 ✔ hosting: hosting emulator started at http://localhost:5000 ✔ functions[ping]: http function initialized (http://localhost:5001/firebase/us- central1/ping). i Running script: npm test > npm run jasmine Randomized with seed 06465 Started i functions: Beginning execution of "ping" 127.0.0.1 - - [22/Oct/2020:17:49:19 +0000] "GET /ping HTTP/1.1" 200 16 "-" "axios/0.19.2" i functions: Finished "ping" in ~1s . 1 spec, 0 failures

Slide 10

Slide 10 text

Firestore

Slide 11

Slide 11 text

exports.answer = functions.https.onRequest(async (request, response) => { try { const doc = await db.collection('universe').doc('answer').get(); const value = doc.data().value; response.send(`Answer is ${value}`); } catch (err) { response.send(`EXCEPTION: ${err.toString()}`); } });

Slide 12

Slide 12 text

$ npm run firebase -- emulators:exec "npm test" EXCEPTION: TypeError: Cannot read property 'value' of undefined

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

$ npm run firebase -- emulator:export spec/fixture

Slide 15

Slide 15 text

$ npm run firebase -- emulators:exec "npm test" --import spec/fixture ✔ firestore: firestore emulator started at http://localhost:8080 i hosting: Serving hosting files from: public/ ✔ hosting: Local server: http://localhost:5000 ✔ hosting: hosting emulator started at http://localhost:5000 i Running script: npm test i functions: Beginning execution of "answer" > Answer is 42 127.0.0.1 - - [22/Oct/2020:18:30:39 +0000] "GET /answer HTTP/1.1" 200 12 "-" "axios/0.19.2" i functions: Finished "answer" in ~1s 1 spec, 0 failures Finished in 0.814 seconds ✔ Script exited successfully (code 0)

Slide 16

Slide 16 text

Epilog

Slide 17

Slide 17 text

name: Tests jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Use Node.js uses: actions/setup-node@v1 with: node-version: 10.x - run: npm ci - run: npm run firebase -- emulators:exec "npm test" --import fixture/ --project hello env: CI: true GitHub Actions

Slide 18

Slide 18 text

Thank You Some artworks are from http://openclipart.org. @ariyahidayat