Write Tests Write unit tests with mock data for Firestore Security Rules.

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

command line
firebase emulators:start
file_type_js_official rules.test.js
describe('Database rules', () => {
    let db;
  
    // Applies only to tests in this describe block
    beforeAll(async () => {
      db = await setup(mockUser, mockData);
    });
  
    afterAll(async () => {
      await teardown();
    });
  
    test('deny when reading an unauthorized collection', async () => {
      const ref = db.collection('secret-stuff');

      expect( await assertFails( ref.get() ) );
  
    });

    test('allow admin to read unpublished posts', async () => {
      const ref = db.doc('posts/abc');

      expect( await assertSucceeds( ref.get() ) );
  
    });

    test('allow admin to update posts of other users', async () => {
      const ref = db.doc('posts/abc');

      expect( await assertSucceeds( ref.update({ published: true }) ) );
  
    });
    
  });

Q&A Chat