Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

feat: add get() function #709

Merged
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
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ function mergeConfigs<T>(options: T & {debug?: T}): T {
delete result.debug;
return Object.assign(result, options.debug);
}

/* Used to access the agent if it has been started. Returns the agent
* if the agent has been started. Otherwise, `undefined` is returned.
*/
export function get(): Debuglet | undefined {
return debuglet;
}
28 changes: 27 additions & 1 deletion test/test-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/

import * as assert from 'assert';
const m: NodeModule & {start: Function} = require('../..');
const m: NodeModule & {
start: Function;
get: () => Debuglet | undefined;
} = require('../..');
import * as nock from 'nock';
import * as nocks from './nocks';
import {Debuglet} from '../src/agent/debuglet';

nock.disableNetConnect();

Expand All @@ -39,4 +43,26 @@ describe('Debug module', () => {
m.start();
});
});

it('should return the agent via the get() method', () => {
const agent = m.get();
assert(agent, 'Expected to get the started agent');
assert.strictEqual(agent!.config.projectId, '0');
});
});

describe('Debug module without start() called', () => {
it('get() should return `undefined`', () => {
delete require.cache[require.resolve('../..')];
const m: NodeModule & {
start: Function;
get: () => Debuglet | undefined;
} = require('../..');
const agent = m.get();
assert.strictEqual(
agent,
undefined,
'Expected `undefined` since the agent was not started'
);
});
});