diff --git a/packages/next/lib/eslint/runLintCheck.ts b/packages/next/lib/eslint/runLintCheck.ts index 51e99ca2499d..1266a12997cf 100644 --- a/packages/next/lib/eslint/runLintCheck.ts +++ b/packages/next/lib/eslint/runLintCheck.ts @@ -1,4 +1,4 @@ -import { promises as fs, promises, statSync } from 'fs' +import { promises as fs } from 'fs' import chalk from 'next/dist/compiled/chalk' import path from 'path' diff --git a/test/integration/eslint/test/index.test.js b/test/integration/eslint/test/index.test.js index 31c7be989885..ff83380208a4 100644 --- a/test/integration/eslint/test/index.test.js +++ b/test/integration/eslint/test/index.test.js @@ -604,5 +604,90 @@ describe('ESLint', () => { expect(output).not.toContain('pages/index.js') expect(output).not.toContain('External synchronous scripts are forbidden') }) + + test('output flag create a file respecting the chosen format', async () => { + const filePath = `${__dirname}/output/output.json` + const { stdout, stderr } = await nextLint( + dirFileLinting, + ['--format', 'json', '--output-file', filePath], + { + stdout: true, + stderr: true, + } + ) + + const cliOutput = stdout + stderr + const fileOutput = await fs.readJSON(filePath) + + expect(cliOutput).toContain( + `The output file has been created: ${filePath}` + ) + + if (fileOutput && fileOutput.length) { + fileOutput.forEach((file) => { + expect(file).toHaveProperty('filePath') + expect(file).toHaveProperty('messages') + expect(file).toHaveProperty('errorCount') + expect(file).toHaveProperty('warningCount') + expect(file).toHaveProperty('fixableErrorCount') + expect(file).toHaveProperty('fixableWarningCount') + expect(file).toHaveProperty('source') + expect(file).toHaveProperty('usedDeprecatedRules') + }) + + expect(fileOutput[0].messages).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + message: + 'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.', + }), + expect.objectContaining({ + message: `Do not use . Use Image from 'next/image' instead. See: https://nextjs.org/docs/messages/no-img-element`, + }), + ]) + ) + + expect(fileOutput[1].messages).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + message: + 'External synchronous scripts are forbidden. See: https://nextjs.org/docs/messages/no-sync-scripts', + }), + ]) + ) + } + }) + + test('output flag create a file respecting the chosen format', async () => { + const filePath = `${__dirname}/output/output.txt` + const { stdout, stderr } = await nextLint( + dirFileLinting, + ['--format', 'compact', '--output-file', filePath], + { + stdout: true, + stderr: true, + } + ) + + const cliOutput = stdout + stderr + const fileOutput = fs.readFileSync(filePath, 'utf8') + + expect(cliOutput).toContain( + `The output file has been created: ${filePath}` + ) + + expect(fileOutput).toContain('file-linting/pages/bar.js') + expect(fileOutput).toContain( + 'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.' + ) + expect(fileOutput).toContain( + `Do not use . Use Image from 'next/image' instead. See: https://nextjs.org/docs/messages/no-img-element` + ) + + expect(fileOutput).toContain('file-linting/pages/index.js') + expect(fileOutput).toContain( + 'External synchronous scripts are forbidden. See: https://nextjs.org/docs/messages/no-sync-scripts' + ) + }) }) })