npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-firestore

v0.2.0

Published

Run your tests using Jest & Firestore Emulator

Downloads

803

Readme

jest-firestore

main-suite

Jest preset to run Firestore emulator automatically within your Jest Tests

This package is created to help people who are using Firestore in their projects to write tests without mocking Firestore.

Motivation

The firebase itself provides emulators suite with a emulators:exec command which helps to use emulator suite in testing scenarios. However, using emulators:exec command brings some drawbacks.

First, you are no longer able to run tests directly using Jest runner or from your IDE. You need to run it using firebase emulators:exec "npx jest" --only firestore command.

The other way would be to always have emulators running in the background while development to be able to run tests from IDE or Jest CLI. This is not a big deal, but it's inconvenient and might not be transparent for other team members.

The second and probably more important drawback is that emulators:exec doesn't work well in monorepo scenarios when you have multiple functions codebases.

Firebase CLI allows you to run only one emulator per project and doesn't allow to set port dynamically.

This package is created to solve these problems. It is heavily inspired by jest-mongodb and allows you to run Firestore emulator automatically within your Jest tests.

It uses native firebase-tools under the hood, so it's exactly the same emulator as you would expect with Firebase CLI.

Usage

0. Install

yarn add jest-firestore --dev

Make sure firebase-tools is installed in the project as well, as it's required as a peer dependency.

1. Create jest.config.js

module.exports = {
  preset: 'jest-firestore',
};

If you have a custom jest.config.js make sure you remove testEnvironment property, otherwise it will conflict with the preset.

By default, one emulator instance would be created for all Jest workers. You can achieve parallelism by leveraging emulator's multi database support.

2. Configure firebase-admin

Library sets the process.env.FIRESTORE_EMULATOR_HOST for your convenience so firebase-admin and other firebase tools will pick it up automatically.

import { type Firestore, getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

describe('insert', () => {
  let firestore: Firestore;

  beforeAll(() => {
    // setup deffiernet database for each jest worker
    const databaseName = 'test-' + process.pid;

    // `firebase-admin` automatically discover FIRESTORE_EMULATOR_HOST.
    const app = initializeApp();
    firestore = getFirestore(app, databaseName);
  });
});

3. PROFIT! Write tests

it('should insert a doc into collection', async () => {
  const users = firestore.collection('users');

  const mockUser = { _id: 'some-user-id', name: 'John' };
  const { id } = await users.add(mockUser);

  const insertedUser = await users.doc(id).get();

  expect(insertedUser.data()).toEqual(mockUser);
});

(optional) Configure Firestore emulator with jest-firebase-config.js

You can change some Firestore emulator options by creating jest-firebase-config.js file next to the jest.config.js file.

module.exports = {
  // full list of available options available here src/types.ts#Options
  firestoreEmulatorOptions: {
    project_id: 'demo-test-project',
    // todo: didn't tested yet, need to figure out is it relative or absolute path
    rules: 'firestore.rules', // optional path to rules file
  },
};

If you want to have a separate emulator instance for each Jest worker, create the following configuration:

module.exports = {
  firestoreEmulatorOptions: {
    // ...
  },
  useSharedDBForAllJestWorkers: false,
};

However, this is not recommended as it will slow down your tests.

5. Clean database before each test (optional)

See the firebase docs

beforeEach(async () => {
  await fetch(
    `http://${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/${process.env.GCLOUD_PROJECT}/databases/${databaseName}/documents`,
    { method: 'DELETE' },
  );
});

Bypassing starting emulator

You may want to bypass starting emulator during tests and connect to the external running emulator instead. Pass FIRESTORE_EMULATOR_HOST environment variable to your tests to achieve this.

FIRESTORE_EMULATOR_HOST=localhost:8080 jest

Firebase's emulators:exec command sets this variable automatically, so you can run tests using it as well:

firebase emulators:exec "jest"

Misc

Cache Firebase Emulators binary in CI by putting this folder to the list of cached paths: ~/.cache/firebase/emulators.

You can enable debug logs by setting environment variable DEBUG=jest-firestore:*

See Also

Publish

Run release workflow from GitHub Actions.

Or run manually:

npx release-it