{"id":15650003,"url":"https://github.com/cartant/firebase-nightlight","last_synced_at":"2025-04-30T13:52:00.051Z","repository":{"id":20318738,"uuid":"89200610","full_name":"cartant/firebase-nightlight","owner":"cartant","description":"An in-memory, JavaScript mock for the Firebase Web API","archived":false,"fork":false,"pushed_at":"2022-12-30T17:23:37.000Z","size":1125,"stargazers_count":37,"open_issues_count":3,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T06:04:49.940Z","etag":null,"topics":["firebase","mock","testing"],"latest_commit_sha":null,"homepage":"https://cartant.github.io/firebase-nightlight/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cartant.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["cartant"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-04-24T05:20:31.000Z","updated_at":"2023-12-19T17:40:19.000Z","dependencies_parsed_at":"2023-01-13T20:54:28.515Z","dependency_job_id":null,"html_url":"https://github.com/cartant/firebase-nightlight","commit_stats":null,"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cartant%2Ffirebase-nightlight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cartant%2Ffirebase-nightlight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cartant%2Ffirebase-nightlight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cartant%2Ffirebase-nightlight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cartant","download_url":"https://codeload.github.com/cartant/firebase-nightlight/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251714939,"owners_count":21631806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["firebase","mock","testing"],"created_at":"2024-10-03T12:32:55.410Z","updated_at":"2025-04-30T13:52:00.006Z","avatar_url":"https://github.com/cartant.png","language":"TypeScript","funding_links":["https://github.com/sponsors/cartant"],"categories":[],"sub_categories":[],"readme":"# firebase-nightlight\n\n[![GitHub License](https://img.shields.io/badge/license-GPL%20v3-blue.svg)](https://github.com/cartant/firebase-nightlight/blob/master/LICENSE)\n[![NPM version](https://img.shields.io/npm/v/firebase-nightlight.svg)](https://www.npmjs.com/package/firebase-nightlight)\n[![Build status](https://img.shields.io/travis/cartant/firebase-nightlight.svg)](http://travis-ci.org/cartant/firebase-nightlight)\n[![dependency status](https://img.shields.io/david/cartant/firebase-nightlight.svg)](https://david-dm.org/cartant/firebase-nightlight)\n[![devDependency Status](https://img.shields.io/david/dev/cartant/firebase-nightlight.svg)](https://david-dm.org/cartant/firebase-nightlight#info=devDependencies)\n[![peerDependency Status](https://img.shields.io/david/peer/cartant/firebase-nightlight.svg)](https://david-dm.org/cartant/firebase-nightlight#info=peerDependencies)\n\n### What is it?\n\n`firebase-nightlight` is an in-memory, JavaScript mock for the Firebase Web API.\n\n### Why might you need it?\n\nUnit testing services or components that use the Firebase Web API can be tedious:\n\n* stubbing multiple API methods for each test involves a writing a lot of code, and\n* the alternative of running tests against an actual Firebase project is slow.\n\nYou might find using an in-memory mock that can be created and destroyed on a per-test or per-suite basis to be less frustrating.\n\n### How does it work?\n\nEach `Mock` instance implements mocked versions of the properties and methods that are in the [`firebase`](https://firebase.google.com/docs/reference/js/firebase) namespace. The options passed when creating a `Mock` instance allow for the specification of the initial database content and authentication identities.\n\n### What is mocked?\n\n* Most of the `database` API is mocked:\n    * References can be used to read, write and query data.\n    * Events are mocked and will be emitted between references.\n    * Security rules are not mocked.\n    * Priorities are not mocked.\n    * `onDisconnect` is not mocked.\n    * The sometimes-synchronous nature of `child_added` events is not mimicked; mocked events are *always* asynchronous.\n* Some of the `auth` API is mocked:\n    * `createUserWithEmailAndPassword`,\n    * `onAuthStateChanged`,\n    * `signInAnonymously`,\n    * `signInWithCredential`,\n    * `signInWithCustomToken`,\n    * `signInWithEmailAndPassword`, and\n    * `signOut` are mocked.\n    * Other methods are not mocked.\n* The `firestore`, `messaging` and `storage` APIs are not mocked.\n\n## Example\n\n```ts\nimport * as firebase from \"firebase/app\";\nimport { expect } from \"chai\";\nimport { Mock } from \"firebase-nightlight\";\n\ndescribe(\"something\", () =\u003e {\n\n    let mockDatabase: any;\n    let mockApp: firebase.app.App;\n\n    beforeEach(() =\u003e {\n\n        mockDatabase = {\n            content: {\n                lorem: \"ipsum\"\n            }\n        };\n        const mock = new Mock({\n            database: mockDatabase,\n            identities: [{\n                email: \"alice@firebase.com\",\n                password: \"wonderland\"\n            }]\n        });\n        mockApp = mock.initializeApp({});\n    });\n\n    it(\"should do something with the mock\", () =\u003e {\n\n        return mockApp\n            .auth()\n            .signInWithEmailAndPassword(\"alice@firebase.com\", \"wonderland\")\n            .then((user) =\u003e {\n\n                expect(user).to.exist;\n                expect(user).to.have.property(\"email\", \"alice@firebase.com\");\n                expect(user).to.have.property(\"uid\");\n\n                return mockApp\n                    .database()\n                    .ref()\n                    .once(\"value\");\n            })\n            .then((snapshot) =\u003e {\n\n                expect(snapshot.val()).to.deep.equal({ lorem: \"ipsum\" });\n\n                return mockApp\n                    .database()\n                    .ref()\n                    .update({ lorem: \"something else\" });\n            })\n            .then(() =\u003e {\n\n                expect(mockDatabase.content).to.deep.equal({ lorem: \"something else\" });\n\n                return mockApp\n                    .auth()\n                    .signOut();\n            });\n    });\n});\n```\n\n## Install\n\nInstall the package using NPM:\n\n```\nnpm install firebase-nightlight --save-dev\n```\n\nAnd import the `Mock` class for use with TypeScript or ES2015:\n\n```js\nimport { Mock } from \"firebase-nightlight\";\nconst mock = new Mock();\nconsole.log(mock);\n```\n\nOr `require` the module for use with Node or a CommonJS bundler:\n\n```js\nconst firebaseNightlight = require(\"firebase-nightlight\");\nconst mock = new firebaseNightlight.Mock();\nconsole.log(mock);\n```\n\nOr include the UMD bundle for use as a `script`:\n\n```html\n\u003cscript src=\"https://unpkg.com/firebase-nightlight\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nvar mock = new firebaseNightlight.Mock();\nconsole.log(mock);\n\u003c/script\u003e\n```\n\n## API\n\nInstances of the `Mock` class implement the properties and methods that are in the Firebase Web API's [`firebase`](https://firebase.google.com/docs/reference/js/firebase) namespace.\n\nThe `Mock` constructor accepts an `options` object with the following optional properties:\n\n| Property | Description |\n| --- | --- |\n| `database` | An object containing the initial database `content`. |\n| `identities` | An array of identities to use use when authenticating users. |\n| `apps` | An object containing `database` and `identities` configurations by app name. |\n\nIf `identities` are specified, they can have the following optional properties:\n\n| Property | Description |\n| --- | --- |\n| `credential` | The `firebase.auth.AuthCredential` to match if `signInWithCredential` is called. |\n| `email` | The user's email. |\n| `password` | The password to match if `signInWithEmailAndPassword` is called. |\n| `token` | The token to match if `signInWithCustomToken` is called. |\n| `uid` | The user's UID. If not specified, a random UID is generated. |\n\n### Additions to the Firebase Web API\n\nThe mock's implementation of `firebase.database.Reference` includes a `stats_` function that will return the current listener counts for each event type. For example:\n\n```js\nmockRef.on(\"child_added\", () =\u003e {});\nmockRef.on(\"child_removed\", () =\u003e {});\n\nconst stats = mockRef.stats_();\nexpect(stats.listeners).to.deep.equal({\n    child_added: 1,\n    child_changed: 0,\n    child_moved: 0,\n    child_removed: 1,\n    total: 2,\n    value: 0\n});\n```\n\n### Forcing database errors\n\nIt's possible to force database errors by delcaring errors in the database content. For example, with this content:\n\n```js\nconst mockDatabase = {\n    content: {\n        a: {\n            b: {\n                \".error\": {\n                    code: \"database/boom\",\n                    message: \"Boom!\"\n                },\n                c: {\n                    value: 3\n                }\n            }\n        }\n    }\n};\nconst mock = new Mock({\n    database: mockDatabase\n});\n```\n\nAll reads and writes on the `a/b` path will fail with the specified error. Any reads or writes on deeper paths - `a/b/c`, for example - will also fail with the specified error.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcartant%2Ffirebase-nightlight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcartant%2Ffirebase-nightlight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcartant%2Ffirebase-nightlight/lists"}