Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): assert - destructure lhr from options #1062

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)');
});
});
Loading