Skip to content

Commit

Permalink
tests: make update:sample-artifacts work for a single artifact type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzeunert authored and brendankenny committed May 3, 2019
1 parent 03e6566 commit 8ad2b62
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ Don't:
If no reference doc exists yet, then you can use the `description` as a stopgap for explaining
both why the audit is important and how to fix it.

## Updating sample artifacts and LHR JSON

```
yarn run update:sample-artifacts # update all artifacts
yarn run update:sample-artifacts ScriptElements # update just one artifact
yarn run update:sample-json # update sample LHR based on sample artifacts
```

When updating all artifacts, usually you'll need to revert changes to the `*.devtoolslog.json` and `*.trace.json` files and manually review changes to `artifacts.json` to make sure they are related to your work.

## Tracking Errors

We track our errors in the wild with Sentry. In general, do not worry about wrapping your audits or gatherers in try/catch blocks and reporting every error that could possibly occur; `lighthouse-core/runner.js` and `lighthouse-core/gather/gather-runner.js` already catch and report any errors that occur while running a gatherer or audit, including errors fatal to the entire run. However, there are some situations when you might want to explicitly handle an error and report it to Sentry or wrap it to avoid reporting. Generally, you can interact with Sentry simply by requiring the `lighthouse-core/lib/sentry.js` file and call its methods. The module exports a delegate that will correctly handle the error reporting based on the user's opt-in preference and will simply no-op if they haven't so you don't need to check.
Expand Down
25 changes: 21 additions & 4 deletions lighthouse-core/scripts/update-report-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

const cli = require('../../lighthouse-cli/run.js');
const cliFlags = require('../../lighthouse-cli/cli-flags.js');
const assetSaver = require('../lib/asset-saver.js');

const artifactPath = 'lighthouse-core/test/results/artifacts';

const {server} = require('../../lighthouse-cli/test/fixtures/static-server.js');

Expand All @@ -32,9 +35,10 @@ const budgetedConfig = {
};

/**
* Update the report artifacts
* Update the report artifacts. If artifactName is set only that artifact will be updated.
* @param {keyof LH.Artifacts=} artifactName
*/
async function update() {
async function update(artifactName) {
// get an available port
server.listen(0, 'localhost');
const port = await new Promise(res => server.on('listening', () => {
Expand All @@ -43,15 +47,28 @@ async function update() {
res(address.port);
}));

const oldArtifacts = await assetSaver.loadArtifacts(artifactPath);

const url = `http://localhost:${port}/dobetterweb/dbw_tester.html`;
const rawFlags = [
'--gather-mode=lighthouse-core/test/results/artifacts',
`--gather-mode=${artifactPath}`,
'--throttling-method=devtools',
url,
].join(' ');
const flags = cliFlags.getFlags(rawFlags);
await cli.runLighthouse(url, flags, budgetedConfig);
await new Promise(res => server.close(res));

if (artifactName) {
// Revert everything except the one artifact
const newArtifacts = await assetSaver.loadArtifacts(artifactPath);
if (!(artifactName in newArtifacts) && !(artifactName in oldArtifacts)) {
console.warn(`❌ Unknown artifact name: '${artifactName}'. Reverting artifacts...`); // eslint-disable-line no-console
}
const finalArtifacts = oldArtifacts;
finalArtifacts[artifactName] = newArtifacts[artifactName];
await assetSaver.saveArtifacts(finalArtifacts, artifactPath);
}
}

update();
update(/** @type {keyof LH.Artifacts | undefined} */ (process.argv[2]));
2 changes: 1 addition & 1 deletion lighthouse-core/test/results/artifacts/artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -2023,4 +2023,4 @@
"systemId": "",
"publicId": ""
}
}
}

0 comments on commit 8ad2b62

Please sign in to comment.