Skip to content

Commit

Permalink
fix(utils): getMetricType - prevent partial match
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Sep 23, 2019
1 parent 67fdd93 commit 8bcc57c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
31 changes: 25 additions & 6 deletions packages/utils/src/metrics/__tests__/get-metric-type.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { omit } from 'lodash';

import { METRIC_TYPE_FILE_SIZE } from '../../config/metrics';
import { getMetricType } from '../get-metric-type';

test('getMetric', () => {
expect(omit(getMetricType('webpack.assets.totalSizeByTypeALL'), ['formatter'])).toEqual(omit({
label: 'Total Size',
type: 'METRIC_TYPE_FILE_SIZE',
biggerIsBetter: false,
}, ['formatter']));
describe('getMetricType', () => {
test('should return metric', () => {
expect(omit(getMetricType('webpack.assets.totalSizeByTypeALL'), ['formatter'])).toEqual(omit({
label: 'Total Size',
type: 'METRIC_TYPE_FILE_SIZE',
biggerIsBetter: false,
}, ['formatter']));
});

test('should return fallback metric for partial matches', () => {
expect(omit(getMetricType('webpack', METRIC_TYPE_FILE_SIZE), ['formatter'])).toEqual(omit({
label: 'webpack',
type: 'METRIC_TYPE_FILE_SIZE',
biggerIsBetter: false,
}, ['formatter']));
});

test('should return fallback metric', () => {
expect(omit(getMetricType('/assets/main.js', METRIC_TYPE_FILE_SIZE), ['formatter'])).toEqual(omit({
label: '/assets/main.js',
type: 'METRIC_TYPE_FILE_SIZE',
biggerIsBetter: false,
}, ['formatter']));
});
});
19 changes: 13 additions & 6 deletions packages/utils/src/metrics/get-metric-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import { get } from 'lodash';
import METRICS, { METRIC_TYPE_NUMERIC, METRIC_TYPES } from '../config/metrics';

export const getMetricType = (key, type) => {
const metric = get(METRICS, key, {
label: key,
type: type || METRIC_TYPE_NUMERIC,
});
const metric = get(METRICS, key);

if (metric && metric.type) {
return {
...METRIC_TYPES[metric.type],
...metric,
};
}

const resolvedType = type || METRIC_TYPE_NUMERIC;

return {
...METRIC_TYPES[metric.type],
...metric,
...METRIC_TYPES[resolvedType],
type: resolvedType,
label: key,
};
};

0 comments on commit 8bcc57c

Please sign in to comment.