Skip to content

Commit 5efb4da

Browse files
committed
Update CLI version, remove stubbing
1 parent 7bc3ff1 commit 5efb4da

File tree

5 files changed

+81
-34
lines changed

5 files changed

+81
-34
lines changed

packages/rules-unit-testing/firebase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
{
2+
"functions": {
3+
"source": "."
4+
},
25
"emulators": {
36
"firestore": {
47
"port": 9003
58
},
69
"database": {
710
"port": 9002
811
},
12+
"functions": {
13+
"port": 9004
14+
},
915
"ui": {
1016
"enabled": false
1117
}

packages/rules-unit-testing/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"@google-cloud/firestore": "4.4.0",
2929
"@types/request": "2.48.5",
3030
"firebase-admin": "9.2.0",
31-
"firebase-tools": "8.12.1",
31+
"firebase-tools": "8.13.0",
32+
"firebase-functions": "3.11.0",
3233
"rollup": "2.29.0",
3334
"rollup-plugin-typescript2": "0.27.3"
3435
},

packages/rules-unit-testing/src/api/index.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export async function loadDatabaseRules(
323323
throw Error('must provide rules to loadDatabaseRules');
324324
}
325325

326-
const resp = await requestPromise({
326+
const resp = await requestPromise(request.put, {
327327
method: 'PUT',
328328
uri: `http://${getDatabaseHost()}/.settings/rules.json?ns=${
329329
options.databaseName
@@ -352,7 +352,7 @@ export async function loadFirestoreRules(
352352
throw new Error('must provide rules to loadFirestoreRules');
353353
}
354354

355-
const resp = await requestPromise({
355+
const resp = await requestPromise(request.put, {
356356
method: 'PUT',
357357
uri: `http://${getFirestoreHost()}/emulator/v1/projects/${
358358
options.projectId
@@ -379,7 +379,7 @@ export async function clearFirestoreData(
379379
throw new Error('projectId not specified');
380380
}
381381

382-
const resp = await requestPromise({
382+
const resp = await requestPromise(request.delete, {
383383
method: 'DELETE',
384384
uri: `http://${getFirestoreHost()}/emulator/v1/projects/${
385385
options.projectId
@@ -416,13 +416,13 @@ export async function withFunctionTriggersDisabled<TResult>(
416416
}
417417

418418
// Disable background triggers
419-
const disableRes = await requestPromise({
419+
const disableRes = await requestPromise(request.put, {
420420
method: 'PUT',
421421
uri: `http://${hubHost}/functions/disableBackgroundTriggers`
422422
});
423423
if (disableRes.statusCode !== 200) {
424424
throw new Error(
425-
`HTTP Error ${disableRes.statusCode} when disabling functions triggers, are you using the latest version of the Firebase CLI?`
425+
`HTTP Error ${disableRes.statusCode} when disabling functions triggers, are you using firebase-tools 8.13.0 or higher?`
426426
);
427427
}
428428

@@ -432,13 +432,13 @@ export async function withFunctionTriggersDisabled<TResult>(
432432
result = await fn();
433433
} finally {
434434
// Re-enable background triggers
435-
const enableRes = await requestPromise({
435+
const enableRes = await requestPromise(request.put, {
436436
method: 'PUT',
437437
uri: `http://${hubHost}/functions/enableBackgroundTriggers`
438438
});
439439
if (enableRes.statusCode !== 200) {
440440
throw new Error(
441-
`HTTP Error ${enableRes.statusCode} when enabling functions triggers, are you using the latest version of the Firebase CLI?`
441+
`HTTP Error ${enableRes.statusCode} when enabling functions triggers, are you using firebase-tools 8.13.0 or higher?`
442442
);
443443
}
444444
}
@@ -475,6 +475,7 @@ export function assertSucceeds(pr: Promise<any>): any {
475475
}
476476

477477
function requestPromise(
478+
method: typeof request.get,
478479
options: request.CoreOptions & request.UriOptions
479480
): Promise<{ statusCode: number; body: any }> {
480481
return new Promise((resolve, reject) => {
@@ -486,23 +487,8 @@ function requestPromise(
486487
}
487488
};
488489

489-
// Unfortunately request() is not stub-compatible, this is to make things testable
490-
switch (options.method) {
491-
case 'GET':
492-
request.get(options, callback);
493-
break;
494-
case 'PUT':
495-
request.put(options, callback);
496-
break;
497-
case 'POST':
498-
request.post(options, callback);
499-
break;
500-
case 'DELETE':
501-
request.delete(options, callback);
502-
break;
503-
default:
504-
request(options, callback);
505-
break;
506-
}
490+
// Unfortunately request's default method is not very test-friendly so having
491+
// the caler pass in the method here makes this whole thing compatible with sinon
492+
method(options, callback);
507493
});
508494
}

packages/rules-unit-testing/test/database.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,28 +331,24 @@ describe('Testing Module Tests', function () {
331331
});
332332

333333
it('disabling function triggers does not throw, returns value', async function () {
334-
const putStub = sandbox
335-
.stub(request, 'put')
336-
.yields(undefined, { statusCode: 200 }, {});
334+
const putSpy = sandbox.spy(request, 'put');
337335

338336
const res = await firebase.withFunctionTriggersDisabled(() => {
339337
return Promise.resolve(1234);
340338
});
341339

342340
expect(res).to.eq(1234);
343-
expect(putStub.callCount).to.equal(2);
341+
expect(putSpy.callCount).to.equal(2);
344342
});
345343

346344
it('disabling function triggers always re-enables, event when the function throws', async function () {
347-
const putStub = sandbox
348-
.stub(request, 'put')
349-
.yields(undefined, { statusCode: 200 }, {});
345+
const putSpy = sandbox.spy(request, 'put');
350346

351347
const res = firebase.withFunctionTriggersDisabled(() => {
352348
throw new Error('I throw!');
353349
});
354350

355351
await expect(res).to.eventually.be.rejectedWith('I throw!');
356-
expect(putStub.callCount).to.equal(2);
352+
expect(putSpy.callCount).to.equal(2);
357353
});
358354
});

yarn.lock

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7245,6 +7245,64 @@ [email protected]:
72457245
winston "^3.0.0"
72467246
ws "^7.2.3"
72477247

7248+
7249+
version "8.13.0"
7250+
resolved "https://registry.npmjs.org/firebase-tools/-/firebase-tools-8.13.0.tgz#324eb47d9c3987b85dfa6818aebd077df0fc431b"
7251+
integrity sha512-IlGJA5WVDTrjj02anUhuBwaCHe+WtB0gNbp9SjIRqIVYbMpJWPi25sqyiJ5kb4u7r7lZOcSGQbAYHqpDdzakfQ==
7252+
dependencies:
7253+
"@google-cloud/pubsub" "^1.7.0"
7254+
JSONStream "^1.2.1"
7255+
archiver "^3.0.0"
7256+
body-parser "^1.19.0"
7257+
chokidar "^3.0.2"
7258+
cjson "^0.3.1"
7259+
cli-color "^1.2.0"
7260+
cli-table "^0.3.1"
7261+
commander "^4.0.1"
7262+
configstore "^5.0.1"
7263+
cross-env "^5.1.3"
7264+
cross-spawn "^7.0.1"
7265+
csv-streamify "^3.0.4"
7266+
dotenv "^6.1.0"
7267+
exegesis-express "^2.0.0"
7268+
exit-code "^1.0.2"
7269+
express "^4.16.4"
7270+
filesize "^3.1.3"
7271+
fs-extra "^0.23.1"
7272+
glob "^7.1.2"
7273+
google-auth-library "^5.5.0"
7274+
google-gax "~1.12.0"
7275+
inquirer "~6.3.1"
7276+
js-yaml "^3.13.1"
7277+
jsonschema "^1.0.2"
7278+
jsonwebtoken "^8.2.1"
7279+
leven "^3.1.0"
7280+
lodash "^4.17.19"
7281+
marked "^0.7.0"
7282+
marked-terminal "^3.3.0"
7283+
minimatch "^3.0.4"
7284+
morgan "^1.10.0"
7285+
open "^6.3.0"
7286+
ora "^3.4.0"
7287+
plist "^3.0.1"
7288+
portfinder "^1.0.23"
7289+
progress "^2.0.3"
7290+
request "^2.87.0"
7291+
rimraf "^3.0.0"
7292+
semver "^5.7.1"
7293+
superstatic "^7.0.0"
7294+
tar "^4.3.0"
7295+
tcp-port-used "^1.0.1"
7296+
tmp "0.0.33"
7297+
triple-beam "^1.3.0"
7298+
tweetsodium "0.0.5"
7299+
universal-analytics "^0.4.16"
7300+
unzipper "^0.10.10"
7301+
update-notifier "^4.1.0"
7302+
uuid "^3.0.0"
7303+
winston "^3.0.0"
7304+
ws "^7.2.3"
7305+
72487306
flagged-respawn@^1.0.0:
72497307
version "1.0.1"
72507308
resolved "https://registry.npmjs.org/flagged-respawn/-/flagged-respawn-1.0.1.tgz#e7de6f1279ddd9ca9aac8a5971d618606b3aab41"

0 commit comments

Comments
 (0)