Skip to content

Add prettier to repo #55

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 2 commits into from
Jan 6, 2020
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
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"arrowParens": "always",
"singleQuote": true,
"trailingComma": "es5",
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '8'
- '10'
- stable
- '8'
- '10'
- stable
sudo: false
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"lint": "node_modules/.bin/tslint src/{**/*,*}.ts spec/{**/*,*}.ts",
"pretest": "node_modules/.bin/tsc",
"test": "mocha .tmp/spec/index.spec.js",
"posttest": "npm run lint && rm -rf .tmp"
"posttest": "npm run lint && rm -rf .tmp",
"format": "prettier --check '**/*.{json,ts,yml,yaml}'",
"format:fix": "prettier --write '**/*.{json,ts,yml,yaml}'"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +42,7 @@
"firebase-admin": "~8.9.0",
"firebase-functions": "^3.3.0",
"mocha": "^6.2.2",
"prettier": "^1.19.1",
"sinon": "^7.5.0",
"tslint": "^5.20.0",
"typescript": "^3.6.4"
Expand Down
13 changes: 10 additions & 3 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import { expect } from 'chai';

describe('index', () => {
/* tslint:disable-next-line:no-var-requires */
const indexExport = require('../src')({projectId: 'fakeProject'}, 'fakeServiceAccount');
const indexExport = require('../src')(
{ projectId: 'fakeProject' },
'fakeServiceAccount'
);
after(() => {
// Call cleanup (handles case of cleanup function not existing)
indexExport.cleanup && indexExport.cleanup();
Expand All @@ -48,8 +51,12 @@ describe('index', () => {
});

it('should set env variables based parameters SDK was initialized with', () => {
expect(process.env.FIREBASE_CONFIG).to.equal(JSON.stringify({projectId: 'fakeProject'}));
expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal('fakeServiceAccount');
expect(process.env.FIREBASE_CONFIG).to.equal(
JSON.stringify({ projectId: 'fakeProject' })
);
expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal(
'fakeServiceAccount'
);
});

it('should clean up env variables once cleanup is called', () => {
Expand Down
22 changes: 14 additions & 8 deletions spec/lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ describe('lifecycle', () => {

it('sets env variables appropriately if SDK initialized without parameters', () => {
test.init();
expect(process.env.FIREBASE_CONFIG).to.equal(JSON.stringify({
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
projectId: 'not-a-project',
}));
expect(process.env.FIREBASE_CONFIG).to.equal(
JSON.stringify({
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
projectId: 'not-a-project',
})
);
expect(process.env.GCLOUD_PROJECT).to.equal('not-a-project');
expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.be.undefined;
});
Expand All @@ -57,9 +59,13 @@ describe('lifecycle', () => {
};
test.init(firebaseConfig, 'path/to/key.json');

expect(process.env.FIREBASE_CONFIG).to.equal(JSON.stringify(firebaseConfig));
expect(process.env.FIREBASE_CONFIG).to.equal(
JSON.stringify(firebaseConfig)
);
expect(process.env.GCLOUD_PROJECT).to.equal('my-project');
expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal('path/to/key.json');
expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal(
'path/to/key.json'
);
});
});

Expand All @@ -81,7 +87,7 @@ describe('lifecycle', () => {
it('deletes all the env variables if they did not previously exist', () => {
let test = new FirebaseFunctionsTest();
test.init();
mockConfig({ foo: {bar: 'faz '}});
mockConfig({ foo: { bar: 'faz ' } });
test.cleanup();
expect(process.env.FIREBASE_CONFIG).to.be.undefined;
expect(process.env.GCLOUD_PROJECT).to.be.undefined;
Expand Down
22 changes: 14 additions & 8 deletions spec/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ import { mockConfig, makeChange, _makeResourceName, wrap } from '../src/main';

describe('main', () => {
describe('#wrap', () => {

const constructCF = (eventType?: string) => {
const cloudFunction = (input) => input;
set(cloudFunction, 'run', (data, context) => {
return { data, context };
});
return { data, context };
});
set(cloudFunction, '__trigger', {
eventTrigger: {
resource: 'ref/{wildcard}/nested/{anotherWildcard}',
Expand All @@ -53,8 +52,11 @@ describe('main', () => {
const context = wrap(constructCF())('data').context;
expect(typeof context.eventId).to.equal('string');
expect(context.resource.service).to.equal('service');
expect(/ref\/wildcard[1-9]\/nested\/anotherWildcard[1-9]/
.test(context.resource.name)).to.be.true;
expect(
/ref\/wildcard[1-9]\/nested\/anotherWildcard[1-9]/.test(
context.resource.name
)
).to.be.true;
expect(context.eventType).to.equal('event');
expect(Date.parse(context.timestamp)).to.be.greaterThan(0);
expect(context.params).to.deep.equal({});
Expand All @@ -73,7 +75,9 @@ describe('main', () => {
});

it('should generate auth and authType for database functions', () => {
const context = wrap(constructCF('google.firebase.database.ref.write'))('data').context;
const context = wrap(constructCF('google.firebase.database.ref.write'))(
'data'
).context;
expect(context.auth).to.equal(null);
expect(context.authType).to.equal('UNAUTHENTICATED');
});
Expand All @@ -90,10 +94,12 @@ describe('main', () => {

it('should throw when passed invalid options', () => {
const wrapped = wrap(constructCF());
expect(() => wrapped('data', {
expect(() =>
wrapped('data', {
auth: { uid: 'abc' },
isInvalid: true,
} as any)).to.throw();
} as any)
).to.throw();
});

it('should generate the appropriate resource based on params', () => {
Expand Down
11 changes: 7 additions & 4 deletions spec/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ describe('providers/database', () => {
});

it('produces the right snapshot with makeDataSnapshot', async () => {
const snapshot = makeDataSnapshot({
foo: 'bar',
}, 'path');
const snapshot = makeDataSnapshot(
{
foo: 'bar',
},
'path'
);

expect(snapshot.val()).to.deep.equal({foo: 'bar'});
expect(snapshot.val()).to.deep.equal({ foo: 'bar' });
expect(snapshot.ref.key).to.equal('path');
});

Expand Down
14 changes: 10 additions & 4 deletions spec/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ describe('providers/firestore', () => {
it('produces the right snapshot with makeDocumentSnapshot', async () => {
const test = fft();

const snapshot = test.firestore.makeDocumentSnapshot({
email_address: '[email protected]',
}, 'collection/doc-id');
const snapshot = test.firestore.makeDocumentSnapshot(
{
email_address: '[email protected]',
},
'collection/doc-id'
);

expect(snapshot.data()).to.deep.equal({
email_address: '[email protected]',
Expand All @@ -18,7 +21,10 @@ describe('providers/firestore', () => {
it('should allow empty document in makeDocumentSnapshot', async () => {
const test = fft();

const snapshot = test.firestore.makeDocumentSnapshot({}, 'collection/doc-id');
const snapshot = test.firestore.makeDocumentSnapshot(
{},
'collection/doc-id'
);

expect(snapshot.data()).to.deep.equal(undefined);
expect(snapshot.id).to.equal('doc-id');
Expand Down
17 changes: 11 additions & 6 deletions spec/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,29 @@ describe('providers/https', () => {
it('should run the wrapped onCall function and return result', async () => {
const test = fft();

const result = await test.wrap(cfToUpperCaseOnCall)({ message: 'lowercase' });
const result = await test.wrap(cfToUpperCaseOnCall)({
message: 'lowercase',
});

expect(result).to.deep.equal({ msg: 'LOWERCASE', from: 'anonymous' });
});

it('should accept auth params', async () => {
const test = fft();
const options = {auth: {uid: 'abc'}};
const options = { auth: { uid: 'abc' } };

const result = await test.wrap(cfToUpperCaseOnCall)({ message: 'lowercase' }, options);
const result = await test.wrap(cfToUpperCaseOnCall)(
{ message: 'lowercase' },
options
);

expect(result).to.deep.equal({msg: 'LOWERCASE', from: 'abc'});
expect(result).to.deep.equal({ msg: 'LOWERCASE', from: 'abc' });
});

it('should accept raw request', async () => {
const mockRequest: any = (sessionData) => {
return {
session: {data: sessionData},
session: { data: sessionData },
};
};
mockRequest.rawBody = Buffer.from('foobar');
Expand All @@ -65,7 +70,7 @@ describe('providers/https', () => {

const result = await test.wrap(cfToUpperCaseOnCall)(
{ message: 'lowercase' },
options,
options
);

expect(result).to.deep.equal({
Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function testApp(): testApp.App {
/** @internal */
export namespace testApp {
export let singleton: testApp.App;
export let init = () => singleton = new testApp.App();
export let init = () => (singleton = new testApp.App());

export class App {
appSingleton: firebase.app.App;
Expand All @@ -44,7 +44,7 @@ export namespace testApp {
this.appSingleton = firebase.initializeApp(
JSON.parse(process.env.FIREBASE_CONFIG),
// Give this app a name so it does not conflict with apps that user initialized.
'firebase-functions-test',
'firebase-functions-test'
);
}
return this.appSingleton;
Expand Down
34 changes: 18 additions & 16 deletions src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,39 @@ export class FirebaseFunctionsTest {

/** Initialize the SDK. */
init(
/** Firebase config values for initializing a Firebase app for your test code to
* interact with (e.g. making database writes). It is recommended that you use
* a project that is specifically for testing. If omitted, mock config values will
* be used and your tests will not interact with a real Firebase app, and all Firebase
* methods need to be stubbed, otherwise they will fail.
*/
firebaseConfig?: AppOptions,
/** Path to a service account key file to be used when initializing the Firebase app. */
pathToServiceAccountKey?: string,
) {

/** Firebase config values for initializing a Firebase app for your test code to
* interact with (e.g. making database writes). It is recommended that you use
* a project that is specifically for testing. If omitted, mock config values will
* be used and your tests will not interact with a real Firebase app, and all Firebase
* methods need to be stubbed, otherwise they will fail.
*/
firebaseConfig?: AppOptions,
/** Path to a service account key file to be used when initializing the Firebase app. */
pathToServiceAccountKey?: string
) {
this._oldEnv = {
FIREBASE_CONFIG: process.env.FIREBASE_CONFIG,
GOOGLE_APPLICATION_CREDENTIALS: process.env.GOOGLE_APPLICATION_CREDENTIALS,
GOOGLE_APPLICATION_CREDENTIALS:
process.env.GOOGLE_APPLICATION_CREDENTIALS,
GCLOUD_PROJECT: process.env.GCLOUD_PROJECT,
CLOUD_RUNTIME_CONFIG: process.env.CLOUD_RUNTIME_CONFIG,
};

if (isEmpty(firebaseConfig)) {
process.env.FIREBASE_CONFIG = JSON.stringify({
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
projectId: 'not-a-project',
databaseURL: 'https://not-a-project.firebaseio.com',
storageBucket: 'not-a-project.appspot.com',
projectId: 'not-a-project',
});
} else {
process.env.FIREBASE_CONFIG = JSON.stringify(firebaseConfig);
if (pathToServiceAccountKey) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = pathToServiceAccountKey;
}
}
process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
process.env.GCLOUD_PROJECT = JSON.parse(
process.env.FIREBASE_CONFIG
).projectId;
}

/** Complete clean up tasks. */
Expand Down
Loading