|
1 |
| -import {expect, test} from '@jest/globals' |
| 1 | +import {expect, describe, test, beforeAll} from '@jest/globals' |
2 | 2 | import {getStatsDiff} from '../src/get-stats-diff'
|
3 | 3 | import {getChunkModuleDiff} from '../src/get-chunk-module-diff'
|
4 | 4 | import {
|
5 | 5 | printAssetTablesByGroup,
|
6 | 6 | printChunkModulesTable,
|
7 | 7 | printTotalAssetTable
|
8 | 8 | } from '../src/print-markdown'
|
9 |
| -import {AssetDiff} from '../src/types' |
| 9 | +import { |
| 10 | + AssetDiff, |
| 11 | + DescribeAssetsOptions, |
| 12 | + DescribeAssetsSection, |
| 13 | + WebpackStatsDiff, |
| 14 | + describeAssetsSections |
| 15 | +} from '../src/types' |
10 | 16 | import {readFile} from 'node:fs/promises'
|
11 | 17 | import {resolve} from 'node:path'
|
12 | 18 | import {StatsCompilation} from 'webpack'
|
| 19 | +import {getDescribeAssetsOptions} from '../src/main' |
| 20 | +import {fail} from 'node:assert' |
13 | 21 |
|
14 | 22 | async function readJsonFile(path: string): Promise<StatsCompilation> {
|
15 | 23 | const data = await readFile(resolve(__dirname, path), 'utf8')
|
@@ -121,3 +129,120 @@ test('does not display module information when it does not exist', async () => {
|
121 | 129 |
|
122 | 130 | expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
|
123 | 131 | })
|
| 132 | + |
| 133 | +describe('printAssetTablesByGroup describes asset sections as requested', () => { |
| 134 | + // generate all combinations of sections |
| 135 | + const cases: DescribeAssetsOptions[] = [] |
| 136 | + for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) { |
| 137 | + const options = {} as DescribeAssetsOptions |
| 138 | + for (let n = 0; n < describeAssetsSections.length; n++) { |
| 139 | + if ((i >> n) & 1) { |
| 140 | + options[describeAssetsSections[n]] = true |
| 141 | + } else { |
| 142 | + options[describeAssetsSections[n]] = false |
| 143 | + } |
| 144 | + } |
| 145 | + cases.push(options) |
| 146 | + } |
| 147 | + |
| 148 | + let statsDiff: WebpackStatsDiff |
| 149 | + beforeAll(async () => { |
| 150 | + statsDiff = getStatsDiff( |
| 151 | + await readJsonFile('./__mocks__/old-stats-assets.json'), |
| 152 | + await readJsonFile('./__mocks__/new-stats-assets.json') |
| 153 | + ) |
| 154 | + }) |
| 155 | + |
| 156 | + test.each(cases)( |
| 157 | + 'printAssetTablesByGroup: %j', |
| 158 | + (options: DescribeAssetsOptions) => { |
| 159 | + const assetTables = printAssetTablesByGroup(statsDiff, options) |
| 160 | + for (const [section, included] of Object.entries(options)) { |
| 161 | + const sectionHeader = `**${section[0].toUpperCase()}${section.slice( |
| 162 | + 1 |
| 163 | + )}**` |
| 164 | + if (included) { |
| 165 | + expect(assetTables).toContain(sectionHeader) |
| 166 | + } else { |
| 167 | + expect(assetTables).not.toContain(sectionHeader) |
| 168 | + } |
| 169 | + } |
| 170 | + if (Object.entries(options).every(([, included]) => included === false)) { |
| 171 | + expect(assetTables).toBe('') |
| 172 | + } |
| 173 | + } |
| 174 | + ) |
| 175 | +}) |
| 176 | + |
| 177 | +describe('getDescribeAssetsOptions', () => { |
| 178 | + test(`getDescribeAssetsOptions: "all"`, () => { |
| 179 | + const generatedOptions = getDescribeAssetsOptions('all') |
| 180 | + for (const section of describeAssetsSections) { |
| 181 | + expect(generatedOptions[section]).toBe(true) |
| 182 | + } |
| 183 | + }) |
| 184 | + |
| 185 | + test(`getDescribeAssetsOptions: "none"`, () => { |
| 186 | + const generatedOptions = getDescribeAssetsOptions('none') |
| 187 | + for (const section of describeAssetsSections) { |
| 188 | + expect(generatedOptions[section]).toBe(false) |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + test(`getDescribeAssetsOptions: "changed-only"`, () => { |
| 193 | + const generatedOptions = getDescribeAssetsOptions('changed-only') |
| 194 | + for (const section of describeAssetsSections) { |
| 195 | + if (section === 'unchanged') { |
| 196 | + expect(generatedOptions[section]).toBe(false) |
| 197 | + } else { |
| 198 | + expect(generatedOptions[section]).toBe(true) |
| 199 | + } |
| 200 | + } |
| 201 | + }) |
| 202 | + |
| 203 | + test('getDescribeAssetsOptions: handles keyword with spaces', () => { |
| 204 | + const generatedOptions = getDescribeAssetsOptions(' all ') |
| 205 | + for (const section of describeAssetsSections) { |
| 206 | + expect(generatedOptions[section]).toBe(true) |
| 207 | + } |
| 208 | + }) |
| 209 | + |
| 210 | + test('getDescribeAssetsOptions: unsupported option throws', () => { |
| 211 | + expect(() => getDescribeAssetsOptions('unsupported options')).toThrow() |
| 212 | + }) |
| 213 | + |
| 214 | + // generate all combinations of sections as string |
| 215 | + const cases: string[] = [] |
| 216 | + for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) { |
| 217 | + const options: string[] = [] |
| 218 | + for (let n = 0; n < describeAssetsSections.length; n++) { |
| 219 | + if ((i >> n) & 1) { |
| 220 | + options.push(describeAssetsSections[n]) |
| 221 | + } |
| 222 | + } |
| 223 | + if (options.length > 0) { |
| 224 | + cases.push(options.join(' ')) |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + test.each(cases)(`getDescribeAssetsOptions: %j`, (optionString: string) => { |
| 229 | + const generatedOptions = getDescribeAssetsOptions(optionString) |
| 230 | + const providedOptions = optionString.split(' ') |
| 231 | + for (const section of providedOptions) { |
| 232 | + expect(generatedOptions[section as DescribeAssetsSection]).toBe(true) |
| 233 | + } |
| 234 | + for (const section of describeAssetsSections.filter( |
| 235 | + s => !providedOptions.includes(s) |
| 236 | + )) { |
| 237 | + expect(generatedOptions[section]).toBe(false) |
| 238 | + } |
| 239 | + }) |
| 240 | + |
| 241 | + test('getDescribeAssetsOptions: handles sections with spaces', () => { |
| 242 | + const optionString = ' added removed bigger' |
| 243 | + const generatedOptions = getDescribeAssetsOptions(optionString) |
| 244 | + for (const section of describeAssetsSections) { |
| 245 | + expect(generatedOptions[section]).toBe(optionString.includes(section)) |
| 246 | + } |
| 247 | + }) |
| 248 | +}) |
0 commit comments