Repository navigation
test: replace proxyquire with sinon in gax, common, error-reporting, translate #9638
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ import * as pfy from '@google-cloud/promisify'; | |
| import * as assert from 'assert'; | ||
| import {after, afterEach, before, beforeEach, describe, it} from 'mocha'; | ||
| import * as extend from 'extend'; | ||
| import * as proxyquire from 'proxyquire'; | ||
| import * as Module from 'module'; | ||
| import * as r from 'request'; | ||
|
|
||
| import * as orig from '../src'; | ||
|
|
@@ -44,7 +44,7 @@ const fakeUtil = extend({}, util, { | |
| makeRequest( | ||
| opts: DecorateRequestOptions, | ||
| cfg: MakeRequestConfig, | ||
| cb: BodyResponseCallback | ||
| cb: BodyResponseCallback, | ||
| ) { | ||
| if (makeRequestOverride) { | ||
| return makeRequestOverride(opts, cfg, cb); | ||
|
|
@@ -74,13 +74,30 @@ describe('Translate v2', () => { | |
| let translate: any; | ||
|
|
||
| before(() => { | ||
| Translate = proxyquire('../src/v2', { | ||
| const stubs: Record<string, unknown> = { | ||
| '@google-cloud/common': { | ||
| util: fakeUtil, | ||
| Service: FakeService, | ||
| }, | ||
| '@google-cloud/promisify': fakePromisify, | ||
| }).Translate; | ||
| }; | ||
| const mod = Module as unknown as {prototype: {require: NodeRequire}}; | ||
| const origRequire = mod.prototype.require; | ||
| const v2Path = require.resolve('../src/v2'); | ||
| const cached = require.cache[v2Path]; | ||
| delete require.cache[v2Path]; | ||
| mod.prototype.require = Object.assign(function ( | ||
| this: NodeModule, | ||
| id: string, | ||
| ) { | ||
| return id in stubs ? stubs[id] : origRequire.call(this, id); | ||
| }, origRequire); | ||
| try { | ||
| Translate = require('../src/v2').Translate; | ||
| } finally { | ||
| mod.prototype.require = origRequire; | ||
| if (cached) require.cache[v2Path] = cached; | ||
| } | ||
|
Comment on lines
+77
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overriding const common = require('@google-cloud/common');
const origUtil = common.util;
const origService = common.Service;
const origPromisifyAll = pfy.promisifyAll;
common.util = fakeUtil;
common.Service = FakeService;
pfy.promisifyAll = fakePromisify;
const v2Path = require.resolve('../src/v2');
const cached = require.cache[v2Path];
delete require.cache[v2Path];
try {
Translate = require('../src/v2').Translate;
} finally {
common.util = origUtil;
common.Service = origService;
pfy.promisifyAll = origPromisifyAll;
if (cached) require.cache[v2Path] = cached;
} |
||
| }); | ||
|
|
||
| beforeEach(() => { | ||
|
|
@@ -266,7 +283,7 @@ describe('Translate v2', () => { | |
| assert.strictEqual(apiResponse_, apiResponse); | ||
| assert.deepStrictEqual(apiResponse_, originalApiResponse); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -314,7 +331,7 @@ describe('Translate v2', () => { | |
| assert.strictEqual(languages, null); | ||
| assert.strictEqual(apiResponse_, apiResponse); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -359,7 +376,7 @@ describe('Translate v2', () => { | |
| assert.deepStrictEqual(languages, expectedResults); | ||
| assert.strictEqual(apiResponse_, apiResponse); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -407,7 +424,7 @@ describe('Translate v2', () => { | |
| from: SOURCE_LANG_CODE, | ||
| to: TARGET_LANG_CODE, | ||
| }, | ||
| assert.ifError | ||
| assert.ifError, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -496,7 +513,7 @@ describe('Translate v2', () => { | |
| assert.strictEqual(translations, null); | ||
| assert.strictEqual(resp, apiResponse); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -531,7 +548,7 @@ describe('Translate v2', () => { | |
| assert.deepStrictEqual(translations, expectedResults); | ||
| assert.strictEqual(resp, apiResponse); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
|
|
@@ -552,7 +569,7 @@ describe('Translate v2', () => { | |
| assert.ifError(err); | ||
| assert.deepStrictEqual(translations, [expectedResults]); | ||
| done(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
@@ -584,7 +601,7 @@ describe('Translate v2', () => { | |
|
|
||
| FakeService.prototype.request = ( | ||
| reqOpts: r.Options, | ||
| callback: Function | ||
| callback: Function, | ||
| ) => { | ||
| assert.strictEqual(reqOpts, fakeOptions); | ||
| callback(); | ||
|
|
@@ -640,7 +657,7 @@ describe('Translate v2', () => { | |
| makeRequestOverride = ( | ||
| reqOpts: r.Options, | ||
| options: {}, | ||
| callback: Function | ||
| callback: Function, | ||
| ) => { | ||
| assert.deepStrictEqual(reqOpts, expectedReqOpts); | ||
| assert.strictEqual(options, translate.options); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of stubbing the prototype of
GoogleAuth(which mutates the class prototype globally for all instances), you can stub thegetClientmethod directly on theauthinstance. This is safer, avoids prototype pollution, and is more idiomatic.