From 04f3b1ace1025d7302cb1fbf923403e1c22bcc95 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Singh Date: Wed, 27 Feb 2019 17:29:37 +0530 Subject: [PATCH] noImplicitAny for index test file --- src/topic.ts | 16 +- test/index.ts | 591 ++++++++++++++++++++++++++------------------------ 2 files changed, 313 insertions(+), 294 deletions(-) diff --git a/src/topic.ts b/src/topic.ts index a0eedd6e3..829ef66a6 100644 --- a/src/topic.ts +++ b/src/topic.ts @@ -308,13 +308,7 @@ export class Topic { callback(null, true); return; } - let code = 0; - if (err.hasOwnProperty('code')) { - code = - (Object.getOwnPropertyDescriptor(err, 'code') as PropertyDescriptor) - .value; - } - if (code === 5) { + if (err.code === 5) { callback(null, false); return; } @@ -376,13 +370,7 @@ export class Topic { callback!(null, this, apiResponse!); return; } - let code = 0; - if (err.hasOwnProperty('code')) { - code = - (Object.getOwnPropertyDescriptor(err, 'code') as PropertyDescriptor) - .value; - } - if (code !== 5 || !autoCreate) { + if (err.code !== 5 || !autoCreate) { callback!(err, null, apiResponse!); return; } diff --git a/test/index.ts b/test/index.ts index a79e97455..181460e71 100644 --- a/test/index.ts +++ b/test/index.ts @@ -19,17 +19,23 @@ import * as promisify from '@google-cloud/promisify'; import * as arrify from 'arrify'; import * as assert from 'assert'; import * as gax from 'google-gax'; +import {CallOptions} from 'grpc'; import * as proxyquire from 'proxyquire'; +import * as sinon from 'sinon'; +import {google} from '../proto/pubsub'; +import * as pubsubTypes from '../src'; +import {Snapshot} from '../src/snapshot'; import * as subby from '../src/subscription'; import * as util from '../src/util'; -const PKG = require('../../package.json'); +const PKG = require('../../package.json'); +const sandbox = sinon.createSandbox(); const fakeCreds = {}; const fakeGoogleGax = { GrpcClient: class extends gax.GrpcClient{ - constructor(opts) { + constructor(opts: gax.GrpcClientOptions) { super(opts); this.grpc = { credentials: { @@ -43,17 +49,21 @@ const fakeGoogleGax = { }; const subscriptionCached = subby.Subscription; -let subscriptionOverride; -function Subscription(a, b, c) { +// tslint:disable-next-line no-any +let subscriptionOverride: any; + +function Subscription( + pubsub: pubsubTypes.PubSub, name: string, + options: pubsubTypes.SubscriptionCallOptions) { const overrideFn = subscriptionOverride || subscriptionCached; - return new overrideFn(a, b, c); + return new overrideFn(pubsub, name, options); } let promisified = false; const fakePromisify = Object.assign({}, promisify, { // tslint:disable-next-line variable-name - promisifyAll(Class, options) { + promisifyAll(Class: Function, options: promisify.PromisifyAllOptions) { if (Class.name !== 'PubSub') { return; } @@ -68,7 +78,7 @@ const fakePromisify = Object.assign({}, promisify, { }, }); -let pjyOverride; +let pjyOverride: Function; function fakePjy() { return (pjyOverride || pjy.replaceProjectIdToken).apply(null, arguments); } @@ -91,7 +101,7 @@ class FakeTopic { let extended = false; const fakePaginator = { // tslint:disable-next-line variable-name - extend(Class, methods) { + extend(Class: Function, methods: string[]) { if (Class.name !== 'PubSub') { return; } @@ -106,12 +116,12 @@ const fakePaginator = { extended = true; }, - streamify(methodName) { + streamify(methodName: string) { return methodName; }, }; -let googleAuthOverride; +let googleAuthOverride: Function|null; function fakeGoogleAuth() { return (googleAuthOverride || util.noop).apply(null, arguments); } @@ -120,8 +130,9 @@ const v1Override = {}; // tslint:disable-next-line no-any let v1ClientOverrides: any = {}; -function defineOverridableClient(clientName) { +function defineOverridableClient(clientName: string) { function DefaultClient() {} + /** @type {never[]} */ DefaultClient.scopes = []; Object.defineProperty(v1Override, clientName, { @@ -137,13 +148,14 @@ defineOverridableClient('SubscriberClient'); describe('PubSub', () => { // tslint:disable-next-line variable-name - let PubSub; + let PubSub: typeof pubsubTypes.PubSub; const PROJECT_ID = 'test-project'; - let pubsub; + // tslint:disable-next-line no-any + let pubsub: pubsubTypes.PubSub; const OPTIONS = { projectId: PROJECT_ID, promise: {}, - }; + } as pubsubTypes.ClientConfig; const PUBSUB_EMULATOR_HOST = process.env.PUBSUB_EMULATOR_HOST; @@ -177,7 +189,6 @@ describe('PubSub', () => { beforeEach(() => { v1ClientOverrides = {}; googleAuthOverride = null; - subscriptionOverride = null; pubsub = new PubSub(OPTIONS); pubsub.projectId = PROJECT_ID; }); @@ -218,7 +229,9 @@ describe('PubSub', () => { v1ClientOverrides.PublisherClient.scopes = ['b', 'c', 'd', 'e']; const pubsub = new PubSub({}); - assert.deepStrictEqual(pubsub.options.scopes, ['a', 'b', 'c', 'd', 'e']); + const options = {scopes: ['a', 'b', 'c', 'd', 'e']}; + const expectedOptions = Object.assign({}, DEFAULT_OPTIONS, options); + assert.deepStrictEqual(pubsub.options, expectedOptions); }); it('should attempt to determine the service path and port', () => { @@ -244,10 +257,10 @@ describe('PubSub', () => { const options = { a: 'b', c: 'd', - }; + } as pubsubTypes.ClientConfig; const expectedOptions = Object.assign({}, DEFAULT_OPTIONS, options); - googleAuthOverride = options_ => { + googleAuthOverride = (options_: pubsubTypes.ClientConfig) => { assert.deepStrictEqual(options_, expectedOptions); return fakeGoogleAuthInstance; }; @@ -282,9 +295,10 @@ describe('PubSub', () => { describe('createSubscription', () => { const TOPIC_NAME = 'topic'; + pubsub = new pubsubTypes.PubSub({}); const TOPIC = Object.assign(new FakeTopic(), { name: 'projects/' + PROJECT_ID + '/topics/' + TOPIC_NAME, - }); + }) as {} as pubsubTypes.Topic; const SUB_NAME = 'subscription'; const SUBSCRIPTION = { @@ -295,60 +309,43 @@ describe('PubSub', () => { name: 'subscription-name', }; - beforeEach(() => { - // tslint:disable-next-line no-any - (Subscription as any).formatMetadata_ = metadata => { - return Object.assign({}, metadata); - }; - }); - it('should throw if no Topic is provided', () => { - assert.throws(() => { - pubsub.createSubscription(); - }, /A Topic is required for a new subscription\./); - }); - it('should throw if no subscription name is provided', () => { - assert.throws(() => { - pubsub.createSubscription(TOPIC_NAME); - }, /A subscription name is required./); + beforeEach(() => { + (Subscription as {} as typeof subby.Subscription).formatMetadata_ = + (metadata: subby.SubscriptionMetadataRaw) => { + return Object.assign({}, metadata) as subby.SubscriptionMetadata; + }; }); it('should not require configuration options', done => { - pubsub.request = (config, callback) => { - callback(null, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse); + }; pubsub.createSubscription(TOPIC, SUB_NAME, done); }); - it('should allow undefined/optional configuration options', done => { - pubsub.request = (config, callback) => { - callback(null, apiResponse); - }; - - pubsub.createSubscription(TOPIC, SUB_NAME, undefined, done); - }); - it('should create a Subscription', done => { - const opts = {a: 'b', c: 'd'}; + const opts = {a: 'b', c: 'd'} as pubsubTypes.CreateSubscriptionOptions; pubsub.request = util.noop; - pubsub.subscription = (subName, options) => { - assert.strictEqual(subName, SUB_NAME); - assert.deepStrictEqual(options, opts); - setImmediate(done); - return SUBSCRIPTION; - }; - + pubsub.subscription = + (subName: string, options: pubsubTypes.CreateSubscriptionOptions) => { + assert.strictEqual(subName, SUB_NAME); + assert.deepStrictEqual(options, opts); + setImmediate(done); + return SUBSCRIPTION as subby.Subscription; + }; pubsub.createSubscription(TOPIC, SUB_NAME, opts, assert.ifError); }); it('should create a Topic object from a string', done => { pubsub.request = util.noop; - pubsub.topic = topicName => { + pubsub.topic = (topicName: string) => { assert.strictEqual(topicName, TOPIC_NAME); setImmediate(done); return TOPIC; @@ -365,20 +362,21 @@ describe('PubSub', () => { pubsub.topic = topicName => { return { name: topicName, - }; + } as pubsubTypes.Topic; }; - - pubsub.subscription = subName => { + pubsub.subscription = (subName: string) => { return { name: subName, - }; + } as pubsubTypes.Subscription; }; - pubsub.request = config => { + + const reqOpts = {topic: TOPIC.name, name: SUB_NAME}; + + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.client, 'SubscriberClient'); assert.strictEqual(config.method, 'createSubscription'); - assert.strictEqual(config.reqOpts.topic, TOPIC.name); - assert.strictEqual(config.reqOpts.name, SUB_NAME); + assert.deepStrictEqual(config.reqOpts, reqOpts); assert.deepStrictEqual(config.gaxOpts, options.gaxOpts); done(); }; @@ -392,26 +390,23 @@ describe('PubSub', () => { pushEndpoint: 'https://domain/push', }; - const expectedBody = Object.assign( - { - topic: TOPIC.name, - name: SUB_NAME, - }, - options); + const expectedBody = + Object.assign({topic: TOPIC.name, name: SUB_NAME}, options); pubsub.topic = () => { return { name: TOPIC_NAME, - }; + } as pubsubTypes.Topic; }; pubsub.subscription = () => { return { name: SUB_NAME, - }; + } as pubsubTypes.Subscription; }; - pubsub.request = config => { + + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.notStrictEqual(config.reqOpts, options); assert.deepStrictEqual(config.reqOpts, expectedBody); done(); @@ -433,16 +428,16 @@ describe('PubSub', () => { pubsub.topic = () => { return { name: TOPIC_NAME, - }; + } as pubsubTypes.Topic; }; pubsub.subscription = () => { return { name: SUB_NAME, - }; + } as pubsubTypes.Subscription; }; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.notStrictEqual(config.reqOpts, options); assert.deepStrictEqual(config.reqOpts, expectedBody); done(); @@ -457,13 +452,13 @@ describe('PubSub', () => { a: 'a', }; - // tslint:disable-next-line no-any - (Subscription as any).formatMetadata_ = metadata => { - assert.deepStrictEqual(metadata, fakeMetadata); - return formatted; - }; + (Subscription as {} as typeof subby.Subscription).formatMetadata_ = + (metadata: subby.SubscriptionMetadataRaw) => { + assert.deepStrictEqual(metadata, fakeMetadata); + return formatted as {} as subby.SubscriptionMetadata; + }; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.reqOpts, formatted); done(); }; @@ -476,17 +471,21 @@ describe('PubSub', () => { const apiResponse = {name: SUB_NAME}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(error, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(error, apiResponse); + }; }); it('should return error & API response to the callback', done => { - pubsub.request = (config, callback) => { - callback(error, apiResponse); - }; - - function callback(err, sub, resp) { + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(error, apiResponse); + }; + + function callback( + err?: Error|null, sub?: subby.Subscription|null, + resp?: google.pubsub.v1.ISubscription) { assert.strictEqual(err, error); assert.strictEqual(sub, null); assert.strictEqual(resp, apiResponse); @@ -501,23 +500,26 @@ describe('PubSub', () => { const apiResponse = {name: SUB_NAME}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(null, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse); + }; }); it('should return Subscription & resp to the callback', done => { const subscription = {}; - pubsub.subscription = () => { - return subscription; + return subscription as pubsubTypes.Subscription; }; - pubsub.request = (config, callback) => { - callback(null, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse); + }; - function callback(err, sub, resp) { + function callback( + err?: Error|null, sub?: subby.Subscription|null, + resp?: google.pubsub.v1.ISubscription) { assert.ifError(err); assert.strictEqual(sub, subscription); assert.strictEqual(resp, apiResponse); @@ -531,19 +533,22 @@ describe('PubSub', () => { describe('createTopic', () => { it('should make the correct API request', done => { + const pubsub = new pubsubTypes.PubSub(); const topicName = 'new-topic-name'; const formattedName = 'formatted-name'; const gaxOpts = {}; - pubsub.topic = name => { + pubsub.topic = (name: string) => { assert.strictEqual(name, topicName); return { name: formattedName, - }; + } as pubsubTypes.Topic; }; - pubsub.request = config => { + + + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.client, 'PublisherClient'); assert.strictEqual(config.method, 'createTopic'); assert.deepStrictEqual(config.reqOpts, {name: formattedName}); @@ -559,18 +564,22 @@ describe('PubSub', () => { const apiResponse = {}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(error, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(error, apiResponse); + }; }); it('should return an error & API response', done => { - pubsub.createTopic('new-topic', (err, topic, apiResponse_) => { - assert.strictEqual(err, error); - assert.strictEqual(topic, null); - assert.strictEqual(apiResponse_, apiResponse); - done(); - }); + pubsub.createTopic( + 'new-topic', + (err?: Error|null, topic?: pubsubTypes.Topic|null, + apiResponse_?: google.pubsub.v1.ITopic) => { + assert.strictEqual(err, error); + assert.strictEqual(topic, null); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); }); }); @@ -578,39 +587,44 @@ describe('PubSub', () => { const apiResponse = {}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(null, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse); + }; }); it('should return a Topic object', done => { const topicName = 'new-topic'; const topicInstance = {}; - pubsub.topic = name => { + pubsub.topic = (name: string) => { assert.strictEqual(name, topicName); - return topicInstance; + return topicInstance as pubsubTypes.Topic; }; - pubsub.createTopic(topicName, (err, topic) => { - assert.ifError(err); - assert.strictEqual(topic, topicInstance); - done(); - }); + pubsub.createTopic( + topicName, (err?: Error|null, topic?: pubsubTypes.Topic|null) => { + assert.ifError(err); + assert.strictEqual(topic, topicInstance); + done(); + }); }); it('should pass apiResponse to callback', done => { - pubsub.createTopic('new-topic', (err, topic, apiResponse_) => { - assert.ifError(err); - assert.strictEqual(apiResponse_, apiResponse); - done(); - }); + pubsub.createTopic( + 'new-topic', + (err?: Error|null, topic?: pubsubTypes.Topic|null, + apiResponse_?: google.pubsub.v1.ITopic) => { + assert.ifError(err); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); }); }); }); describe('determineBaseUrl_', () => { - function setHost(host) { + function setHost(host: string) { process.env.PUBSUB_EMULATOR_HOST = host; } @@ -683,9 +697,10 @@ describe('PubSub', () => { const apiResponse = {snapshots: [{name: SNAPSHOT_NAME}]}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(null, apiResponse.snapshots, {}, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse.snapshots, {}, apiResponse); + }; }); it('should accept a query and a callback', done => { @@ -704,7 +719,7 @@ describe('PubSub', () => { e: 'f', }, autoPaginate: false, - }; + } as {} as pubsubTypes.GetSnapshotsOptions; const expectedOptions = Object.assign({}, options, { project: 'projects/' + pubsub.projectId, @@ -719,7 +734,7 @@ describe('PubSub', () => { delete expectedOptions.gaxOpts; delete expectedOptions.autoPaginate; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.client, 'SubscriberClient'); assert.strictEqual(config.method, 'listSnapshots'); assert.deepStrictEqual(config.reqOpts, expectedOptions); @@ -733,15 +748,15 @@ describe('PubSub', () => { it('should return Snapshot instances with metadata', done => { const snapshot = {}; - pubsub.snapshot = name => { + sandbox.stub(pubsub, 'snapshot').callsFake((name: string) => { assert.strictEqual(name, SNAPSHOT_NAME); - return snapshot; - }; + return snapshot as Snapshot; + }); - pubsub.getSnapshots((err, snapshots) => { + pubsub.getSnapshots((err?: Error|null, snapshots?: Snapshot[]|null) => { assert.ifError(err); - assert.strictEqual(snapshots[0], snapshot); - assert.strictEqual(snapshots[0].metadata, apiResponse.snapshots[0]); + assert.strictEqual(snapshots![0], snapshot); + assert.strictEqual(snapshots![0].metadata, apiResponse.snapshots[0]); done(); }); }); @@ -752,17 +767,19 @@ describe('PubSub', () => { const nextQuery_ = {}; const apiResponse_ = {}; - pubsub.request = (config, callback) => { - callback(err_, snapshots_, nextQuery_, apiResponse_); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(err_, snapshots_, nextQuery_, apiResponse_); + }; - pubsub.getSnapshots((err, snapshots, nextQuery, apiResponse) => { - assert.strictEqual(err, err_); - assert.deepStrictEqual(snapshots, snapshots_); - assert.strictEqual(nextQuery, nextQuery_); - assert.strictEqual(apiResponse, apiResponse_); - done(); - }); + pubsub.getSnapshots( + (err?: Error|null, snapshots?: Snapshot[]|null, + apiResponse?: google.pubsub.v1.ISnapshot) => { + assert.strictEqual(err, err_); + assert.deepStrictEqual(snapshots, snapshots_); + assert.strictEqual(apiResponse, nextQuery_); + done(); + }); }); }); @@ -770,9 +787,10 @@ describe('PubSub', () => { const apiResponse = {subscriptions: [{name: 'fake-subscription'}]}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(null, apiResponse.subscriptions, {}, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse.subscriptions, {}, apiResponse); + }; }); it('should accept a query and a callback', done => { @@ -789,7 +807,7 @@ describe('PubSub', () => { a: 'b', }, autoPaginate: false, - }; + } as {} as pubsubTypes.SubscriptionCallOptions; const expectedGaxOpts = Object.assign( { @@ -799,7 +817,7 @@ describe('PubSub', () => { const project = 'projects/' + pubsub.projectId; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.client, 'SubscriberClient'); assert.strictEqual(config.method, 'listSubscriptions'); assert.deepStrictEqual(config.reqOpts, {project}); @@ -813,10 +831,12 @@ describe('PubSub', () => { it('should pass options to API request', done => { const opts = {pageSize: 10, pageToken: 'abc'}; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { const reqOpts = config.reqOpts; - assert.strictEqual(reqOpts.pageSize, opts.pageSize); - assert.strictEqual(reqOpts.pageToken, opts.pageToken); + const expectedOptions = Object.assign({}, opts, { + project: 'projects/' + pubsub.projectId, + }); + assert.deepStrictEqual(reqOpts, expectedOptions); done(); }; @@ -824,11 +844,12 @@ describe('PubSub', () => { }); it('should return Subscription instances', done => { - pubsub.getSubscriptions((err, subscriptions) => { - assert.ifError(err); - assert(subscriptions[0] instanceof subscriptionCached); - done(); - }); + pubsub.getSubscriptions( + (err?: Error|null, subscriptions?: subby.Subscription[]|null) => { + assert.ifError(err); + assert(subscriptions![0] instanceof subscriptionCached); + done(); + }); }); it('should pass back all params', done => { @@ -837,17 +858,20 @@ describe('PubSub', () => { const nextQuery_ = {}; const apiResponse_ = {}; - pubsub.request = (config, callback) => { - callback(err_, subs_, nextQuery_, apiResponse_); - }; - pubsub.getSubscriptions((err, subs, nextQuery, apiResponse) => { - assert.strictEqual(err, err_); - assert.deepStrictEqual(subs, subs_); - assert.strictEqual(nextQuery, nextQuery_); - assert.strictEqual(apiResponse, apiResponse_); - done(); - }); + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(err_, subs_, nextQuery_, apiResponse_); + }; + + pubsub.getSubscriptions( + (err?: Error|null, subs?: subby.Subscription[]|null, + apiResponse?: google.pubsub.v1.ISubscription) => { + assert.strictEqual(err, err_); + assert.deepStrictEqual(subs, subs_); + assert.strictEqual(apiResponse, nextQuery_); + done(); + }); }); describe('with topic', () => { @@ -858,12 +882,14 @@ describe('PubSub', () => { const opts = { topic, - }; + } as {} as pubsubTypes.SubscriptionCallOptions; - topic.getSubscriptions = (options, callback) => { - assert.strictEqual(options, opts); - callback(); // the done fn - }; + topic.getSubscriptions = + (options: pubsubTypes.SubscriptionCallOptions, + callback: pubsubTypes.RequestCallback) => { + assert.strictEqual(options, opts); + callback(); // the done fn + }; pubsub.getSubscriptions(opts, done); }); @@ -871,18 +897,20 @@ describe('PubSub', () => { it('should create a topic instance from a name', done => { const opts = { topic: TOPIC_NAME, - }; + } as {} as pubsubTypes.SubscriptionCallOptions; const fakeTopic = { - getSubscriptions(options, callback) { + getSubscriptions( + options: pubsubTypes.SubscriptionCallOptions, + callback: pubsubTypes.RequestCallback) { assert.strictEqual(options, opts); callback(); // the done fn }, }; - pubsub.topic = name => { + pubsub.topic = (name: string) => { assert.strictEqual(name, TOPIC_NAME); - return fakeTopic; + return fakeTopic as pubsubTypes.Topic; }; pubsub.getSubscriptions(opts, done); @@ -895,9 +923,10 @@ describe('PubSub', () => { const apiResponse = {topics: [{name: topicName}]}; beforeEach(() => { - pubsub.request = (config, callback) => { - callback(null, apiResponse.topics, {}, apiResponse); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, apiResponse.topics, {}, apiResponse); + }; }); it('should accept a query and a callback', done => { @@ -916,7 +945,7 @@ describe('PubSub', () => { e: 'f', }, autoPaginate: false, - }; + } as {} as pubsubTypes.GetTopicsOptions; const expectedOptions = Object.assign({}, options, { project: 'projects/' + pubsub.projectId, @@ -931,7 +960,7 @@ describe('PubSub', () => { delete expectedOptions.gaxOpts; delete expectedOptions.autoPaginate; - pubsub.request = config => { + pubsub.request = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config.client, 'PublisherClient'); assert.strictEqual(config.method, 'listTopics'); assert.deepStrictEqual(config.reqOpts, expectedOptions); @@ -945,17 +974,19 @@ describe('PubSub', () => { it('should return Topic instances with metadata', done => { const topic = {}; - pubsub.topic = name => { + pubsub.topic = (name: string) => { assert.strictEqual(name, topicName); - return topic; + return topic as pubsubTypes.Topic; }; - pubsub.getTopics((err, topics) => { - assert.ifError(err); - assert.strictEqual(topics[0], topic); - assert.strictEqual(topics[0].metadata, apiResponse.topics[0]); - done(); - }); + + pubsub.getTopics( + (err?: Error|null, topics?: pubsubTypes.Topic[]|null) => { + assert.ifError(err); + assert.strictEqual(topics![0], topic); + assert.strictEqual(topics![0].metadata, apiResponse.topics[0]); + done(); + }); }); it('should pass back all params', done => { @@ -964,17 +995,19 @@ describe('PubSub', () => { const nextQuery_ = {}; const apiResponse_ = {}; - pubsub.request = (config, callback) => { - callback(err_, topics_, nextQuery_, apiResponse_); - }; + pubsub.request = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(err_, topics_, nextQuery_, apiResponse_); + }; - pubsub.getTopics((err, topics, nextQuery, apiResponse) => { - assert.strictEqual(err, err_); - assert.deepStrictEqual(topics, topics_); - assert.strictEqual(nextQuery, nextQuery_); - assert.strictEqual(apiResponse, apiResponse_); - done(); - }); + pubsub.getTopics( + (err?: Error|null, topics?: pubsubTypes.Topic[]|null, + apiResponse?: google.pubsub.v1.ITopic) => { + assert.strictEqual(err, err_); + assert.deepStrictEqual(topics, topics_); + assert.strictEqual(apiResponse, nextQuery_); + done(); + }); }); }); @@ -984,26 +1017,26 @@ describe('PubSub', () => { method: 'fakeMethod', reqOpts: {a: 'a'}, gaxOpts: {b: 'b'}, - }; + } as pubsubTypes.RequestConfig; beforeEach(() => { delete pubsub.projectId; + afterEach(() => sandbox.restore()); - pubsub.auth = { - getProjectId(callback) { + sandbox.stub(pubsub, 'auth').value({ + getProjectId: (callback: Function) => { callback(null, PROJECT_ID); - }, - }; + } + }); - pjyOverride = reqOpts => { + // tslint:disable-next-line no-any + pjyOverride = (reqOpts: any) => { return reqOpts; }; - - pubsub.config = CONFIG; }); it('should call getClient_ with the correct config', done => { - pubsub.getClient_ = config => { + pubsub.getClient_ = (config: pubsubTypes.RequestConfig) => { assert.strictEqual(config, CONFIG); done(); }; @@ -1013,11 +1046,12 @@ describe('PubSub', () => { it('should return error from getClient_', done => { const expectedError = new Error('some error'); - pubsub.getClient_ = (config, callback) => { - callback(expectedError); - }; + pubsub.getClient_ = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(expectedError); + }; - pubsub.request(CONFIG, err => { + pubsub.request(CONFIG, (err?: Error|null) => { assert.strictEqual(expectedError, err); done(); }); @@ -1026,19 +1060,21 @@ describe('PubSub', () => { it('should call client method with correct options', done => { const fakeClient = {}; // tslint:disable-next-line no-any - (fakeClient as any).fakeMethod = (reqOpts, gaxOpts) => { + (fakeClient as any).fakeMethod = (reqOpts: any, gaxOpts: CallOptions) => { assert.deepStrictEqual(CONFIG.reqOpts, reqOpts); assert.deepStrictEqual(CONFIG.gaxOpts, gaxOpts); done(); }; - pubsub.getClient_ = (config, callback) => { - callback(null, fakeClient); - }; + pubsub.getClient_ = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, fakeClient); + }; pubsub.request(CONFIG, assert.ifError); }); it('should replace the project id token on reqOpts', done => { - pjyOverride = (reqOpts, projectId) => { + // tslint:disable-next-line no-any + pjyOverride = (reqOpts: any, projectId: string) => { assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); assert.strictEqual(projectId, PROJECT_ID); done(); @@ -1051,16 +1087,14 @@ describe('PubSub', () => { const FAKE_CLIENT_INSTANCE = class {}; const CONFIG = { client: 'FakeClient', - }; + } as {} as pubsubTypes.GetClientConfig; beforeEach(() => { - pubsub.auth = { - getProjectId: util.noop, - }; + sandbox.stub(pubsub, 'auth').value({getProjectId: () => util.noop}); v1ClientOverrides.FakeClient = FAKE_CLIENT_INSTANCE; }); - + afterEach(() => sandbox.restore()); describe('project ID', () => { beforeEach(() => { delete pubsub.projectId; @@ -1068,12 +1102,15 @@ describe('PubSub', () => { }); it('should get and cache the project ID', done => { - pubsub.auth.getProjectId = callback => { - assert.strictEqual(typeof callback, 'function'); - callback(null, PROJECT_ID); - }; + sandbox.stub(pubsub, 'auth').value({ + getProjectId: (callback: Function) => { + assert.strictEqual(typeof callback, 'function'); + callback(null, PROJECT_ID); + } + }); + - pubsub.getClient_(CONFIG, err => { + pubsub.getClient_(CONFIG, (err?: Error|null) => { assert.ifError(err); assert.strictEqual(pubsub.projectId, PROJECT_ID); assert.strictEqual(pubsub.options.projectId, PROJECT_ID); @@ -1084,21 +1121,23 @@ describe('PubSub', () => { it('should get the project ID if placeholder', done => { pubsub.projectId = '{{projectId}}'; - pubsub.auth.getProjectId = () => { - done(); - }; + sandbox.stub(pubsub, 'auth').value({ + getProjectId: () => { + done(); + } + }); pubsub.getClient_(CONFIG, assert.ifError); }); it('should return errors to the callback', done => { const error = new Error('err'); + sandbox.stub(pubsub.auth, 'getProjectId') + .callsFake((callback: Function) => { + callback(error); + }); - pubsub.auth.getProjectId = callback => { - callback(error); - }; - - pubsub.getClient_(CONFIG, err => { + pubsub.getClient_(CONFIG, (err?: Error|null) => { assert.strictEqual(err, error); done(); }); @@ -1136,11 +1175,11 @@ describe('PubSub', () => { return FAKE_CLIENT_INSTANCE; }; - pubsub.getClient_(CONFIG, err => { + pubsub.getClient_(CONFIG, (err?: Error|null) => { assert.ifError(err); assert.strictEqual(pubsub.api.FakeClient, FAKE_CLIENT_INSTANCE); - pubsub.getClient_(CONFIG, err => { + pubsub.getClient_(CONFIG, (err?: Error|null) => { assert.ifError(err); assert.strictEqual(numTimesFakeClientInstantiated, 1); done(); @@ -1149,13 +1188,14 @@ describe('PubSub', () => { }); it('should return the correct client', done => { - // tslint:disable-next-line only-arrow-functions - v1ClientOverrides.FakeClient = function(options) { + // tslint:disable-next-line only-arrow-functions no-any + v1ClientOverrides.FakeClient = function( + options: pubsubTypes.ClientConfig) { assert.strictEqual(options, pubsub.options); return FAKE_CLIENT_INSTANCE; }; - pubsub.getClient_(CONFIG, (err, client) => { + pubsub.getClient_(CONFIG, (err?: Error|null, client?: gax.ClientStub) => { assert.ifError(err); assert.strictEqual(client, FAKE_CLIENT_INSTANCE); done(); @@ -1169,24 +1209,26 @@ describe('PubSub', () => { method: 'fakeMethod', reqOpts: {a: 'a'}, gaxOpts: {}, - }; + } as pubsubTypes.RequestConfig; const FAKE_CLIENT_INSTANCE = { [CONFIG.method]: util.noop, }; beforeEach(() => { - pjyOverride = reqOpts => { + // tslint:disable-next-line no-any + pjyOverride = (reqOpts: any) => { return reqOpts; }; - pubsub.getClient_ = (config, callback) => { - callback(null, FAKE_CLIENT_INSTANCE); - }; + pubsub.getClient_ = + (config: pubsubTypes.RequestConfig, callback: Function) => { + callback(null, FAKE_CLIENT_INSTANCE); + }; }); - + afterEach(() => sandbox.restore()); it('should get the client', done => { - pubsub.getClient_ = config => { + pubsub.getClient_ = (config: pubsubTypes.GetClientConfig) => { assert.strictEqual(config, CONFIG); done(); }; @@ -1197,18 +1239,20 @@ describe('PubSub', () => { it('should return error from getting the client', done => { const error = new Error('Error.'); - pubsub.getClient_ = (config, callback) => { - callback(error); - }; + pubsub.getClient_ = + (config: pubsubTypes.GetClientConfig, callback: Function) => { + callback(error); + }; - pubsub.request(CONFIG, err => { + pubsub.request(CONFIG, (err?: Error|null) => { assert.strictEqual(err, error); done(); }); }); it('should replace the project id token on reqOpts', done => { - pjyOverride = (reqOpts, projectId) => { + // tslint:disable-next-line no-any + pjyOverride = (reqOpts: any, projectId: string) => { assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); assert.strictEqual(projectId, PROJECT_ID); done(); @@ -1223,7 +1267,7 @@ describe('PubSub', () => { method: 'fakeMethod', reqOpts: {a: 'a'}, gaxOpts: {}, - }; + } as {} as pubsubTypes.RequestConfig; const replacedReqOpts = {}; @@ -1232,32 +1276,28 @@ describe('PubSub', () => { }; const fakeClient = { - fakeMethod(reqOpts, gaxOpts, callback) { + // tslint:disable-next-line no-any + fakeMethod(reqOpts: any, gaxOpts: CallOptions, callback: Function) { assert.strictEqual(reqOpts, replacedReqOpts); assert.strictEqual(gaxOpts, CONFIG.gaxOpts); callback(); // done() }, }; - pubsub.getClient_ = (config, callback) => { - callback(null, fakeClient); - }; + pubsub.getClient_ = + (config: pubsubTypes.GetClientConfig, callback: Function) => { + callback(null, fakeClient); + }; pubsub.request(CONFIG, done); }); }); describe('snapshot', () => { - it('should throw if a name is not provided', () => { - assert.throws(() => { - pubsub.snapshot(); - }, /You must supply a valid name for the snapshot\./); - }); - it('should return a Snapshot object', () => { const SNAPSHOT_NAME = 'new-snapshot'; const snapshot = pubsub.snapshot(SNAPSHOT_NAME); - const args = snapshot.calledWith_; + const args = (snapshot as {} as FakeSnapshot).calledWith_; assert(snapshot instanceof FakeSnapshot); assert.strictEqual(args[0], pubsub); @@ -1278,7 +1318,8 @@ describe('PubSub', () => { it('should pass specified name to the Subscription', done => { // tslint:disable-next-line only-arrow-functions - subscriptionOverride = function(pubsub, name) { + subscriptionOverride = function( + pubsub: pubsubTypes.PubSub, name: string) { assert.strictEqual(name, SUB_NAME); done(); }; @@ -1287,27 +1328,17 @@ describe('PubSub', () => { it('should honor settings', done => { // tslint:disable-next-line only-arrow-functions - subscriptionOverride = function(pubsub, name, options) { + subscriptionOverride = function( + pubsub: pubsubTypes.PubSub, name: string, + options: pubsubTypes.CreateSubscriptionOptions) { assert.strictEqual(options, CONFIG); done(); }; pubsub.subscription(SUB_NAME, CONFIG); }); - - it('should throw if a name is not provided', () => { - assert.throws(() => { - return pubsub.subscription(); - }, /A name must be specified for a subscription\./); - }); }); describe('topic', () => { - it('should throw if a name is not provided', () => { - assert.throws(() => { - pubsub.topic(); - }, /A name must be specified for a topic\./); - }); - it('should return a Topic object', () => { assert(pubsub.topic('new-topic') instanceof FakeTopic); }); @@ -1317,7 +1348,7 @@ describe('PubSub', () => { const fakeOptions = {}; const topic = pubsub.topic(fakeName, fakeOptions); - const [ps, name, options] = topic.calledWith_; + const [ps, name, options] = (topic as {} as FakeTopic).calledWith_; assert.strictEqual(ps, pubsub); assert.strictEqual(name, fakeName);