diff --git a/src/linter/reporters/console.mjs b/src/linter/reporters/console.mjs index 58975931..29275472 100644 --- a/src/linter/reporters/console.mjs +++ b/src/linter/reporters/console.mjs @@ -21,10 +21,10 @@ export default issue => { ? ` (${issue.location.position.start.line}:${issue.location.position.end.line})` : ''; - process.stdout.write( + console[issue.level]( styleText( levelToColorMap[issue.level], `${issue.message} at ${issue.location.path}${position}` - ) + '\n' + ) ); }; diff --git a/src/linter/tests/reporters/console.test.mjs b/src/linter/tests/reporters/console.test.mjs index cde6024f..a14d4172 100644 --- a/src/linter/tests/reporters/console.test.mjs +++ b/src/linter/tests/reporters/console.test.mjs @@ -1,26 +1,36 @@ import { describe, it } from 'node:test'; -import console from '../../reporters/console.mjs'; import assert from 'node:assert'; +import reporter from '../../reporters/console.mjs'; import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs'; -describe('console', () => { - it('should write to stdout with the correct colors based on the issue level', t => { - t.mock.method(process.stdout, 'write'); - - console(infoIssue); - console(warnIssue); - console(errorIssue); +const testCases = [ + { + issue: infoIssue, + method: 'info', + expected: '\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m', + }, + { + issue: warnIssue, + method: 'warn', + expected: '\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m', + }, + { + issue: errorIssue, + method: 'error', + expected: '\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m', + }, +]; - assert.strictEqual(process.stdout.write.mock.callCount(), 3); +describe('console', () => { + testCases.forEach(({ issue, method, expected }) => { + it(`should use correct colors and output on ${method} issues`, t => { + t.mock.method(console, method); + const mock = console[method].mock; - const callsArgs = process.stdout.write.mock.calls.map(call => - call.arguments[0].trim() - ); + reporter(issue); - assert.deepStrictEqual(callsArgs, [ - '\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m', - '\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m', - '\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m', - ]); + assert.strictEqual(mock.callCount(), 1); + assert.deepStrictEqual(mock.calls[0].arguments, [expected]); + }); }); });