|
| 1 | +# Contributing |
| 2 | + |
| 3 | +## Setup |
| 4 | + |
| 5 | +Make sure to install dependencies: |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install |
| 9 | +``` |
| 10 | + |
| 11 | +### Chrome path |
| 12 | + |
| 13 | +In this plugin we provide Lighthouse functionality exposed over the `lighthousePlugin`. |
| 14 | +To test lighthouse properly we work with a predefined testing setup. |
| 15 | + |
| 16 | +On some OS there could be a problem finding the path to Chrome. |
| 17 | + |
| 18 | +We try to detect it automatically in the set-setup script. |
| 19 | + |
| 20 | +If no chrome path is detected the error looks like this: `Runtime error encountered: No Chrome installations found.` |
| 21 | + |
| 22 | +To prevent this from happening you have to provide the path manually in your `.env`: |
| 23 | + |
| 24 | +```bash |
| 25 | +CUSTOM_CHROME_PATH=/Applications/Google Chrome.app/Contents/MacOS/Google Chrome |
| 26 | +``` |
| 27 | + |
| 28 | +In the CI you can set the env variable like this: |
| 29 | + |
| 30 | +```yml |
| 31 | +# ... |
| 32 | +- name: Set custom Chrome path for Windows only |
| 33 | + if: matrix.os == 'windows-latest' |
| 34 | + run: | |
| 35 | + echo "CUSTOM_CHROME_PATH=C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 |
| 36 | + shell: pwsh |
| 37 | + |
| 38 | +# Optional debug log |
| 39 | +- name: Log all environment variables |
| 40 | + run: printenv |
| 41 | +# ... |
| 42 | +``` |
| 43 | + |
| 44 | +We added consider this path in our `beforeAll` hook. |
| 45 | + |
| 46 | +```ts |
| 47 | +beforeEach(() => { |
| 48 | + try { |
| 49 | + vi.stubEnv('CHROME_PATH', getChromePath()); |
| 50 | + } catch (e) { |
| 51 | + const customChromePath = process.env['CUSTOM_CHROME_PATH']; |
| 52 | + if (customChromePath == null || customChromePath === '') { |
| 53 | + throw new Error('Chrome path not found. Please read the in the packages CONTRIBUTING.md/#trouble-shooting section.'); |
| 54 | + } |
| 55 | + vi.stubEnv('CHROME_PATH', customChromePath); |
| 56 | + } |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +### Testing chrome flags |
| 61 | + |
| 62 | +1. run `npx chrome-debug --<chromium-flag>` to pass terminal arguments to Chrome. E.g. `npx chrome-debug --headless=shell`. |
| 63 | + `npx chrome-debug --headless=shell --@TODO-PUT-OTHER-EXAMPLE-FOR-FLAG` |
| 64 | + |
| 65 | +For a full list of available flags check out [this document](https://peter.sh/experiments/chromium-command-line-switches/). |
| 66 | + |
| 67 | +> [!NOTE] |
| 68 | +> To pass chrome flags to lighthouse you have to provide them under `--chrome-flags="<chrome-flags-as-array>"`. |
| 69 | +> E.g. `lighthouse https://example.com --chrome-flage="--headless=shell"` |
| 70 | +
|
| 71 | +2. Check if the flag got accepted. This is quite unintuitive as we would expect the passed flag to be visible under `chrome://flags/` but as you can see in the screenshot it is not visible. |
| 72 | + <img width="1202" alt="chrome-flags" src="./docs/images/chrome-flags.png"> |
| 73 | + Instead open `chrome://version/` and look under the "Command Line" section. |
| 74 | + <img width="1202" alt="chrome-chrome-version" src="./docs/images/chrome-version.png"> |
| 75 | + |
| 76 | +### Chrome User Data |
| 77 | + |
| 78 | +To bootstrap Chrome with a predefined for setting we have to provide a couple of config files that we located under `<project-root>/mock/chromium-user-data`. |
| 79 | +When executing Lighthouse we provide the path to this folder over the `Flag` object. |
| 80 | + |
| 81 | +To generate initialise or edit the file structure under `chromium-user-data` do the following steps: |
| 82 | + |
| 83 | +1. Spin up Chrome by running `npx chrome-debug --user-data-dir=./packages/plugin-lighthouse/mock/chromium-user-data` |
| 84 | + <img width="1202" alt="chrome-blank-screen" src="./docs/images/chrome-blank-screen.png"> |
| 85 | + |
| 86 | +2. If you do this the first time you should already see content under `<project-root>/mock/chromium-user-data` |
| 87 | +3. Edit the configuration over the Chrome UI. E.g. adding a profile |
| 88 | +4. Close chromium and open it again, and you should see chromium bootstraps as the configured user |
| 89 | + <img width="1202" alt="chrome-blank-screen-pre-configured" src="./docs/images/chrome-blank-screen-pre-configure.png"> |
| 90 | + |
| 91 | +To reset the above just delete the folder and apply the settings again. |
| 92 | + |
| 93 | +_A helpful chromium setup is preconfigured with the following settings:_ |
| 94 | + |
| 95 | +- A user profile is set up. This enables certain debugging related options as well as help to visually distinguish between test setups as the header bar is colored. |
| 96 | + <img width="1202" alt="chrome-settings-manage-profile" src="./docs/images/chrome-settings-manage-profile.png"> |
| 97 | + |
| 98 | +#### Resources |
| 99 | + |
| 100 | +- [chromium flags guide](https://www.chromium.org/developers/how-tos/run-chromium-with-flags/) |
| 101 | + |
| 102 | +## Troubleshooting |
| 103 | + |
| 104 | +1. Verify Chrome Installation |
| 105 | + Ensure Chrome is correctly installed and accessible to the Lighthouse process. |
| 106 | + Run `npx chrome-debug` to test it. Read further under [chrome-path](#chrome-path) |
| 107 | + |
| 108 | +2. Increase Timeout |
| 109 | + Lighthouse has a longer runtime which can time out in different environments. |
| 110 | + **Try increasing the test timeout** in `lighthouse-plugin.integration.test.ts` for `runner creation and execution` test suite. |
| 111 | + |
| 112 | +3. Turn on debug mode |
| 113 | + Show debug logs of Lighthouse. Set the following environment variable: `DEBUG='*'` |
| 114 | + |
| 115 | +4. Understand error messages (⏳ could also be because of timeout problems :D ) |
| 116 | + |
| 117 | +- Could not find `report.json` (⏳) |
| 118 | +  |
| 119 | +- Lighthouse Error - `Could Not Connect to Chrome` (⏳) |
| 120 | +  |
| 121 | + Your Chrome path is set incorrectly. Read further under [chrome-path](#chrome-path) |
| 122 | +- Lighthouse Error - `start lh:<any>:<performancemark>" performance mark has not been set` (⏳) |
| 123 | +  |
| 124 | + If this error pops up you are able to launch Chrome but had problems to communicate over the ports. |
0 commit comments