From 3aed77ff33de446c1bd937b3618c6b7c182f0865 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Sat, 17 May 2025 17:59:56 -0400 Subject: [PATCH 1/2] chore(lint): use `console[xyz]` over `process` --- src/linter/reporters/console.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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' + ) ); }; From 9431f78b4cc74ab8c21d06fe70c3fe3e7f560879 Mon Sep 17 00:00:00 2001 From: avivkeller Date: Sun, 18 May 2025 14:17:13 -0400 Subject: [PATCH 2/2] fix tests --- src/linter/tests/reporters/console.test.mjs | 44 +++++++++++++-------- 1 file changed, 27 insertions(+), 17 deletions(-) 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]); + }); }); });