1.0.15 • Published 2 years ago

ts-firebase-driver-testing v1.0.15

Weekly downloads
1,623
License
MIT
Repository
github
Last release
2 years ago

Firebase Driver Testing

Build Status npm version

Swap out Firebase as a driver for in-process testing

This package allows easier high-level testing of business logic in Firebase applications by providing the ability to swap out Firebase as a "driver".

The Firebase Test SDK for Cloud Functions has a similar use-case, but requires a lot more meddling with Firebase internals to stub out the parts of the system that interact with the outside world.

ts-firebase-driver-testing instead provides an interface for Firebase as a driver that can be swapped out in one go, using either the real Firebase or an in-memory version that exists in a single process with your tests.

Installation

Install via NPM:

npm i ts-firebase-driver-testing

Or via Yarn:

yarn add ts-firebase-driver-testing

Usage

ts-firebase-driver-testing provides an interface called IFirebaseDriver. This interface gives access to various parts of the Firebase API, such as Firebase Realtime Database.

Instead of grabbing the database directly from Firebase, you should instead take it from an instance of the Firebase Driver. This allows the driver to be swapped out as one to use either the real Firebase or an in-memory, synchronous version with largely the same behaviour.

Whereas normally you might grab the Firebase Realtime Database directly:

import { database } from "firebase-admin"
const database = database()
// ...

To use ts-firebase-driver-testing you should instead get the Firebase Driver, and get the database instance from there:

import { firebaseDriver } from "./wherever/you/have/configured/the/driver"
const database = firebaseDriver.realTimeDatabase()

How you manage and configure the Firebase driver is up to you. You might want to use a dependency container pattern to manage dependencies between tests and real usage.

For example, you can configure the real Firebase driver in an index.ts file that will set it up before the rest of the application.

In tests, you can use a setup.ts file and ensure it's run before the tests via Jest:

jest --setupFilesAfterEnv='<rootDir>/__tests__/setup.ts'

You can also configure Jest to use the setup file in your project's package.json:

{
    "jest": {
        "setupFilesAfterEnv": ["<rootDir>/__tests__/setup.ts"]
    }
}

Either way, the important thing is that the driver is configured before the rest of the application. This package doesn't have an opinion on how you should do that; it just provides the in-process Firebase driver implementation for your tests to use.

Available driver components

So far the Firebase driver offers these components:

export interface IFirebaseDriver {
    realTimeDatabase(): IFirebaseRealtimeDatabase
    runWith(runtimeOptions: IRunTimeOptions): IFirebaseFunctionBuilder
    runOptions(memory?: MemoryOption, timeoutSeconds?: number): IRunTimeOptions
}

We'll be adding more components as we need them for our own testing.

Trigger functions

Firebase pubsub and Realtime Database trigger functions are handled within the Firebase driver with the same API as the real Firebase.

This means you can set up a database event trigger as normal, and it will be triggered during your tests in the same process, so that you can debug it and have fast-running, controllable tests.

FAQ

What is this for?

ts-firebase-driver-testing is intended to make it easier to write higher-level tests for business logic in Firebase applications.

For example, this kind of test case:

// Given a pub-sub message is sent to the foobar topic;

// Then an update should be made to foobar/some-id;

// And bar/foo/some-id should also be updated. 

There may be business logic in event trigger functions and pub-sub functions that needs to be tested as a whole. This is tricky to do with the Firebase Test SDK for Cloud Functions as you have to carefully stub out the right parts and manage the overall behaviour from the test.

ts-firebase-driver-testing lets you swap out the Firebase pub-sub and event trigger functions for the test, so that you can interact with an application entry point as normal, and then assert that the correct behaviour happened across functions.

Why would I want this?

If you want to benefit from high-level business logic tests that run quickly in a single process without interacting with the outside world, then you might find ts-firebase-driver-testing useful.

How is this different to the Firebase Function Test SDK?

The Firebase Test SDK for Cloud Functions also lets you run in-process tests for your Firebase functions, and provides a more complete and accurate representation of the Firebase API than ts-firebase-driver-testing.

The Firebase Test SDK requires carefully stubbing out the correct parts of the Firebase application under test, whereas ts-firebase-driver-testing allows swapping it all out and once and then checking that the application interacted with Firebase correctly.

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.8

2 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.3

3 years ago

0.28.11

3 years ago

0.28.10

3 years ago

0.28.8

3 years ago

0.28.7

3 years ago

0.28.6

3 years ago

0.28.5

3 years ago

0.28.4

3 years ago

0.28.3

3 years ago

0.28.2

3 years ago

0.28.1

3 years ago

0.28.0

3 years ago

0.27.1

4 years ago

0.27.0

4 years ago

0.26.2

4 years ago

0.26.0

4 years ago

0.25.8

4 years ago

0.25.7

4 years ago

0.25.6

4 years ago

0.24.5

4 years ago

0.24.4

4 years ago

0.24.3

4 years ago

0.24.2

4 years ago

0.24.1

4 years ago

0.24.0

4 years ago

0.23.68

4 years ago

0.23.67

4 years ago

0.23.66

4 years ago

0.23.65

4 years ago

0.23.64

4 years ago

0.23.63

4 years ago

0.23.61

4 years ago

0.23.59

4 years ago

0.23.58

4 years ago

0.23.57

4 years ago

0.23.56

4 years ago

0.23.55

4 years ago

0.23.54

4 years ago

0.23.53

4 years ago

0.23.52

4 years ago

0.23.51

4 years ago

0.23.50

4 years ago

0.23.49

4 years ago

0.23.48

4 years ago

0.23.47

4 years ago

0.23.46

4 years ago

0.23.45

4 years ago

0.23.44

4 years ago

0.23.43

4 years ago

0.23.42

4 years ago

0.23.41

4 years ago

0.23.40

4 years ago

0.23.39

4 years ago

0.23.38

4 years ago

0.23.37

4 years ago

0.23.36

4 years ago

0.23.35

4 years ago

0.23.34

4 years ago

0.23.33

4 years ago

0.23.31

4 years ago

0.23.30

4 years ago

0.23.32

4 years ago

0.23.28

4 years ago

0.23.29

4 years ago

0.23.27

4 years ago

0.23.26

4 years ago

0.23.25

4 years ago

0.23.24

4 years ago

0.23.23

4 years ago

0.23.22

4 years ago

0.23.20

4 years ago

0.23.21

4 years ago

0.23.19

4 years ago

0.23.18

4 years ago

0.23.17

4 years ago

0.23.16

4 years ago

0.23.15

4 years ago

0.23.14

4 years ago

0.23.13

4 years ago

0.23.12

4 years ago

0.23.9

4 years ago

0.23.11

4 years ago

0.23.10

4 years ago

0.23.8

4 years ago

0.23.7

4 years ago

0.23.6

4 years ago

0.23.5

4 years ago

0.23.4

4 years ago

0.23.3

4 years ago

0.23.2

4 years ago

0.23.1

4 years ago

0.23.0

4 years ago

0.22.2

4 years ago

0.22.1

4 years ago

0.21.1

4 years ago

0.21.0

4 years ago

0.20.6

4 years ago

0.22.0

4 years ago

0.20.5

4 years ago

0.20.1

4 years ago

0.20.0

4 years ago

0.20.4

4 years ago

0.20.3

4 years ago

0.20.2

4 years ago

0.19.9

4 years ago

0.19.8

4 years ago

0.19.6

4 years ago

0.19.7

4 years ago

0.19.5

4 years ago

0.19.4

4 years ago

0.19.3

4 years ago

0.19.2

4 years ago

0.19.1

4 years ago

0.19.0

4 years ago

0.18.2

4 years ago

0.18.3

4 years ago

0.18.4

4 years ago

0.18.1

4 years ago

0.17.1

4 years ago

0.17.0

4 years ago

0.16.1

4 years ago

0.16.0

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.3

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago