tests: make update:sample-artifacts work for a single artifact type (#8802)

This commit is contained in:
Matt Zeunert 2019-05-03 22:50:00 +01:00 коммит произвёл Brendan Kenny
Родитель 03e6566fc9
Коммит 8ad2b62d0e
3 изменённых файлов: 32 добавлений и 5 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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');
@ -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', () => {
@ -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]));