Skip to content

Commit

Permalink
Add emulator-based integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchenshi committed Feb 4, 2021
1 parent 6ce98e2 commit c653f95
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install, build and test
- name: Install and build
run: |
npm ci
npm run build
npm run build:tests
- name: Lint and run unit tests
run: |
npm test
npm run api-extractor
- name: Run emulator-based integration tests
run: |
npm install -g firebase-tools
npm integration:emulator
22 changes: 21 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ There are two test suites: unit and integration. The unit test suite is intended
development, and the integration test suite is intended to be run before packaging up release
candidates.

#### Unit Tests

To run the unit test suite:

```bash
Expand All @@ -135,7 +137,25 @@ If you wish to skip the linter, and only run the unit tests:
$ npm run test:unit
```

The integration tests run against an actual Firebase project. Create a new
#### Integration Tests with Emulator Suite

Some of the integration tests work with the Emulator Suite and you can run them
without an actual Firebase project.

First, make sure to [install Firebase CLI](https://firebase.google.com/docs/cli#install_the_firebase_cli).
And then:

```bash
npm integration:emulator
```

Currently, only the Auth, Database, and Firestore test suites work. Some test
cases will be automatically skipped due to lack of emulator support. The section
below covers how to run the full test suite against an actual Firebase project.

#### Integration Tests with an actual Firebase project

Other integration tests require an actual Firebase project. Create a new
project in the [Firebase Console](https://console.firebase.google.com), if you
do not already have one suitable for running the tests against. Then obtain the
following credentials from the project:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"lint": "run-p lint:src lint:test",
"test": "run-s lint test:unit",
"integration": "run-s build test:integration",
"integration:emulator": "run-s build test:integration:emulator",
"test:unit": "mocha test/unit/*.spec.ts --require ts-node/register",
"test:integration": "mocha test/integration/*.ts --slow 5000 --timeout 20000 --require ts-node/register",
"test:integration:emulator": "firebase emulators:exec --project fake-project-id --only auth,database,firestore 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register'",
"test:coverage": "nyc npm run test:unit",
"lint:src": "eslint src/ --ext .ts",
"lint:test": "eslint test/ --ext .ts",
Expand Down
22 changes: 18 additions & 4 deletions test/integration/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as admin from '../../lib/index';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { defaultApp, nullApp, nonNullApp, cmdArgs, databaseUrl } from './setup';
import { defaultApp, nullApp, nonNullApp, cmdArgs, databaseUrl, isEmulator } from './setup';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const chalk = require('chalk');
Expand Down Expand Up @@ -64,7 +64,13 @@ describe('admin.database', () => {
.should.eventually.be.fulfilled;
});

it('App with null auth overrides is blocked by security rules', () => {
it('App with null auth overrides is blocked by security rules', function () {
if (isEmulator) {
// RTDB emulator has open security rules by default and won't block this.
// TODO(https://github.com/firebase/firebase-admin-node/issues/1149):
// remove this once updating security rules through admin is in place.
return this.skip();
}
return nullApp.database().ref('blocked').set(admin.database.ServerValue.TIMESTAMP)
.should.eventually.be.rejectedWith('PERMISSION_DENIED: Permission denied');
});
Expand Down Expand Up @@ -157,13 +163,21 @@ describe('admin.database', () => {
});
});

it('admin.database().getRules() returns currently defined rules as a string', () => {
it('admin.database().getRules() returns currently defined rules as a string', function () {
if (isEmulator) {
// https://github.com/firebase/firebase-admin-node/issues/1149
return this.skip();
}
return admin.database().getRules().then((result) => {
return expect(result).to.be.not.empty;
});
});

it('admin.database().getRulesJSON() returns currently defined rules as an object', () => {
it('admin.database().getRulesJSON() returns currently defined rules as an object', function () {
if (isEmulator) {
// https://github.com/firebase/firebase-admin-node/issues/1149
return this.skip();
}
return admin.database().getRulesJSON().then((result) => {
return expect(result).to.be.not.undefined;
});
Expand Down

0 comments on commit c653f95

Please sign in to comment.