Skip to content

Commit

Permalink
fix(instrumentation-node): refactor methods to private with ts-expect…
Browse files Browse the repository at this point in the history
…-error within tests
  • Loading branch information
adambartholomew committed May 13, 2021
1 parent 1137b9c commit 159eb90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class InstrumentationBase<T = any>
}
}

protected _onRequire<T>(
private _onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
name: string,
Expand Down Expand Up @@ -102,15 +102,15 @@ export abstract class InstrumentationBase<T = any>
return exports;
}

protected getModuleVersion(directory: string): string {
private getModuleVersion(directory: string): string {
const modulePackage = this.findModulePackage(directory);
if (!modulePackage) {
return '0.0.0';
}
return modulePackage.version;
}

protected findModulePackage(directory: string): any {
private findModulePackage(directory: string): unknown {
const parentDirectory = path.dirname(directory)
if (directory === parentDirectory) { return; }
let contents = null
Expand All @@ -122,7 +122,7 @@ export abstract class InstrumentationBase<T = any>
return this.findModulePackage(parentDirectory)
}

protected loadModulePackage(directory: string): any {
private loadModulePackage(directory: string): unknown {
return require(path.join(directory, 'package.json'))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ class TestInstrumentation extends InstrumentationBase {
enable() {}
disable() {}
init() {}
_onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
name: string,
baseDir?: string
): T {
return super._onRequire(module, exports, name, baseDir)
}
getModuleVersion(directory: string): string {
return super.getModuleVersion(directory)
}
findModulePackage(directory: string): any {
return super.findModulePackage(directory)
}
loadModulePackage(directory: string): any {
return super.loadModulePackage(directory)
}
}

describe('BaseInstrumentation', () => {
Expand Down Expand Up @@ -78,11 +61,15 @@ describe('BaseInstrumentation', () => {
describe('_onRequire', () => {
it('loads package.json recursively returning 0.0.0 when undiscovered', () => {
const instrumentation = new TestInstrumentation()
// @ts-expect-error access internal property for testing
const versionSpy = sinon.spy(instrumentation, 'getModuleVersion');
// @ts-expect-error access internal property for testing
const findSpy = sinon.spy(instrumentation, 'findModulePackage');
// @ts-expect-error access internal property for testing
const loadSpy = sinon.spy(instrumentation, 'loadModulePackage')

const moduleDefinition = {} as InstrumentationModuleDefinition<unknown>
// @ts-expect-error access internal property for testing
instrumentation._onRequire<unknown>(
moduleDefinition,
{} as unknown,
Expand All @@ -100,8 +87,11 @@ describe('BaseInstrumentation', () => {

it('loads package.json recursively returning version when discovered', () => {
const instrumentation = new TestInstrumentation()
// @ts-expect-error access internal property for testing
const versionSpy = sinon.spy(instrumentation, 'getModuleVersion');
// @ts-expect-error access internal property for testing
const findSpy = sinon.spy(instrumentation, 'findModulePackage');
// @ts-expect-error access internal property for testing
const loadStub = sinon.stub(instrumentation, 'loadModulePackage')

loadStub.withArgs('/foo/bar').returns({
Expand All @@ -110,6 +100,7 @@ describe('BaseInstrumentation', () => {
})

const moduleDefinition = {} as InstrumentationModuleDefinition<unknown>
// @ts-expect-error access internal property for testing
instrumentation._onRequire<unknown>(
moduleDefinition,
{} as unknown,
Expand Down

0 comments on commit 159eb90

Please sign in to comment.