diff --git a/packages/cli/src/assert/assert.js b/packages/cli/src/assert/assert.js index 5cabd9c85..618488691 100644 --- a/packages/cli/src/assert/assert.js +++ b/packages/cli/src/assert/assert.js @@ -50,14 +50,14 @@ function readBudgets(budgetsFile) { * @return {Promise} */ 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); diff --git a/packages/cli/test/assert.test.js b/packages/cli/test/assert.test.js index b3e5867c7..f4be1c2e7 100644 --- a/packages/cli/test/assert.test.js +++ b/packages/cli/test/assert.test.js @@ -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 = {}) => { @@ -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'); @@ -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)'); + }); });