Skip to content

Add integration test for firebase-functions binary #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- 10.x
- 12.x
- 14.x
- 16.x
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
Expand All @@ -31,4 +32,26 @@ jobs:
- run: npm install
- run: npm run lint
- run: npm run format
- run: npm run build && npm run test
- run: npm run test
integration:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 12.x
- 14.x
- 16.x
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Cache npm
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}

- run: npm install
- run: npm run test:bin
4 changes: 1 addition & 3 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
exit: true
extension:
- ts
file:
- mocha/setup.ts
package: ./package.json
reporter: spec
require:
- 'ts-node/register'
spec: spec/**/*.spec.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this just means that we now have test files outside the spec/ dir and they don't need spec in the title, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - having the spec defined here made it impossible for me to not run them for integration tests. So we now pass in the path/glob of test we want to run in the npm script definition (in package.json) itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh the changed line - "test": "mocha --file ./mocha/setup.ts spec/**/*.spec.ts ",. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... there's no other way to achieve our goal? Changing mocha to need a --file means that running an individual file's tests with npx mocha got harder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm don't you still need to pass individual file via npx mocha, e.g. npx mocha spec/path/to/my/test.ts?

I'm not sure what devx I just broke here. I don't know if other ways exists, but happy to explore.

- 'source-map-support/register'
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
"format:fix": "prettier --write '**/*.{json,md,ts,yml,yaml}'",
"lint": "tslint --config tslint.json --project tsconfig.json ",
"lint:fix": "tslint --config tslint.json --fix --project tsconfig.json",
"test": "mocha"
"test": "mocha --file ./mocha/setup.ts spec/**/*.spec.ts ",
"test:bin": "./scripts/bin-test/run.sh"
},
"dependencies": {
"@types/cors": "^2.8.5",
Expand Down Expand Up @@ -180,10 +181,12 @@
"mock-require": "^3.0.3",
"mz": "^2.7.0",
"nock": "^10.0.6",
"node-fetch": "^2.6.7",
"portfinder": "^1.0.28",
"prettier": "^1.18.2",
"semver": "^7.3.5",
"sinon": "^7.3.2",
"ts-node": "^8.3.0",
"ts-node": "^10.4.0",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0",
"tslint-no-unused-expression-chai": "^0.1.4",
Expand Down
4 changes: 4 additions & 0 deletions scripts/bin-test/mocha-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);
20 changes: 20 additions & 0 deletions scripts/bin-test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -ex # Immediately exit on failure

# Link the Functions SDK for the testing environment.
npm run build
npm link

# Link local SDK to all test sources.
for f in scripts/bin-test/sources/*; do
if [ -d "$f" ]; then
(cd "$f" && npm link firebase-functions)
fi
done

## DEBUG
ls -la scripts/bin-test/sources/commonjs/node_modules

mocha \
--file ./scripts/bin-test/mocha-setup.ts \
./scripts/bin-test/test.ts
9 changes: 9 additions & 0 deletions scripts/bin-test/sources/commonjs-grouped/g1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const functions = require("firebase-functions");

exports.groupedhttp = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.groupedcallable = functions.https.onCall(() => {
return "PASS";
});
20 changes: 20 additions & 0 deletions scripts/bin-test/sources/commonjs-grouped/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const functions = require("firebase-functions");
const functionsv2 = require("firebase-functions/v2");

exports.v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v1callable = functions.https.onCall(() => {
return "PASS";
});

exports.v2http = functionsv2.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v2callable = functionsv2.https.onCall(() => {
return "PASS";
});

exports.g1 = require("./g1");
3 changes: 3 additions & 0 deletions scripts/bin-test/sources/commonjs-grouped/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "commonjs-grouped"
}
18 changes: 18 additions & 0 deletions scripts/bin-test/sources/commonjs-main/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const functions = require("firebase-functions");
const functionsv2 = require("firebase-functions/v2");

exports.v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v1callable = functions.https.onCall(() => {
return "PASS";
});

exports.v2http = functionsv2.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v2callable = functionsv2.https.onCall(() => {
return "PASS";
});
4 changes: 4 additions & 0 deletions scripts/bin-test/sources/commonjs-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "commonjs-main",
"main": "functions.js"
}
18 changes: 18 additions & 0 deletions scripts/bin-test/sources/commonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const functions = require("firebase-functions");
const functionsv2 = require("firebase-functions/v2");

exports.v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v1callable = functions.https.onCall(() => {
return "PASS";
});

exports.v2http = functionsv2.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v2callable = functionsv2.https.onCall(() => {
return "PASS";
});
3 changes: 3 additions & 0 deletions scripts/bin-test/sources/commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as functions from '../../../../lib/index.js';
import * as functionsv2 from "../../../../lib/v2/index.js";
import * as functions from "firebase-functions";
import * as functionsv2 from "firebase-functions/v2";

export const v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as functions from "../../../../lib/index.js";
import * as functionsv2 from "../../../../lib/v2/index.js";
import * as functions from "firebase-functions";
import * as functionsv2 from "firebase-functions/v2";

export const v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as functions from "../../../../lib/index.js";
import * as functionsv2 from "../../../../lib/v2/index.js";
import * as functions from "firebase-functions";
import * as functionsv2 from "firebase-functions/v2";

export const v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
Expand Down
Loading