-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from Scc33/formatLighthouse
Format lighthouse and comment on PRs
- Loading branch information
Showing
7 changed files
with
146 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable jsdoc/require-param */ | ||
"use strict"; | ||
|
||
const stoplight = (res) => (res >= 90 ? "🟢" : res >= 75 ? "🟠" : "🔴"); | ||
const normalizeScore = (res) => Math.round(res * 100); | ||
const formatScore = (res) => { | ||
const normalizedScore = normalizeScore(res); | ||
return `${stoplight(normalizedScore)} ${normalizedScore}`; | ||
}; | ||
|
||
/** | ||
* `core` is in scope from https://github.com/actions/github-script | ||
*/ | ||
export const formatLighthouseResults = ({ core }) => { | ||
// this will be the shape of https://github.com/treosh/lighthouse-ci-action#manifest | ||
const resultsJson = JSON.parse(process.env.LIGHTHOUSE_RESULT); | ||
|
||
// start creating our markdown table | ||
const header = | ||
"# Lighthouse Results\n" + | ||
"| Performance | Accessibility | Best Practices | SEO |\n" + | ||
"| - | - | - | - |\n"; | ||
|
||
// make each formatted score from our lighthouse properties | ||
const performanceScore = formatScore(resultsJson.performance.score); | ||
const accessibilityScore = formatScore(resultsJson.accessibility.score); | ||
const bestPracticesScore = formatScore(resultsJson["best-practices"].score); | ||
const seoScore = formatScore(resultsJson.seo.score); | ||
|
||
const results = `| ${performanceScore} | ${accessibilityScore} | ${bestPracticesScore} | ${seoScore} |`; | ||
|
||
// join the header and the rows together | ||
const finalResults = header + results; | ||
|
||
// return our output to the github action | ||
core.setOutput("comment", finalResults); | ||
}; |
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,56 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { formatLighthouseResults } from "./index"; | ||
|
||
describe("formatLighthouseResults", () => { | ||
const MOCK_LIGHTHOUSE_RESULT = `{ | ||
"performance": { | ||
"title": "Performance", | ||
"id": "performance", | ||
"score": 0.65 | ||
}, | ||
"accessibility": { | ||
"title": "Accessibility", | ||
"description": "These checks highlight opportunities to [improve the accessibility of your web app](https://developer.chrome.com/docs/lighthouse/accessibility/). Automatic detection can only detect a subset of issues and does not guarantee the accessibility of your web app, so [manual testing](https://web.dev/articles/how-to-review) is also encouraged.", | ||
"score": 0.75 | ||
}, | ||
"best-practices": { | ||
"id": "best-practices", | ||
"score": 1 | ||
}, | ||
"seo": { | ||
"title": "SEO", | ||
"description": "These checks ensure that your page is following basic search engine optimization advice. There are many additional factors Lighthouse does not score here that may affect your search ranking, including performance on [Core Web Vitals](https://web.dev/explore/vitals). [Learn more about Google Search Essentials](https://support.google.com/webmasters/answer/35769).", | ||
"id": "seo", | ||
"score": 1 | ||
} | ||
}`; | ||
|
||
let mockCore, originalEnv; | ||
|
||
beforeEach(() => { | ||
mockCore = { setOutput: vi.fn() }; | ||
originalEnv = process.env; | ||
process.env = { | ||
...process.env, | ||
LIGHTHOUSE_RESULT: MOCK_LIGHTHOUSE_RESULT | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it("formats stoplight colors correctly", () => { | ||
formatLighthouseResults({ core: mockCore }); | ||
|
||
const expectations = [ | ||
expect.stringContaining("🟢 100"), | ||
expect.stringContaining("🟠 75"), | ||
expect.stringContaining("🔴 65") | ||
]; | ||
|
||
expectations.forEach((expectation) => { | ||
expect(mockCore.setOutput).toBeCalledWith("comment", expectation); | ||
}); | ||
}); | ||
}); |
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
146c05d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit Test Coverage Report
Unit Test Report