Skip to content

Commit

Permalink
fix(cli): assert - destructure lhr from options (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahmij8 authored Jul 22, 2024
1 parent 82b272c commit 2cb0d07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/assert/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function readBudgets(budgetsFile) {
* @return {Promise<void>}
*/
async function runCommand(options) {
const {budgetsFile, assertions, assertMatrix, preset} = options;
const {budgetsFile, assertions, assertMatrix, preset, lhr} = options;
const areAssertionsSet = Boolean(assertions || assertMatrix || preset);
if (!areAssertionsSet && !budgetsFile) throw new Error('No assertions to use');
if (budgetsFile && areAssertionsSet) throw new Error('Cannot use both budgets AND assertions');
// If we have a budgets file, convert it to our assertions format.
if (budgetsFile) options = await convertBudgetsToAssertions(readBudgets(budgetsFile));

const lhrs = loadSavedLHRs(options.lhr).map(json => JSON.parse(json));
const lhrs = loadSavedLHRs(lhr).map(json => JSON.parse(json));
const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl));
const allResults = getAllAssertionResults(options, lhrs);
const groupedResults = _.groupBy(allResults, result => result.url);
Expand Down
25 changes: 24 additions & 1 deletion packages/cli/test/assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const fullPreset = require('@lhci/utils/src/presets/all.js');
const {runCLI} = require('./test-utils.js');

describe('Lighthouse CI assert CLI', () => {
const rootFixturesDir = path.join(__dirname, 'fixtures');
const fixtureDir = path.join(__dirname, 'fixtures/assertions');
const lighthouseciDir = path.join(fixtureDir, '.lighthouseci');
// eslint-disable-next-line no-control-regex
const replaceTerminalChars = s => s.replace(/\u001b/g, '').replace(/\[\d+m/g, '');
const run = async (args, options = {}) => {
Expand All @@ -34,7 +36,6 @@ describe('Lighthouse CI assert CLI', () => {
};

const writeLhr = (passingAuditIds = []) => {
const lighthouseciDir = path.join(fixtureDir, '.lighthouseci');
if (fs.existsSync(lighthouseciDir)) rimraf.sync(lighthouseciDir);
if (!fs.existsSync(lighthouseciDir)) fs.mkdirSync(lighthouseciDir, {recursive: true});
const fakeLhrPath = path.join(lighthouseciDir, 'lhr-12345.json');
Expand Down Expand Up @@ -123,4 +124,26 @@ describe('Lighthouse CI assert CLI', () => {
expect(result.warnings.length).toMatchInlineSnapshot(`0`);
expect(result.passes.length).toMatchInlineSnapshot(`1`);
});

it('shoud load LHRs from a file', async () => {
const result = await run([
`--no-lighthouserc`,
`--budgetsFile=${path.join(rootFixturesDir, 'budgets.json')}`,
`--lhr ${path.join(lighthouseciDir, 'lhr-12345.json')}`,
]);
expect(result.status).toEqual(1);
expect(result.failures.length).toMatchInlineSnapshot(`1`);
expect(result.stderr).toContain('Checking assertions against 1 URL(s), 1 total run(s)');
});

it('should load LHRs from a directory', async () => {
const result = await run([
`--no-lighthouserc`,
`--budgetsFile=${path.join(rootFixturesDir, 'budgets.json')}`,
`--lhr ${lighthouseciDir}`,
]);
expect(result.status).toEqual(1);
expect(result.failures.length).toMatchInlineSnapshot(`1`);
expect(result.stderr).toContain('Checking assertions against 1 URL(s), 1 total run(s)');
});
});

0 comments on commit 2cb0d07

Please sign in to comment.