Skip to content

Commit 7efda84

Browse files
committed
Add tests
1 parent cd84a1c commit 7efda84

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { expect } from 'chai';
18+
import {
19+
FunctionsErrorCode,
20+
httpsCallable as httpsCallableExp,
21+
useFunctionsEmulator as useFunctionsEmulatorExp
22+
} from '@firebase/functions-exp';
23+
import { createTestService } from '../test/utils';
24+
import { firebase, FirebaseApp } from '@firebase/app-compat';
25+
26+
// eslint-disable-next-line @typescript-eslint/no-require-imports
27+
export const TEST_PROJECT = require('../../../config/project.json');
28+
29+
// Chai doesn't handle Error comparisons in a useful way.
30+
// https://github.com/chaijs/chai/issues/608
31+
async function expectError(
32+
promise: Promise<any>,
33+
code: FunctionsErrorCode,
34+
message: string,
35+
details?: any
36+
): Promise<void> {
37+
let failed = false;
38+
try {
39+
await promise;
40+
} catch (e) {
41+
failed = true;
42+
// Errors coming from callable functions usually have the functions-exp
43+
// code in the message since it's thrown inside functions-exp.
44+
expect(e.code).to.match(new RegExp(`functions.*/${code}`));
45+
expect(e.message).to.equal(message);
46+
expect(e.details).to.deep.equal(details);
47+
}
48+
if (!failed) {
49+
expect(false, 'Promise should have failed.').to.be.true;
50+
}
51+
}
52+
53+
describe('Firebase Functions Interop - exp functions work if given compat instance', () => {
54+
let app: FirebaseApp;
55+
const region = 'us-central1';
56+
57+
before(() => {
58+
const useEmulator = !!process.env.HOST;
59+
const projectId = useEmulator
60+
? 'functions-integration-test'
61+
: TEST_PROJECT.projectId;
62+
const messagingSenderId = 'messaging-sender-id';
63+
64+
app = firebase.initializeApp({ projectId, messagingSenderId });
65+
});
66+
67+
after(async () => {
68+
await app.delete();
69+
});
70+
71+
it('httpsCallable: simple data', async () => {
72+
const functions = createTestService(app, region);
73+
// TODO(klimt): Should we add an API to create a "long" in JS?
74+
const data = {
75+
bool: true,
76+
int: 2,
77+
str: 'four',
78+
array: [5, 6],
79+
null: null
80+
};
81+
82+
const func = httpsCallableExp(functions, 'dataTest');
83+
const result = await func(data);
84+
85+
expect(result.data).to.deep.equal({
86+
message: 'stub response',
87+
code: 42,
88+
long: 420
89+
});
90+
});
91+
92+
it('httpsCallable: scalars', async () => {
93+
const functions = createTestService(app, region);
94+
const func = httpsCallableExp(functions, 'scalarTest');
95+
const result = await func(17);
96+
expect(result.data).to.equal(76);
97+
});
98+
99+
it('httpsCallable: null', async () => {
100+
const functions = createTestService(app, region);
101+
const func = httpsCallableExp(functions, 'nullTest');
102+
let result = await func(null);
103+
expect(result.data).to.be.null;
104+
105+
// Test with void arguments version.
106+
result = await func();
107+
expect(result.data).to.be.null;
108+
});
109+
110+
it('httpsCallable: missing result', async () => {
111+
const functions = createTestService(app, region);
112+
const func = httpsCallableExp(functions, 'missingResultTest');
113+
await expectError(func(), 'internal', 'Response is missing data field.');
114+
});
115+
116+
it('httpsCallable: unhandled error', async () => {
117+
const functions = createTestService(app, region);
118+
const func = httpsCallableExp(functions, 'unhandledErrorTest');
119+
await expectError(func(), 'internal', 'internal');
120+
});
121+
122+
it('httpsCallable: unknown error', async () => {
123+
const functions = createTestService(app, region);
124+
const func = httpsCallableExp(functions, 'unknownErrorTest');
125+
await expectError(func(), 'internal', 'internal');
126+
});
127+
128+
it('httpsCallable: explicit error', async () => {
129+
const functions = createTestService(app, region);
130+
const func = httpsCallableExp(functions, 'explicitErrorTest');
131+
await expectError(func(), 'out-of-range', 'explicit nope', {
132+
start: 10,
133+
end: 20,
134+
long: 30
135+
});
136+
});
137+
138+
it('httpsCallable: http error', async () => {
139+
const functions = createTestService(app, region);
140+
const func = httpsCallableExp(functions, 'httpErrorTest');
141+
await expectError(func(), 'invalid-argument', 'invalid-argument');
142+
});
143+
144+
it('httpsCallable: timeout', async () => {
145+
const functions = createTestService(app, region);
146+
const func = httpsCallableExp(functions, 'timeoutTest', { timeout: 10 });
147+
await expectError(func(), 'deadline-exceeded', 'deadline-exceeded');
148+
});
149+
150+
it('useFunctionsEmulator', async () => {
151+
const functions = createTestService(app, region);
152+
useFunctionsEmulatorExp(functions, 'myhost', 4000);
153+
expect((functions as any)._delegate.emulatorOrigin).to.equal(
154+
'http://myhost:4000'
155+
);
156+
});
157+
});

0 commit comments

Comments
 (0)