-
Notifications
You must be signed in to change notification settings - Fork 16
docs(examples-plugins): add example plugin file-size #315
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
Merged
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
0616166
wip
BioPhoton 04d1536
docs(examples): add custom file-size plugin example
BioPhoton f40eb8a
refactor: remove target lint
BioPhoton 5cf9a3b
revert changes
BioPhoton f691181
test(examples-plugins): add tests
BioPhoton 17ad833
refactor(examples-plugins): format
BioPhoton 12783e6
refactor(examples-plugins): format
BioPhoton ad22e2d
test(examples-plugins): fix target
BioPhoton cee2772
test(examples-plugins): fix target
BioPhoton 81fadd3
test(examples-plugins): wip
BioPhoton 65760a9
test(examples-plugins): test file-size plugin
BioPhoton 76ca096
test(examples-plugins): adopt path for windows
BioPhoton 9112b10
test(examples-plugins): adopt path for windows
BioPhoton 2b89a33
ci(examples-plugins): setup lint target and fix issues
BioPhoton f546538
ci(test-utils): add memfs
BioPhoton 415695d
test(examples-plugins): add dependency
BioPhoton d2651d9
test(examples-plugins): fix imports
BioPhoton 5a22841
test(examples-plugins): fix lint config
BioPhoton 1750b88
test(examples-plugins): fix format
BioPhoton 63f3b9d
test(examples-plugins): fix format
BioPhoton 5db05a2
refactor(examples-plugins): reduce code
BioPhoton 93f7584
refactor(examples-plugins): reduce code
BioPhoton ede253e
refactor(examples-plugins): refactor code
BioPhoton 5eeaae6
refactor(examples-plugins): refactor code
BioPhoton de95230
refactor(examples-plugins): add comments
BioPhoton 9962271
Update examples/plugins/src/file-size.plugin.ts
BioPhoton 8641070
Update examples/plugins/src/file-size.plugin.ts
BioPhoton 2f59ec0
Update examples/plugins/src/file-size.plugin.unit.test.ts
BioPhoton 1f391a8
refactor(examples-plugins): adopt logic and tests
BioPhoton 4528bca
refactor(examples-plugins): add file path formatting
BioPhoton 745557b
Merge branch 'main' into example-plugin-file-size
BioPhoton 43279db
refactor: merge
BioPhoton 15a37e2
refactor(examples-plugins): fix lint
BioPhoton e0afab7
refactor(examples-plugins): fix lint
BioPhoton b5f0003
merge main
BioPhoton dc3f959
Update examples/plugins/src/file-size.plugin.unit.test.ts
BioPhoton 860dd10
merge main
BioPhoton 3bcd9f2
merge main
BioPhoton 62a1626
wip
BioPhoton f58c427
wip
BioPhoton 10d2f7e
test(examples-plugins): adopt tests
BioPhoton df683b3
test(examples-plugins): format
BioPhoton 0fecf08
test(examples-plugins): change imports
BioPhoton 641c19c
test(examples-plugins): change imports 2
BioPhoton dff457d
test(examples-plugins): change imports 3
BioPhoton 016ca2f
test(examples-plugins): change file type
BioPhoton 0d38661
test(examples-plugins): fix linting
BioPhoton 38b2940
Update testing-utils/src/lib/setup/fs.mock.ts
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
| { | ||
| "extends": ["../../.eslintrc.json"], | ||
| "ignorePatterns": ["!**/*"], | ||
| "overrides": [ | ||
| { | ||
| "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
| "rules": { | ||
| "@nx/enforce-module-boundaries": ["off"] | ||
| } | ||
| }, | ||
| { | ||
| "files": ["*.ts", "*.tsx"], | ||
| "parserOptions": { | ||
| "project": ["examples/plugins/tsconfig.*?.json"] | ||
| }, | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.test.ts", "*.test.tsx"], | ||
| "parserOptions": { | ||
| "project": ["examples/plugins/tsconfig.*?.json"] | ||
| }, | ||
| "rules": { | ||
| "max-lines": "off" | ||
| } | ||
| }, | ||
| { | ||
| "files": ["*.js", "*.jsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.json"], | ||
| "parser": "jsonc-eslint-parser", | ||
| "rules": { | ||
| "@nx/dependency-checks": ["error"] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or 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,38 @@ | ||
| import { | ||
| create as fileSizePlugin, | ||
| recommendedRefs as fileSizeRecommendedRefs, | ||
| } from './src/file-size/file-size.plugin'; | ||
|
|
||
| /** | ||
| * Run it with: | ||
| * `nx run-collect examples-plugins` | ||
| * | ||
| * - For all formats use `--persist.format=md,json` | ||
| * - For better debugging use `--verbose --no-progress` | ||
| * | ||
| */ | ||
|
|
||
| const outputDir = '.code-pushup'; | ||
| // eslint-disable-next-line unicorn/no-unreadable-iife | ||
| const config = (() => ({ | ||
| persist: { | ||
| outputDir, | ||
| }, | ||
| plugins: [ | ||
| fileSizePlugin({ | ||
| directory: './dist', | ||
| pattern: /\.js$/, | ||
| // eslint-disable-next-line no-magic-numbers | ||
| budget: 42_000, | ||
| }), | ||
| ], | ||
| categories: [ | ||
| { | ||
| slug: 'performance', | ||
| title: 'Performance', | ||
| refs: [...fileSizeRecommendedRefs], | ||
| }, | ||
| ], | ||
| }))(); | ||
|
|
||
| export default config; |
This file contains hidden or 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,40 @@ | ||
| { | ||
| "name": "examples-plugins", | ||
| "$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
| "sourceRoot": "examples/plugins/src", | ||
| "projectType": "library", | ||
| "implicitDependencies": ["cli"], | ||
| "targets": { | ||
| "lint": { | ||
| "executor": "@nx/linter:eslint", | ||
| "outputs": ["{options.outputFile}"], | ||
| "options": { | ||
| "lintFilePatterns": ["examples/plugins/**/*.ts"] | ||
| }, | ||
| "dependsOn": ["^build"] | ||
| }, | ||
| "unit-test": { | ||
| "executor": "@nx/vite:test", | ||
| "outputs": ["{options.reportsDirectory}"], | ||
| "options": { | ||
BioPhoton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "config": "examples/plugins/vite.config.unit.ts", | ||
| "reportsDirectory": "../../coverage/examples-plugins/unit-tests" | ||
| }, | ||
| "dependsOn": ["^build"] | ||
| }, | ||
| "integration-test": { | ||
| "executor": "@nx/vite:test", | ||
| "outputs": ["{options.reportsDirectory}"], | ||
| "options": { | ||
| "config": "examples/plugins/vite.config.integration.ts", | ||
| "reportsDirectory": "../../coverage/examples-plugins/integration-tests" | ||
| }, | ||
| "dependsOn": ["^build"] | ||
| }, | ||
| "run-collect": { | ||
| "command": "npx dist/packages/cli collect --config=examples/plugins/code-pushup.config.ts --persist.format=md", | ||
| "dependsOn": ["^build"] | ||
| } | ||
| }, | ||
| "tags": [] | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
examples/plugins/src/file-size/file-size.plugin.integration.test.ts
This file contains hidden or 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,115 @@ | ||
| import { vol } from 'memfs'; | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { executePlugin } from '@code-pushup/core'; | ||
| import { | ||
| auditSchema, | ||
| categoryRefSchema, | ||
| pluginConfigSchema, | ||
| } from '@code-pushup/models'; | ||
| import { MEMFS_VOLUME } from '@code-pushup/testing-utils'; | ||
| import { | ||
| PluginOptions, | ||
| audits, | ||
| create, | ||
| recommendedRefs, | ||
| pluginSlug as slug, | ||
| } from './file-size.plugin'; | ||
|
|
||
| const projectJson = JSON.stringify( | ||
| { | ||
| test: 42, | ||
| arr: [1, 2, 3], | ||
| obj: { | ||
| test: 42, | ||
| }, | ||
| }, | ||
| null, | ||
| 2, | ||
| ); | ||
| const testJs = ` | ||
| const str = 'Hello World' | ||
| const num = 42; | ||
| const obj = ${projectJson}; | ||
| `; | ||
|
|
||
| describe('create', () => { | ||
| const baseOptions: PluginOptions = { | ||
| directory: '/', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vol.fromJSON( | ||
| { | ||
| 'project.json': projectJson, | ||
| 'src/test.js': testJs, | ||
| }, | ||
| MEMFS_VOLUME, | ||
| ); | ||
| }); | ||
|
|
||
| it('should return valid PluginConfig', () => { | ||
| const pluginConfig = create(baseOptions); | ||
| expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
| expect(pluginConfig).toEqual({ | ||
| audits, | ||
| description: | ||
| 'A plugin to measure and assert filesize of files in a directory.', | ||
| icon: 'javascript', | ||
| runner: expect.any(Function), | ||
| slug, | ||
| title: 'File Size', | ||
| }); | ||
| }); | ||
|
|
||
| it('should return PluginConfig that executes correctly', async () => { | ||
| const pluginConfig = create(baseOptions); | ||
| await expect(executePlugin(pluginConfig)).resolves.toMatchObject({ | ||
| description: | ||
| 'A plugin to measure and assert filesize of files in a directory.', | ||
| slug, | ||
| title: 'File Size', | ||
| duration: expect.any(Number), | ||
| date: expect.any(String), | ||
| audits: expect.any(Array), | ||
| }); | ||
| }); | ||
|
|
||
| it('should use pattern', async () => { | ||
| const pluginConfig = create({ | ||
| ...baseOptions, | ||
| pattern: /\.js$/, | ||
| }); | ||
| const { audits: auditOutputs } = await executePlugin(pluginConfig); | ||
|
|
||
| expect(auditOutputs).toHaveLength(1); | ||
| expect(auditOutputs[0]?.score).toBe(1); | ||
| expect(auditOutputs[0]?.details?.issues).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should use budget', async () => { | ||
| const pluginConfig = create({ | ||
| ...baseOptions, | ||
| budget: 0, | ||
| }); | ||
| const { audits: auditOutputs } = await executePlugin(pluginConfig); | ||
|
|
||
| expect(auditOutputs).toHaveLength(1); | ||
| expect(auditOutputs[0]?.score).toBe(0); | ||
| expect(auditOutputs[0]?.details?.issues).toHaveLength(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('audits', () => { | ||
| it.each(audits)('should be a valid audit meta info', audit => { | ||
| expect(() => auditSchema.parse(audit)).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('recommendedRefs', () => { | ||
| it.each(recommendedRefs)( | ||
| 'should be a valid category reference', | ||
| categoryRef => { | ||
| expect(() => categoryRefSchema.parse(categoryRef)).not.toThrow(); | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.