Skip to content

Commit df952ec

Browse files
committed
test: add tests to improve coverage
1 parent 2cbeb49 commit df952ec

File tree

1 file changed

+81
-11
lines changed

1 file changed

+81
-11
lines changed

packages/metrics/tests/unit/dimensions.test.ts

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,26 @@ describe('Working with dimensions', () => {
115115
);
116116
});
117117

118-
it('adds multiple dimension sets to the metric', () => {
118+
it('adds empty dimension set when no dimensions are provided', () => {
119119
// Prepare
120120
const metrics = new Metrics({
121121
singleMetric: true,
122122
});
123123

124124
// Act
125-
metrics.addDimension('environment', 'test');
126-
metrics.addDimensions({ dimension1: '1', dimension2: '2' });
125+
metrics.addDimensions({});
127126
metrics.addMetric('test', MetricUnit.Count, 1);
128127

129128
// Assess
130129
expect(console.log).toHaveEmittedEMFWith(
131130
expect.objectContaining({
132131
service: 'hello-world',
133-
environment: 'test',
134-
dimension1: '1',
135-
dimension2: '2',
136132
})
137133
);
138-
// With the new implementation, we expect two dimension sets
134+
// With empty dimensions, we should only have the default dimension set
139135
expect(console.log).toHaveEmittedMetricWith(
140136
expect.objectContaining({
141-
Dimensions: expect.arrayContaining([
142-
['service', 'environment', 'dimension1', 'dimension2'],
143-
['service', 'dimension1', 'dimension2'],
144-
]),
137+
Dimensions: [['service']],
145138
})
146139
);
147140
});
@@ -424,3 +417,80 @@ describe('Working with dimensions', () => {
424417
);
425418
});
426419
});
420+
it('adds multiple dimension sets to the metric', () => {
421+
// Prepare
422+
const metrics = new Metrics({
423+
singleMetric: true,
424+
});
425+
426+
// Act
427+
metrics.addDimension('environment', 'test');
428+
metrics.addDimensions({ dimension1: '1', dimension2: '2' });
429+
metrics.addMetric('test', MetricUnit.Count, 1);
430+
431+
// Assess
432+
expect(console.log).toHaveEmittedEMFWith(
433+
expect.objectContaining({
434+
service: 'hello-world',
435+
environment: 'test',
436+
dimension1: '1',
437+
dimension2: '2',
438+
})
439+
);
440+
// With the new implementation, we expect two dimension sets
441+
expect(console.log).toHaveEmittedMetricWith(
442+
expect.objectContaining({
443+
Dimensions: expect.arrayContaining([
444+
['service', 'environment', 'dimension1', 'dimension2'],
445+
['service', 'dimension1', 'dimension2'],
446+
]),
447+
})
448+
);
449+
});
450+
it('skips adding dimension set when all values are invalid', () => {
451+
// Prepare
452+
const metrics = new Metrics({
453+
singleMetric: true,
454+
});
455+
456+
// Act
457+
metrics.addDimensions({
458+
dimension1: '',
459+
dimension2: null as unknown as string,
460+
});
461+
metrics.addMetric('test', MetricUnit.Count, 1);
462+
463+
// Assess
464+
expect(console.log).toHaveEmittedEMFWith(
465+
expect.objectContaining({
466+
service: 'hello-world',
467+
})
468+
);
469+
// With all invalid dimensions, we should only have the default dimension set
470+
expect(console.log).toHaveEmittedMetricWith(
471+
expect.objectContaining({
472+
Dimensions: [['service']],
473+
})
474+
);
475+
});
476+
it('throws when adding dimensions would exceed the maximum dimension count', () => {
477+
// Prepare
478+
const metrics = new Metrics({
479+
singleMetric: true,
480+
});
481+
482+
// Mock getCurrentDimensionsCount to return a value close to the limit
483+
const getCurrentDimensionsCountSpy = vi.spyOn(
484+
metrics,
485+
'getCurrentDimensionsCount'
486+
);
487+
getCurrentDimensionsCountSpy.mockReturnValue(MAX_DIMENSION_COUNT - 1);
488+
489+
// Act & Assert
490+
expect(() => {
491+
metrics.addDimensions({ oneMore: 'tooMany' });
492+
}).toThrow(RangeError);
493+
494+
// Restore the mock
495+
getCurrentDimensionsCountSpy.mockRestore();
496+
});

0 commit comments

Comments
 (0)