Skip to content

Commit 3095ad5

Browse files
committed
test(e2e): use functions emulator in testing
1 parent c2f5b82 commit 3095ad5

File tree

12 files changed

+255
-2
lines changed

12 files changed

+255
-2
lines changed

.github/workflows/scripts/firebase.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"rules": "firestore.rules",
44
"indexes": "firestore.indexes.json"
55
},
6+
"functions": {
7+
"predeploy": [
8+
"yarn",
9+
"yarn --prefix \"$RESOURCE_DIR\" build"
10+
],
11+
"source": "functions"
12+
},
613
"database": {
714
"rules": "database.rules"
815
},
@@ -19,6 +26,9 @@
1926
"firestore": {
2027
"port": "8080"
2128
},
29+
"functions": {
30+
"port": "5001"
31+
},
2232
"storage": {
2333
"port": "9199"
2434
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Compiled JavaScript files
2+
lib/**/*.js
3+
lib/**/*.js.map
4+
5+
# TypeScript v1 declaration files
6+
typings/
7+
8+
# Node.js dependency directory
9+
node_modules/
10+
yarn.lock
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "functions",
3+
"scripts": {
4+
"build": "tsc",
5+
"serve": "npm run build && firebase emulators:start --only functions",
6+
"shell": "npm run build && firebase functions:shell",
7+
"start": "npm run shell",
8+
"deploy": "firebase deploy --only functions",
9+
"logs": "firebase functions:log"
10+
},
11+
"engines": {
12+
"node": "14"
13+
},
14+
"main": "lib/index.js",
15+
"dependencies": {
16+
"firebase-admin": "^9.8.0",
17+
"firebase-functions": "^3.14.1"
18+
},
19+
"devDependencies": {
20+
"typescript": "^3.8.0",
21+
"firebase-functions-test": "^0.2.0"
22+
},
23+
"private": true
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
*
3+
* Testing tools for invertase/react-native-firebase use only.
4+
*
5+
* Copyright (C) 2018-present Invertase Limited <[email protected]>
6+
*
7+
* See License file for more information.
8+
*/
9+
10+
/* eslint-disable global-require */
11+
module.exports = {
12+
SAMPLE_DATA: require('./functions/sample-data'),
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as functions from 'firebase-functions';
2+
3+
// // Start writing Firebase Functions
4+
// // https://firebase.google.com/docs/functions/typescript
5+
//
6+
export const helloWorld = functions.https.onRequest((request, response) => {
7+
functions.logger.info('Hello logs!', { structuredData: true });
8+
response.send('Hello from Firebase!');
9+
});
10+
11+
export { testFunctionCustomRegion } from './testFunctionCustomRegion';
12+
export { testFunctionDefaultRegion } from './testFunctionDefaultRegion';
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Testing tools for invertase/react-native-firebase use only.
3+
*
4+
* Copyright (C) 2018-present Invertase Limited <[email protected]>
5+
*
6+
* See License file for more information.
7+
*/
8+
9+
const SAMPLE_DATA: { [key: string]: any } = {
10+
number: 1234,
11+
string: 'acde',
12+
boolean: true,
13+
null: null,
14+
object: {
15+
number: 1234,
16+
string: 'acde',
17+
boolean: true,
18+
null: null,
19+
},
20+
array: [1234, 'acde', true, null],
21+
deepObject: {
22+
array: [1234, 'acde', false, null],
23+
object: {
24+
number: 1234,
25+
string: 'acde',
26+
boolean: true,
27+
null: null,
28+
array: [1234, 'acde', true, null],
29+
},
30+
number: 1234,
31+
string: 'acde',
32+
boolean: true,
33+
null: null,
34+
},
35+
deepArray: [
36+
1234,
37+
'acde',
38+
true,
39+
null,
40+
[1234, 'acde', true, null],
41+
{
42+
number: 1234,
43+
string: 'acde',
44+
boolean: true,
45+
null: null,
46+
array: [1234, 'acde', true, null],
47+
},
48+
],
49+
deepMap: {
50+
number: 123,
51+
string: 'foo',
52+
booleanTrue: true,
53+
booleanFalse: false,
54+
null: null,
55+
list: ['1', 2, true, false],
56+
map: {
57+
number: 123,
58+
string: 'foo',
59+
booleanTrue: true,
60+
booleanFalse: false,
61+
null: null,
62+
},
63+
},
64+
deepList: [
65+
'1',
66+
2,
67+
true,
68+
false,
69+
['1', 2, true, false],
70+
{
71+
number: 123,
72+
string: 'foo',
73+
booleanTrue: true,
74+
booleanFalse: false,
75+
null: null,
76+
},
77+
],
78+
};
79+
80+
export default SAMPLE_DATA;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
*
3+
* Testing tools for invertase/react-native-firebase use only.
4+
*
5+
* Copyright (C) 2018-present Invertase Limited <[email protected]>
6+
*
7+
* See License file for more information.
8+
*/
9+
10+
import * as functions from 'firebase-functions';
11+
12+
export const testFunctionCustomRegion = functions
13+
.region('europe-west1')
14+
.https.onCall(() => 'europe-west1');
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
*
3+
* Testing tools for invertase/react-native-firebase use only.
4+
*
5+
* Copyright (C) 2018-present Invertase Limited <[email protected]>
6+
*
7+
* See License file for more information.
8+
*/
9+
10+
import * as assert from 'assert';
11+
import { FirebaseError } from 'firebase-admin';
12+
import * as functions from 'firebase-functions';
13+
import SAMPLE_DATA from './sample-data';
14+
15+
export const testFunctionDefaultRegion = functions.https.onCall(data => {
16+
console.log(Date.now(), data);
17+
18+
if (typeof data === 'undefined') {
19+
return 'undefined';
20+
}
21+
22+
if (typeof data === 'string') {
23+
return 'string';
24+
}
25+
26+
if (typeof data === 'number') {
27+
return 'number';
28+
}
29+
30+
if (typeof data === 'boolean') {
31+
return 'boolean';
32+
}
33+
34+
if (data === null) {
35+
return 'null';
36+
}
37+
38+
if (Array.isArray(data)) {
39+
return 'array';
40+
}
41+
42+
const { type, asError, inputData } = data;
43+
if (!Object.hasOwnProperty.call(SAMPLE_DATA, type)) {
44+
throw new functions.https.HttpsError('invalid-argument', 'Invalid test requested.');
45+
}
46+
47+
const outputData = SAMPLE_DATA[type];
48+
49+
try {
50+
assert.deepEqual(outputData, inputData);
51+
} catch (e) {
52+
console.error(e);
53+
throw new functions.https.HttpsError(
54+
'invalid-argument',
55+
'Input and Output types did not match.',
56+
(e as FirebaseError).message,
57+
);
58+
}
59+
60+
// all good
61+
if (asError) {
62+
throw new functions.https.HttpsError(
63+
'cancelled',
64+
'Response data was requested to be sent as part of an Error payload, so here we are!',
65+
outputData,
66+
);
67+
}
68+
69+
return outputData;
70+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitReturns": true,
5+
"noUnusedLocals": true,
6+
"outDir": "lib",
7+
"sourceMap": true,
8+
"skipLibCheck": true,
9+
"strict": true,
10+
"target": "es2017"
11+
},
12+
"compileOnSave": true,
13+
"include": [
14+
"src"
15+
]
16+
}

.github/workflows/scripts/start-firebase-emulator.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ if ! [ -x "$(command -v firebase)" ]; then
44
exit 1
55
fi
66

7-
EMU_START_COMMAND="firebase emulators:start --only auth,database,firestore,storage --project react-native-firebase-testing"
7+
EMU_START_COMMAND="firebase emulators:start --only auth,database,firestore,functions,storage --project react-native-firebase-testing"
88
#EMU_START_COMMAND="sleep 120"
99
MAX_RETRIES=3
1010
MAX_CHECKATTEMPTS=60
1111
CHECKATTEMPTS_WAIT=1
1212

13+
# Make sure functions are ready to go
14+
pushd "$(dirname "$0")/functions" && yarn && yarn build && popd
15+
1316

1417
RETRIES=1
1518
while [ $RETRIES -le $MAX_RETRIES ]; do

0 commit comments

Comments
 (0)