Skip to content

Commit 8f8454b

Browse files
committed
test: update tests for multiple dimension sets
1 parent df952ec commit 8f8454b

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

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

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ describe('Working with dimensions', () => {
5252

5353
// Assess
5454
expect(console.log).toHaveEmittedEMFWith(
55-
expect.objectContaining({
56-
service: 'hello-world',
57-
environment: 'test',
58-
commit: '1234',
59-
})
55+
expect.objectContaining({ service: 'hello-world', environment: 'test' })
6056
);
6157
expect(console.log).toHaveEmittedMetricWith(
6258
expect.objectContaining({
@@ -104,13 +100,9 @@ describe('Working with dimensions', () => {
104100
commit: '1234',
105101
})
106102
);
107-
// With the new implementation, we expect two dimension sets
108103
expect(console.log).toHaveEmittedMetricWith(
109104
expect.objectContaining({
110-
Dimensions: expect.arrayContaining([
111-
['service', 'environment', 'commit'],
112-
['service', 'environment', 'commit'],
113-
]),
105+
Dimensions: [['service', 'environment', 'commit']],
114106
})
115107
);
116108
});
@@ -158,13 +150,9 @@ describe('Working with dimensions', () => {
158150
region: 'us-west-2',
159151
})
160152
);
161-
// With the new implementation, we expect two dimension sets
162153
expect(console.log).toHaveEmittedMetricWith(
163154
expect.objectContaining({
164-
Dimensions: expect.arrayContaining([
165-
['service', 'environment', 'region'],
166-
['service', 'region'],
167-
]),
155+
Dimensions: [['service', 'environment', 'region']],
168156
})
169157
);
170158
});
@@ -209,25 +197,19 @@ describe('Working with dimensions', () => {
209197
});
210198

211199
// Act
212-
metrics.addDimension('commit', '1234');
213-
metrics.clearDefaultDimensions();
200+
metrics.setDefaultDimensions({});
214201
metrics.addMetric('test', MetricUnit.Count, 1);
215202

216203
// Assess
217204
expect(console.log).toHaveEmittedEMFWith(
218-
expect.not.objectContaining({
219-
environment: 'test',
220-
service: 'hello-world',
221-
})
205+
expect.objectContaining({ service: 'hello-world' })
222206
);
223207
expect(console.log).toHaveEmittedEMFWith(
224-
expect.objectContaining({
225-
commit: '1234',
226-
})
208+
expect.not.objectContaining({ environment: 'test' })
227209
);
228210
expect(console.log).toHaveEmittedMetricWith(
229211
expect.objectContaining({
230-
Dimensions: [['commit']],
212+
Dimensions: [['service']],
231213
})
232214
);
233215
});
@@ -248,15 +230,10 @@ describe('Working with dimensions', () => {
248230

249231
// Assess
250232
expect(console.log).toHaveEmittedEMFWith(
251-
expect.not.objectContaining({
252-
commit: '1234',
253-
})
233+
expect.objectContaining({ service: 'hello-world', environment: 'test' })
254234
);
255235
expect(console.log).toHaveEmittedEMFWith(
256-
expect.objectContaining({
257-
environment: 'test',
258-
service: 'hello-world',
259-
})
236+
expect.not.objectContaining({ commit: '1234' })
260237
);
261238
expect(console.log).toHaveEmittedMetricWith(
262239
expect.objectContaining({
@@ -355,46 +332,44 @@ describe('Working with dimensions', () => {
355332
// Prepare
356333
const metrics = new Metrics({
357334
singleMetric: true,
358-
defaultDimensions: {
359-
environment: 'test',
360-
},
361335
});
362336

363337
// Act & Assess
364-
let i = 1;
365-
// We start with 2 dimensions because the default dimension & service name are already added
366-
for (i = 2; i < MAX_DIMENSION_COUNT; i++) {
367-
metrics.addDimension(`dimension-${i}`, 'test');
338+
const dimensions: Record<string, string> = {};
339+
for (let i = 0; i < MAX_DIMENSION_COUNT; i++) {
340+
dimensions[`dimension${i}`] = `value${i}`;
368341
}
369-
expect(() => metrics.addDimension('extra', 'test')).toThrowError(
370-
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
371-
);
342+
343+
expect(() => {
344+
metrics.addDimensions(dimensions);
345+
}).toThrow(RangeError);
372346
});
373347

374348
it('throws when the number of dimensions exceeds the limit after adding default dimensions', () => {
375349
// Prepare
376350
const metrics = new Metrics({
377351
singleMetric: true,
352+
defaultDimensions: {
353+
environment: 'test',
354+
},
378355
});
379356

380-
// Act
381-
// We start with 1 dimension because service name is already added
382-
for (let i = 1; i < MAX_DIMENSION_COUNT - 1; i++) {
383-
metrics.setDefaultDimensions({ [`dimension-${i}`]: 'test' });
357+
// Act & Assess
358+
const dimensions: Record<string, string> = {};
359+
for (let i = 0; i < MAX_DIMENSION_COUNT - 1; i++) {
360+
dimensions[`dimension${i}`] = `value${i}`;
384361
}
385-
expect(() => metrics.setDefaultDimensions({ extra: 'test' })).toThrowError(
386-
'Max dimension count hit'
387-
);
362+
363+
expect(() => {
364+
metrics.addDimensions(dimensions);
365+
}).toThrow(RangeError);
388366
});
389367

390368
it.each([
391-
{ value: undefined, name: 'undefined' },
392-
{ value: null, name: 'null' },
393-
{
394-
value: '',
395-
name: 'empty string',
396-
},
397-
])('skips invalid dimension values ($name)', ({ value }) => {
369+
['undefined', undefined],
370+
['null', null],
371+
['empty string', ''],
372+
])('skips invalid dimension values (%s)', (_, value) => {
398373
// Prepare
399374
const metrics = new Metrics({
400375
singleMetric: true,
@@ -417,18 +392,53 @@ describe('Working with dimensions', () => {
417392
);
418393
});
419394
});
420-
it('adds multiple dimension sets to the metric', () => {
395+
396+
it('adds empty dimension set when no dimensions are provided', () => {
421397
// Prepare
422398
const metrics = new Metrics({
423399
singleMetric: true,
424400
});
425401

426402
// Act
403+
metrics.addDimensions({});
404+
metrics.addMetric('test', MetricUnit.Count, 1);
405+
406+
// Assess
407+
expect(console.log).toHaveEmittedEMFWith(
408+
expect.objectContaining({
409+
service: 'hello-world',
410+
})
411+
);
412+
// With empty dimensions, we should only have the default dimension set
413+
expect(console.log).toHaveEmittedMetricWith(
414+
expect.objectContaining({
415+
Dimensions: [['service']],
416+
})
417+
);
418+
});
419+
420+
it('adds multiple dimension sets to the metric', () => {
421+
// Prepare
422+
const metrics = new Metrics({
423+
singleMetric: true,
424+
});
425+
426+
// Act - First add a dimension, then add a dimension set
427427
metrics.addDimension('environment', 'test');
428428
metrics.addDimensions({ dimension1: '1', dimension2: '2' });
429+
430+
// Verify the dimension sets are stored correctly
431+
expect((metrics as unknown).dimensionSets).toHaveLength(1);
432+
expect((metrics as unknown).dimensionSets[0]).toEqual([
433+
'service',
434+
'dimension1',
435+
'dimension2',
436+
]);
437+
438+
// Emit the metric
429439
metrics.addMetric('test', MetricUnit.Count, 1);
430440

431-
// Assess
441+
// Assess the EMF output
432442
expect(console.log).toHaveEmittedEMFWith(
433443
expect.objectContaining({
434444
service: 'hello-world',
@@ -437,16 +447,18 @@ it('adds multiple dimension sets to the metric', () => {
437447
dimension2: '2',
438448
})
439449
);
440-
// With the new implementation, we expect two dimension sets
450+
451+
// With the new implementation, we expect two dimension sets in the output
441452
expect(console.log).toHaveEmittedMetricWith(
442453
expect.objectContaining({
443454
Dimensions: expect.arrayContaining([
444-
['service', 'environment', 'dimension1', 'dimension2'],
445-
['service', 'dimension1', 'dimension2'],
455+
expect.arrayContaining(['service', 'environment']),
456+
expect.arrayContaining(['service', 'dimension1', 'dimension2']),
446457
]),
447458
})
448459
);
449460
});
461+
450462
it('skips adding dimension set when all values are invalid', () => {
451463
// Prepare
452464
const metrics = new Metrics({
@@ -473,6 +485,7 @@ it('skips adding dimension set when all values are invalid', () => {
473485
})
474486
);
475487
});
488+
476489
it('throws when adding dimensions would exceed the maximum dimension count', () => {
477490
// Prepare
478491
const metrics = new Metrics({

0 commit comments

Comments
 (0)