diff --git a/cli/printer.js b/cli/printer.js index 026cc9ae9b5d..6a8dc2a67b06 100644 --- a/cli/printer.js +++ b/cli/printer.js @@ -67,7 +67,7 @@ function writeFile(filePath, output, outputMode) { if (err) { return reject(err); } - log.log('Printer', `${LH.OutputMode[outputMode]} output written to ${filePath}`); + log.log('Printer', `${OutputMode[outputMode]} output written to ${filePath}`); resolve(); }); }); diff --git a/cli/test/cli/printer-test.js b/cli/test/cli/printer-test.js index 82f5cc1c37c7..3bf6687ae86c 100644 --- a/cli/test/cli/printer-test.js +++ b/cli/test/cli/printer-test.js @@ -6,6 +6,7 @@ import assert from 'assert/strict'; import fs from 'fs'; +import path from 'path'; import {readJson} from '../../../core/test/test-utils.js'; import * as Printer from '../../printer.js'; @@ -49,4 +50,20 @@ describe('Printer', () => { assert.strictEqual(typeof mode, 'string'); }); }); + + it('creates missing directories when writing to file', () => { + const dirPath = './non/existent/directory/.test-file.json'; + const report = JSON.stringify(sampleResults); + const dir = path.dirname(dirPath); + if (fs.existsSync(dir)) { + fs.rmdirSync(dir, { recursive: true }); + } + return Printer.write(report, 'json', dirPath).then(_ => { + assert.ok(fs.existsSync(dir), `Directory ${dir} should exist now`); + const fileContents = fs.readFileSync(dirPath, 'utf8'); + assert.ok(/lighthouseVersion/gim.test(fileContents)); + fs.unlinkSync(dirPath); + fs.rmdirSync(dir, { recursive: true }); + }); + }); });