Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add describe-assets option #459

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ jobs:
base-stats-json-path: ./__tests__/__mocks__/old-stats-assets.json
github-token: ${{ secrets.GITHUB_TOKEN }}
title: No changes
- uses: ./
with:
current-stats-json-path: ./__tests__/__mocks__/new-stats-assets.json
base-stats-json-path: ./__tests__/__mocks__/old-stats-assets.json
github-token: ${{ secrets.GITHUB_TOKEN }}
title: 'With describe-assets: \'none\''
describe-assets: none
- uses: ./
with:
current-stats-json-path: ./__tests__/__mocks__/old-stats-assets.json
base-stats-json-path: ./__tests__/__mocks__/old-stats-assets.json
github-token: ${{ secrets.GITHUB_TOKEN }}
title: 'With describe-assets: \'changed-only\''
describe-assets: changed-only
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ This action requires the `write` permission for the [`permissions.pull-requests`
| github-token | The Github token | true | string |
| title | An optional addition to the title, which also helps key comments, useful if running more than 1 copy of this action | false | string |

| describe-assets | Option for asset description output. One of "all" (default), "changed-only", or "none". | false | string |

## Example PR Comment

https://github.com/github/webpack-bundlesize-compare-action/pull/50#issuecomment-1054919780
129 changes: 127 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import {expect, test} from '@jest/globals'
import {expect, describe, test, beforeAll} from '@jest/globals'
import {getStatsDiff} from '../src/get-stats-diff'
import {getChunkModuleDiff} from '../src/get-chunk-module-diff'
import {
printAssetTablesByGroup,
printChunkModulesTable,
printTotalAssetTable
} from '../src/print-markdown'
import {AssetDiff} from '../src/types'
import {
AssetDiff,
DescribeAssetsOptions,
DescribeAssetsSection,
WebpackStatsDiff,
describeAssetsSections
} from '../src/types'
import {readFile} from 'node:fs/promises'
import {resolve} from 'node:path'
import {StatsCompilation} from 'webpack'
import {getDescribeAssetsOptions} from '../src/main'
import {fail} from 'node:assert'

async function readJsonFile(path: string): Promise<StatsCompilation> {
const data = await readFile(resolve(__dirname, path), 'utf8')
Expand Down Expand Up @@ -121,3 +129,120 @@ test('does not display module information when it does not exist', async () => {

expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
})

describe('printAssetTablesByGroup describes asset sections as requested', () => {
// generate all combinations of sections
const cases: DescribeAssetsOptions[] = []
for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) {
const options = {} as DescribeAssetsOptions
for (let n = 0; n < describeAssetsSections.length; n++) {
if ((i >> n) & 1) {
options[describeAssetsSections[n]] = true
} else {
options[describeAssetsSections[n]] = false
}
}
cases.push(options)
}

let statsDiff: WebpackStatsDiff
beforeAll(async () => {
statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
})

Comment on lines +148 to +154
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Could we get away with just initializing this once? Can't tell right away if there's any mutation going on here or something

Suggested change
let statsDiff: WebpackStatsDiff
beforeAll(async () => {
statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
})
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't do that since describe() can't be async: jestjs/jest#2235

beforeAll does indeed only run once before all the test cases within the describe though, unlike beforeEach, so it's ugly but should be correct 👍🏽

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now you are probably wondering if we can change the let to a const. Not sure of a better way around this but lmk if you think of one!

test.each(cases)(
'printAssetTablesByGroup: %j',
(options: DescribeAssetsOptions) => {
const assetTables = printAssetTablesByGroup(statsDiff, options)
for (const [section, included] of Object.entries(options)) {
const sectionHeader = `**${section[0].toUpperCase()}${section.slice(
1
)}**`
if (included) {
expect(assetTables).toContain(sectionHeader)
} else {
expect(assetTables).not.toContain(sectionHeader)
}
}
if (Object.entries(options).every(([, included]) => included === false)) {
expect(assetTables).toBe('')
}
}
)
})

describe('getDescribeAssetsOptions', () => {
test(`getDescribeAssetsOptions: "all"`, () => {
const generatedOptions = getDescribeAssetsOptions('all')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(true)
}
})

test(`getDescribeAssetsOptions: "none"`, () => {
const generatedOptions = getDescribeAssetsOptions('none')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(false)
}
})

test(`getDescribeAssetsOptions: "changed-only"`, () => {
const generatedOptions = getDescribeAssetsOptions('changed-only')
for (const section of describeAssetsSections) {
if (section === 'unchanged') {
expect(generatedOptions[section]).toBe(false)
} else {
expect(generatedOptions[section]).toBe(true)
}
}
})

test('getDescribeAssetsOptions: handles keyword with spaces', () => {
const generatedOptions = getDescribeAssetsOptions(' all ')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(true)
}
})

test('getDescribeAssetsOptions: unsupported option throws', () => {
expect(() => getDescribeAssetsOptions('unsupported options')).toThrow()
})

// generate all combinations of sections as string
const cases: string[] = []
for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) {
const options: string[] = []
for (let n = 0; n < describeAssetsSections.length; n++) {
if ((i >> n) & 1) {
options.push(describeAssetsSections[n])
}
}
if (options.length > 0) {
cases.push(options.join(' '))
}
}

test.each(cases)(`getDescribeAssetsOptions: %j`, (optionString: string) => {
const generatedOptions = getDescribeAssetsOptions(optionString)
const providedOptions = optionString.split(' ')
for (const section of providedOptions) {
expect(generatedOptions[section as DescribeAssetsSection]).toBe(true)
}
for (const section of describeAssetsSections.filter(
s => !providedOptions.includes(s)
)) {
expect(generatedOptions[section]).toBe(false)
}
})

test('getDescribeAssetsOptions: handles sections with spaces', () => {
const optionString = ' added removed bigger'
const generatedOptions = getDescribeAssetsOptions(optionString)
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(optionString.includes(section))
}
})
})
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ inputs:
title:
description: 'An optional addition to the title, which also helps key comments, useful if running more than 1 copy of this action'
required: false
describe-assets:
description: |
Optional specification describing asset changes. May be one of the convenience
keywords "all", "none", or "changed-only" (for all except the unchanged section), OR
a string of space-separated section names, e.g. "added bigger unchanged", where
all sections are:
- added
- removed
- bigger
- smaller
- unchanged
If not provided, "all" is used (equivalent to "added removed bigger smaller unchanged")
required: false
default: 'all'

runs:
using: 'node20'
Expand Down
Loading
Loading