-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/utils/src/webpack/extract/__tests__/assets-new-insight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { extractAssetsNewInsight } from '../assets-new-insight'; | ||
|
||
describe('Webpack/extracts/extractAssetsNewInsight', () => { | ||
test('should return null when there is no baseline', () => { | ||
const actual = extractAssetsNewInsight(null, { | ||
metrics: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(actual).toEqual(null); | ||
}); | ||
|
||
test('should return null when there are no new assets', () => { | ||
const actual = extractAssetsNewInsight( | ||
null, | ||
{ | ||
metrics: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
metrics: { | ||
webpack: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as any, | ||
); | ||
|
||
expect(actual).toEqual(null); | ||
}); | ||
|
||
test('should return insights when there is a new assets', () => { | ||
const actual = extractAssetsNewInsight( | ||
null, | ||
{ | ||
metrics: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
'./main.css': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
metrics: { | ||
webpack: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as any, | ||
); | ||
|
||
expect(actual).toEqual({ | ||
insights: { | ||
newAssets: { | ||
type: 'warning', | ||
data: { | ||
text: 'Bundle introduced one new asset', | ||
assets: ['./main.css'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should return insights when there are new packages', () => { | ||
const actual = extractAssetsNewInsight( | ||
null, | ||
{ | ||
metrics: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
'./main.css': { | ||
size: 100, | ||
}, | ||
'./logo.png': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
metrics: { | ||
webpack: { | ||
assets: { | ||
'./main.js': { | ||
size: 100, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as any, | ||
); | ||
|
||
expect(actual).toEqual({ | ||
insights: { | ||
newAssets: { | ||
type: 'warning', | ||
data: { | ||
text: 'Bundle introduced 2 new assets', | ||
assets: ['./main.css', './logo.png'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { InsightType, Job, JobInsights } from '../../constants'; | ||
import { Assets } from '../types'; | ||
|
||
const getAssetNames = (assets: Assets) => new Set(Object.keys(assets)); | ||
|
||
interface AssetsNewInsight { | ||
insights?: { | ||
newAssets: JobInsights['webpack']['newAssets']; | ||
}; | ||
} | ||
|
||
export const extractAssetsNewInsight = ( | ||
_: any, | ||
currentExtractedData: any, | ||
baselineJob?: Job, | ||
): AssetsNewInsight | null => { | ||
const currentAssets = currentExtractedData.metrics?.assets as Assets; | ||
const baselineAssets = baselineJob?.metrics?.webpack?.assets as Assets; | ||
|
||
if (!baselineAssets) { | ||
return null; | ||
} | ||
|
||
const currentAssetNames = getAssetNames(currentAssets); | ||
const baselineAssetNames = getAssetNames(baselineAssets); | ||
|
||
const newAssets: Array<string> = []; | ||
currentAssetNames.forEach((assetName) => { | ||
if (!baselineAssetNames.has(assetName)) { | ||
newAssets.push(assetName); | ||
} | ||
}); | ||
|
||
if (newAssets.length === 0) { | ||
return null; | ||
} | ||
|
||
const text = `Bundle introduced ${newAssets.length > 1 ? `${newAssets.length} new assets` : 'one new asset' }`; | ||
|
||
return { | ||
insights: { | ||
newAssets: { | ||
type: InsightType.WARNING, | ||
data: { | ||
text, | ||
assets: newAssets, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters