-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-lighthouse): add onlyAudits logic (#472)
- Loading branch information
Showing
10 changed files
with
288 additions
and
126 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
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
73 changes: 73 additions & 0 deletions
73
packages/plugin-lighthouse/src/lib/lighthouse-plugin.unit.test.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,73 @@ | ||
import { expect } from 'vitest'; | ||
import { | ||
auditSchema, | ||
groupSchema, | ||
pluginConfigSchema, | ||
} from '@code-pushup/models'; | ||
import { AUDITS, GROUPS } from './constants'; | ||
import { lighthousePlugin } from './lighthouse-plugin'; | ||
|
||
describe('lighthousePlugin-config-object', () => { | ||
it('should create valid plugin config', () => { | ||
const pluginConfig = lighthousePlugin({ | ||
url: 'https://code-pushup-portal.com', | ||
}); | ||
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
expect(pluginConfig.audits.length).toBeGreaterThan(0); | ||
expect(pluginConfig.groups?.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('should filter audits by onlyAudits string "first-contentful-paint"', () => { | ||
const pluginConfig = lighthousePlugin({ | ||
url: 'https://code-pushup-portal.com', | ||
onlyAudits: 'first-contentful-paint', | ||
}); | ||
|
||
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
|
||
expect(pluginConfig.audits[0]).toEqual( | ||
expect.objectContaining({ | ||
slug: 'first-contentful-paint', | ||
}), | ||
); | ||
}); | ||
|
||
it('should filter groups by onlyAudits string "first-contentful-paint"', () => { | ||
const pluginConfig = lighthousePlugin({ | ||
url: 'https://code-pushup-portal.com', | ||
onlyAudits: 'first-contentful-paint', | ||
}); | ||
|
||
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
expect(pluginConfig.groups).toHaveLength(1); | ||
|
||
const refs = pluginConfig.groups?.[0]?.refs; | ||
expect(refs).toHaveLength(1); | ||
|
||
expect(refs).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
slug: 'first-contentful-paint', | ||
}), | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
describe('constants', () => { | ||
it.each(AUDITS.map(a => [a.slug, a]))( | ||
'should parse audit "%s" correctly', | ||
(slug, audit) => { | ||
expect(() => auditSchema.parse(audit)).not.toThrow(); | ||
expect(audit.slug).toEqual(slug); | ||
}, | ||
); | ||
|
||
it.each(GROUPS.map(g => [g.slug, g]))( | ||
'should parse group "%s" correctly', | ||
(slug, group) => { | ||
expect(() => groupSchema.parse(group)).not.toThrow(); | ||
expect(group.slug).toEqual(slug); | ||
}, | ||
); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Audit, Group } from '@code-pushup/models'; | ||
import { toArray } from './transform'; | ||
|
||
export function filterGroupsByAuditSlug( | ||
groups: Group[], | ||
auditSlugs: string | string[], | ||
): Group[] { | ||
const slugs = toArray(auditSlugs); | ||
if (slugs.length === 0) { | ||
return groups; | ||
} | ||
return ( | ||
groups | ||
.map(group => ({ | ||
...group, | ||
refs: filterSlug(group.refs, slugs), | ||
})) | ||
// filter out groups that have no audits includes from onlyAudits (avoid empty groups) | ||
.filter(group => group.refs.length) | ||
); | ||
} | ||
|
||
export function filterAuditsBySlug( | ||
list: Audit[], | ||
auditSlugs: string[] | string, | ||
): Audit[] { | ||
const slugs = toArray(auditSlugs); | ||
if (slugs.length === 0) { | ||
return list; | ||
} | ||
return filterSlug(list, slugs); | ||
} | ||
|
||
export function filterSlug<T extends { slug: string }>( | ||
refs: T[], | ||
slugOrSlugs: string | string[], | ||
): T[] { | ||
const slugs = toArray(slugOrSlugs); | ||
return refs.filter(({ slug }) => slugs.includes(slug)); | ||
} |
Oops, something went wrong.