Fireship · @fireship

Firestore Rules Testing with the Emulator - New Feature

Uploaded November 1, 2018 · Length 11:45

Firestore Rules Testing with the Emulator - New Feature

Summary

Learn how to test Firestore rules using the emulator and Jest, including setting up the emulator, writing test cases, and using helpers to make testing easier.

  • firestore rules testing
  • firebase emulator
  • jest testing
  • firebase security rules
  • cloud firestore testing

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    fire base summit wrapped up a few days

  3. 0:08

    ago and although I wasn't able to make

  4. 0:09

    it in person they did announce a bunch

  5. 0:11

    of cool new features the feature that is

  6. 0:13

    most exciting to me is the local

  7. 0:14

    emulator for Fighter Store and the

  8. 0:16

    real-time database the emulators allow

  9. 0:18

    you to run an instance of your database

  10. 0:19

    locally meaning you can build up a test

  11. 0:21

    suite around your security rules

  12. 0:23

    this not only speeds up development time

  13. 0:25

    but it also increases the integrity of

  14. 0:27

    your back-end security in today's video

  15. 0:29

    you'll learn how to set up the emulator

  16. 0:30

    and build out a full test suite using

  17. 0:32

    chests if you're new here like and

  18. 0:34

    subscribe and make sure to grab the full

  19. 0:36

    source code from angular firebase comm

  20. 0:38

    now before we get going with the

  21. 0:39

    emulator I'd also like to point out that

  22. 0:41

    firebase announced a new project

  23. 0:42

    management API this API makes it

  24. 0:45

    possible for apps to manage firebase

  25. 0:47

    projects of other users for example you

  26. 0:49

    can now deploy your firebase projects

  27. 0:51

    directly from stack blitz or glitch but

  28. 0:53

    the more exciting thing going on there

  29. 0:54

    is that you can use this project

  30. 0:56

    management API to build your own apps

  31. 0:58

    that do the exact same thing so let me

  32. 1:00

    know in the comments if you want to see

  33. 1:01

    that covered in a future video so now

  34. 1:03

    let's get started talking about the

  35. 1:04

    emulator the emulator just runs fire

  36. 1:06

    store or the real-time database locally

  37. 1:08

    on your machine this means you can now

  38. 1:10

    simulate interaction with the database

  39. 1:12

    without actually having to run it in the

  40. 1:13

    cloud this makes it so much easier to be

  41. 1:16

    confident that a complex set of security

  42. 1:18

    rules are working the way that they

  43. 1:19

    should I'm gonna go ahead and set this

  44. 1:21

    project up with just a vanilla

  45. 1:22

    JavaScript and just as the testing

  46. 1:24

    library the reason I choose jest is

  47. 1:26

    because it's very developer friendly and

  48. 1:28

    also really good at testing asynchronous

  49. 1:30

    code to start testing your rules in

  50. 1:32

    isolation

  51. 1:32

    I'd recommend starting from a brand new

  52. 1:34

    directory and then install the latest

  53. 1:36

    version of firebase tools once that's

  54. 1:38

    done you can run firebase init fire

  55. 1:40

    store and that will pull in your

  56. 1:42

    existing security rules from whichever

  57. 1:43

    project you select from there you can

  58. 1:45

    run NPM in it to set up a package.json

  59. 1:47

    in this folder then after that we can

  60. 1:50

    install our testing utilities which

  61. 1:51

    include at firebase testing and jest

  62. 1:54

    then we'll give us a directory structure

  63. 1:56

    that looks something like this I've also

  64. 1:58

    created a spec folder that includes our