Skip to content

Applying Linter on Tests #201

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 16 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 15 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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
"homepage": "https://firebase.google.com/",
"scripts": {
"build": "gulp build",
"lint": "tslint --project tsconfig-lint.json --format stylish",
"lint": "run-s lint:src lint:unit lint:integration",
Copy link
Contributor

Choose a reason for hiding this comment

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

npm-run-all supports glob-like pattern matching, so you can do run-s lint:*.

Why did you decide to run these in sequence instead of parallel?

"test": "run-s lint test:unit",
"integration": "run-s build test:integration",
"test:unit": "mocha test/unit/*.spec.ts --compilers ts:ts-node/register",
"test:integration": "mocha test/integration/*.ts --slow 5000 --compilers ts:ts-node/register",
"test:coverage": "nyc npm run test:unit"
"test:coverage": "nyc npm run test:unit",
"lint:src": "tslint --format stylish -p tsconfig.json",
"lint:unit": "tslint -c tslint-test.json --format stylish test/unit/*.ts test/unit/**/*.ts",
"lint:integration": "tslint -c tslint-test.json --format stylish test/integration/*.ts"
},
"nyc": {
"extension": [
Expand Down
14 changes: 7 additions & 7 deletions test/integration/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ describe('admin', () => {
});

it('does not load Firestore by default', () => {
let gcloud = require.cache[require.resolve('@google-cloud/firestore')];
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
expect(gcloud).to.be.undefined;
});

it('loads Firestore when calling admin.firestore', () => {
const firestoreNamespace = admin.firestore;
expect(firestoreNamespace).to.not.be.null;
let gcloud = require.cache[require.resolve('@google-cloud/firestore')];
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
expect(gcloud).to.not.be.undefined;
});
});

describe('admin.app', () => {
it('admin.app() returns the default App', () => {
let app = admin.app();
const app = admin.app();
expect(app).to.deep.equal(defaultApp);
expect(app.name).to.equal('[DEFAULT]');
expect(app.options.databaseURL).to.equal(databaseUrl);
Expand All @@ -51,7 +51,7 @@ describe('admin.app', () => {
});

it('admin.app("null") returns the App named "null"', () => {
let app = admin.app('null');
const app = admin.app('null');
expect(app).to.deep.equal(nullApp);
expect(app.name).to.equal('null');
expect(app.options.databaseURL).to.equal(databaseUrl);
Expand All @@ -60,7 +60,7 @@ describe('admin.app', () => {
});

it('admin.app("nonNull") returns the App named "nonNull"', () => {
let app = admin.app('nonNull');
const app = admin.app('nonNull');
expect(app).to.deep.equal(nonNullApp);
expect(app.name).to.equal('nonNull');
expect(app.options.databaseURL).to.equal(databaseUrl);
Expand All @@ -69,15 +69,15 @@ describe('admin.app', () => {
});

it('namespace services are attached to the default App', () => {
let app = admin.app();
const app = admin.app();
expect(admin.auth(app).app).to.deep.equal(app);
expect(admin.database(app).app).to.deep.equal(app);
expect(admin.messaging(app).app).to.deep.equal(app);
expect(admin.storage(app).app).to.deep.equal(app);
});

it('namespace services are attached to the named App', () => {
let app = admin.app('null');
const app = admin.app('null');
expect(admin.auth(app).app).to.deep.equal(app);
expect(admin.database(app).app).to.deep.equal(app);
expect(admin.messaging(app).app).to.deep.equal(app);
Expand Down
21 changes: 11 additions & 10 deletions test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as admin from '../../lib/index';
import {expect} from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import firebase = require('firebase');
Expand All @@ -25,6 +24,8 @@ import {generateRandomString, projectId, apiKey} from './setup';
chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

const newUserUid = generateRandomString(20);
const nonexistentUid = generateRandomString(20);
const testPhoneNumber = '+11234567890';
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('admin.auth', () => {
});

it('createUser() creates a new user when called without a UID', () => {
let newUserData = clone(mockUserData);
const newUserData = clone(mockUserData);
newUserData.email = generateRandomString(20) + '@example.com';
newUserData.phoneNumber = testPhoneNumber2;
return admin.auth().createUser(newUserData)
Expand All @@ -79,7 +80,7 @@ describe('admin.auth', () => {
});

it('createUser() creates a new user with the specified UID', () => {
let newUserData: any = clone(mockUserData);
const newUserData: any = clone(mockUserData);
newUserData.uid = newUserUid;
return admin.auth().createUser(newUserData)
.then((userRecord) => {
Expand All @@ -92,7 +93,7 @@ describe('admin.auth', () => {
});

it('createUser() fails when the UID is already in use', () => {
let newUserData: any = clone(mockUserData);
const newUserData: any = clone(mockUserData);
newUserData.uid = newUserUid;
return admin.auth().createUser(newUserData)
.should.eventually.be.rejected.and.have.property('code', 'auth/uid-already-exists');
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('admin.auth', () => {
});

it('listUsers() returns up to the specified number of users', () => {
let promises: Promise<admin.auth.UserRecord>[] = [];
const promises: Array<Promise<admin.auth.UserRecord>> = [];
uids.forEach((uid) => {
const tempUserData = {
uid,
Expand Down Expand Up @@ -168,7 +169,7 @@ describe('admin.auth', () => {
.then((decodedIdToken) => {
// Verification should succeed. Revoke that user's session.
return new Promise((resolve) => setTimeout(() => resolve(
admin.auth().revokeRefreshTokens(decodedIdToken.sub)
admin.auth().revokeRefreshTokens(decodedIdToken.sub),
), 1000));
})
.then(() => {
Expand Down Expand Up @@ -224,7 +225,7 @@ describe('admin.auth', () => {
})
.then((decodedIdToken) => {
// Confirm expected claims set on the user's ID token.
for (let key in customClaims) {
for (const key in customClaims) {
if (customClaims.hasOwnProperty(key)) {
expect(decodedIdToken[key]).to.equal(customClaims[key]);
}
Expand Down Expand Up @@ -331,12 +332,12 @@ function deletePhoneNumberUser(phoneNumber) {
/**
* Runs cleanup routine that could affect outcome of tests and removes any
* intermediate users created.
*
*
* @return {Promise} A promise that resolves when test preparations are ready.
*/
function cleanup() {
// Delete any existing users that could affect the test outcome.
let promises: Promise<void>[] = [
const promises: Array<Promise<void>> = [
deletePhoneNumberUser(testPhoneNumber),
deletePhoneNumberUser(testPhoneNumber2),
deletePhoneNumberUser(nonexistentPhoneNumber),
Expand All @@ -351,7 +352,7 @@ function cleanup() {
if (error.code !== 'auth/user-not-found') {
throw error;
}
})
}),
);
});
return Promise.all(promises);
Expand Down
9 changes: 5 additions & 4 deletions test/integration/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as admin from '../../lib/index';
import {expect} from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import url = require('url');
Expand All @@ -29,6 +28,8 @@ const chalk = require('chalk');
chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

const path = 'adminNodeSdkManualTest';

describe('admin.database', () => {
Expand Down Expand Up @@ -99,7 +100,7 @@ describe('admin.database', () => {
it('once() returns the current value of the reference', () => {
return ref.once('value')
.then((snapshot) => {
let value = snapshot.val();
const value = snapshot.val();
expect(value.success).to.be.true;
expect(typeof value.timestamp).to.equal('number');
});
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('admin.database', () => {
it('once() returns the current value of the reference', () => {
return refWithUrl.once('value')
.then((snapshot) => {
let value = snapshot.val();
const value = snapshot.val();
expect(value.success).to.be.true;
expect(typeof value.timestamp).to.equal('number');
});
Expand All @@ -165,6 +166,6 @@ function addValueEventListener(
callback: (s: admin.database.DataSnapshot) => any) {
// Check for type compilation. This method is not invoked by any tests. But it will
// trigger a TS compilation failure if the RTDB typings were not loaded correctly.
let eventType: admin.database.EventType = 'value';
const eventType: admin.database.EventType = 'value';
db.ref().on(eventType, callback);
}
35 changes: 18 additions & 17 deletions test/integration/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as admin from '../../lib/index';
import {expect} from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {clone} from 'lodash';
Expand All @@ -24,6 +23,8 @@ import {DocumentReference} from '@google-cloud/firestore';
chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

const mountainView = {
name: 'Mountain View',
population: 77846,
Expand All @@ -49,31 +50,31 @@ describe('admin.firestore', () => {

it('supports basic data access', () => {
return reference.set(mountainView)
.then(result => {
.then((result) => {
return reference.get();
})
.then(snapshot => {
let data = snapshot.data();
.then((snapshot) => {
const data = snapshot.data();
expect(data).to.deep.equal(mountainView);
return reference.delete();
})
.then(result => {
.then((result) => {
return reference.get();
})
.then(snapshot => {
.then((snapshot) => {
expect(snapshot.exists).to.be.false;
});
}).timeout(5000);

it('admin.firestore.FieldValue.serverTimestamp() provides a server-side timestamp', () => {
let expected: any = clone(mountainView);
const expected: any = clone(mountainView);
expected.timestamp = admin.firestore.FieldValue.serverTimestamp();
return reference.set(expected)
.then(result => {
.then((result) => {
return reference.get();
})
.then(snapshot => {
let data = snapshot.data();
.then((snapshot) => {
const data = snapshot.data();
expect(data.timestamp).is.not.null;
expect(data.timestamp instanceof Date).is.true;
return reference.delete();
Expand All @@ -97,16 +98,16 @@ describe('admin.firestore', () => {
const source = admin.firestore().collection('cities').doc();
const target = admin.firestore().collection('cities').doc();
return source.set(mountainView)
.then(result => {
.then((result) => {
return target.set({name: 'Palo Alto', sisterCity: source});
})
.then(result => {
.then((result) => {
return target.get();
})
.then(snapshot => {
let data = snapshot.data();
.then((snapshot) => {
const data = snapshot.data();
expect(data.sisterCity.path).to.deep.equal(source.path);
let promises = [];
const promises = [];
promises.push(source.delete());
promises.push(target.delete());
return Promise.all(promises);
Expand All @@ -121,10 +122,10 @@ describe('admin.firestore', () => {
logs.push(log);
});
return source.set({name: 'San Francisco'})
.then(result => {
.then((result) => {
return source.delete();
})
.then(result => {
.then((result) => {
expect(logs.length).greaterThan(0);
});
}).timeout(5000);
Expand Down
3 changes: 2 additions & 1 deletion test/integration/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/

import * as admin from '../../lib/index';
import {expect} from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

// The registration token and notification key have the proper format, but are not guaranteed to
// work. The intention of these integration tests is that the endpoints returns the proper payload,
// but it is hard to ensure these tokens will always be valid. The tests below should still pass
Expand Down
4 changes: 2 additions & 2 deletions test/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ before(() => {
console.log(chalk.red(
'The integration test suite requires a service account JSON file for a ' +
'Firebase project to be saved to `test/resources/key.json`.',
error
error,
));
throw error;
}
Expand All @@ -55,7 +55,7 @@ before(() => {
console.log(chalk.red(
'The integration test suite requires an API key for a ' +
'Firebase project to be saved to `test/resources/apikey.txt`.',
error
error,
));
throw error;
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as admin from '../../lib/index';
import {expect} from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {Bucket, File} from '@google-cloud/storage';
Expand All @@ -25,6 +24,8 @@ import {projectId} from './setup';
chai.should();
chai.use(chaiAsPromised);

const expect = chai.expect;

describe('admin.storage', () => {
it('bucket() returns a handle to the default bucket', () => {
const bucket: Bucket = admin.storage().bucket();
Expand Down
Loading