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

Commit

Permalink
feat: add get() function (#709)
Browse files Browse the repository at this point in the history
The `get()` function is used to access an agent if it
has been started.
  • Loading branch information
DominicKramer authored Jun 5, 2019
1 parent e2f52b1 commit 003c662
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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'
);
});
});

0 comments on commit 003c662

Please sign in to comment.