Skip to content

Commit

Permalink
Merge pull request #35 from ysa23/fix/metrics-init-no-reporters
Browse files Browse the repository at this point in the history
Fix: allow an empty reporters array
  • Loading branch information
ysa23 authored May 30, 2022
2 parents 3a8755c + e98ef35 commit 29ccbb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"datadog",
"DogStatsD"
],
"version": "0.14.1",
"version": "0.14.2",
"repository": {
"type": "git",
"url": "https://github.com/ysa23/metrics-reporter"
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { validate } = require('./validation/validator');
const { Space } = require('./space');

function Metrics({ reporters, tags: defaultTags, errback }) {
if (!reporters || !Array.isArray(reporters) || reporters.length === 0) throw new TypeError('reporters is missing or empty');
if (!reporters || !Array.isArray(reporters)) throw new TypeError('reporters is missing');
if (defaultTags && (Array.isArray(defaultTags) || typeof defaultTags !== 'object')) throw new TypeError('tags should be an object (key-value)');

validate({
Expand Down
11 changes: 9 additions & 2 deletions src/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ describe('Metrics', () => {
it.each([
['undefined', undefined],
['null', null],
['empty array', []],
['number', 1],
['string', 'no strings on me'],
['object', { key: 'value' }],
])('should throw an error when reporters is %s', (title, reporters) => {
expect(() => new Metrics({ reporters }))
.toThrow('reporters is missing or empty');
.toThrow('reporters is missing');
});

it.each([
Expand All @@ -65,6 +64,14 @@ describe('Metrics', () => {
.toThrow(TypeError);
});

it('should not throw when reporters is an empty array', () => {
const reporters = [];
const errback = jest.fn();

expect(() => new Metrics({ reporters, errback }))
.not.toThrow();
});

it('should create a metrics object', () => {
const reporters = [new InMemoryReporter({ buffer: [] })];
const errback = jest.fn();
Expand Down

0 comments on commit 29ccbb8

Please sign in to comment.