Skip to content

Commit

Permalink
fix(plugin-lighthouse): add chromium to dev deps (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored May 2, 2024
1 parent 14b8aff commit 6f92d90
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 30 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ jobs:
uses: nrwl/nx-set-shas@v4
- name: Install dependencies
run: npm ci
- name: Set custom Chrome path for Windows only
if: matrix.os == 'windows-latest'
# This path is considered in `testing/setup/src/lib/chrome-path-setup.ts` and used in different test configurations
run: |
echo "CUSTOM_CHROME_PATH=C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
shell: pwsh
#- name: Log all environment variables
# run: |
# printenv
- name: Integration test affected projects
run: npx nx affected -t integration-test --parallel=3 --coverage.enabled

Expand All @@ -139,7 +130,7 @@ jobs:
- name: Install dependencies
run: npm ci
- name: E2E test affected projects
run: npx nx affected:e2e --parallel=3 --skipNxCache
run: npx nx affected:e2e --parallel=3

build:
runs-on: ubuntu-latest
Expand Down
191 changes: 191 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@vitest/coverage-v8": "1.3.1",
"@vitest/ui": "1.3.1",
"benchmark": "^2.1.4",
"chromium": "^3.0.3",
"commitizen": "^4.3.0",
"commitlint-plugin-tense": "^1.0.2",
"conventional-changelog-angular": "^7.0.0",
Expand Down
29 changes: 11 additions & 18 deletions packages/plugin-lighthouse/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ To test lighthouse properly we work with a predefined testing setup.

On some OS there could be a problem finding the path to Chrome.

We try to detect it automatically in the set-setup script.
We try to detect it automatically in the [`chrome-path.setup.ts` script](../../testing/test-setup/src/lib/chrome-path.setup.ts).
There we use `getChromePath` and have `chromium` installed as NPM package, so detecting the path should not cause any problem.

If no chrome path is detected the error looks like this: `Runtime error encountered: No Chrome installations found.`
However, if no chrome path is detected automatically the error looks like this:

```bash
Runtime error encountered: No Chrome installations found.
```

To prevent this from happening you have to provide the path manually in your `.env`:

```bash
CUSTOM_CHROME_PATH=/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
```

In the CI you can set the env variable like this:
In the CI you can set a static path if needed over the env variable like this:

```yml
# ...
Expand All @@ -41,21 +46,7 @@ In the CI you can set the env variable like this:
# ...
```

We added consider this path in our `beforeAll` hook.

```ts
beforeEach(() => {
try {
vi.stubEnv('CHROME_PATH', getChromePath());
} catch (e) {
const customChromePath = process.env['CUSTOM_CHROME_PATH'];
if (customChromePath == null || customChromePath === '') {
throw new Error('Chrome path not found. Please read the in the packages CONTRIBUTING.md/#trouble-shooting section.');
}
vi.stubEnv('CHROME_PATH', customChromePath);
}
});
```
We consider this path in our `beforeAll` hook in a [`chrome-path.setup.ts` script](../../testing/test-setup/src/lib/chrome-path.setup.ts).

### Testing chrome flags

Expand Down Expand Up @@ -114,6 +105,8 @@ _A helpful chromium setup is preconfigured with the following settings:_

4. Understand error messages (⏳ could also be because of timeout problems :D )

- Lighthouse error - `Runtime error encountered: No Chrome installations found.`
Read further under [chrome-path](#chrome-path)
- Could not find `report.json` (⏳)
![lighthouse-error-2.png](./docs/images/lighthouse-error-2.png)
- Lighthouse Error - `Could Not Connect to Chrome` (⏳)
Expand Down
23 changes: 21 additions & 2 deletions testing/test-setup/src/lib/chrome-path.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ import { getChromePath } from 'chrome-launcher';
import * as process from 'node:process';
import { beforeEach, vi } from 'vitest';

beforeEach(() => {
beforeEach(async () => {
const customChromePath = process.env['CUSTOM_CHROME_PATH'];

if (customChromePath == null) {
vi.stubEnv('CHROME_PATH', getChromePath());
try {
const path = getChromePath();
vi.stubEnv('CHROME_PATH', path);
} catch (error) {
if (
error instanceof Error &&
error.message.includes('No Chrome installations found.')
) {
const chromium = (await import('chromium' as string)) as Record<
'path',
string
>;
console.info(
`${error.message} Using chromium from node_modules instead: ${chromium.path}`,
);
vi.stubEnv('CHROME_PATH', chromium.path);
} else {
throw error;
}
}
} else {
vi.stubEnv('CHROME_PATH', customChromePath);
}
Expand Down

0 comments on commit 6f92d90

Please sign in to comment.