diff --git a/packages/vite-plugin-checker/__tests__/unit/__snapshots__/logger.spec.ts.snap b/packages/vite-plugin-checker/__tests__/unit/__snapshots__/logger.spec.ts.snap new file mode 100644 index 00000000..4b398176 --- /dev/null +++ b/packages/vite-plugin-checker/__tests__/unit/__snapshots__/logger.spec.ts.snap @@ -0,0 +1,29 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`logger diagnosticToTerminalLog get error 1`] = ` +" ERROR(ESLint) Unexpected var, use let or const instead. + FILE /Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts:3:1 + + 1 | import { text } from './text' + 2 | + > 3 | var hello = 'Hello' + | ^^^^^^^^^^^^^^^^^^^ + 4 | + 5 | const rootDom = document.querySelector('#root')! + 6 | rootDom.innerHTML = hello + text +" +`; + +exports[`logger diagnosticToTerminalLog get warning 1`] = ` +" WARNING(ESLint) Unexpected var, use let or const instead. + FILE /Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts:3:1 + + 1 | import { text } from './text' + 2 | + > 3 | var hello = 'Hello' + | ^^^^^^^^^^^^^^^^^^^ + 4 | + 5 | const rootDom = document.querySelector('#root')! + 6 | rootDom.innerHTML = hello + text +" +`; diff --git a/packages/vite-plugin-checker/__tests__/unit/fixtures/eslintDiagnostic.ts b/packages/vite-plugin-checker/__tests__/unit/fixtures/eslintDiagnostic.ts new file mode 100644 index 00000000..92db2688 --- /dev/null +++ b/packages/vite-plugin-checker/__tests__/unit/fixtures/eslintDiagnostic.ts @@ -0,0 +1,14 @@ +export const error1 = { + message: 'Unexpected var, use let or const instead.', + conclusion: '', + codeFrame: + " \u001b[0m \u001b[90m 1 |\u001b[39m \u001b[36mimport\u001b[39m { text } \u001b[36mfrom\u001b[39m \u001b[32m'./text'\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 2 |\u001b[39m\u001b[0m\n \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 3 |\u001b[39m \u001b[36mvar\u001b[39m hello \u001b[33m=\u001b[39m \u001b[32m'Hello'\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m |\u001b[39m \u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 4 |\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 5 |\u001b[39m \u001b[36mconst\u001b[39m rootDom \u001b[33m=\u001b[39m document\u001b[33m.\u001b[39mquerySelector(\u001b[32m'#root'\u001b[39m)\u001b[33m!\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 6 |\u001b[39m rootDom\u001b[33m.\u001b[39minnerHTML \u001b[33m=\u001b[39m hello \u001b[33m+\u001b[39m text\u001b[0m", + stripedCodeFrame: + " 1 | import { text } from './text'\n 2 |\n > 3 | var hello = 'Hello'\n | ^^^^^^^^^^^^^^^^^^^\n 4 |\n 5 | const rootDom = document.querySelector('#root')!\n 6 | rootDom.innerHTML = hello + text", + id: '/Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts', + checker: 'ESLint', + loc: { start: { line: 3, column: 1 }, end: { line: 3, column: 20 } }, + level: 1, +} as const + +export const warning1 = { ...error1, level: 0 } diff --git a/packages/vite-plugin-checker/__tests__/unit/logger.spec.ts b/packages/vite-plugin-checker/__tests__/unit/logger.spec.ts new file mode 100644 index 00000000..6e574d8a --- /dev/null +++ b/packages/vite-plugin-checker/__tests__/unit/logger.spec.ts @@ -0,0 +1,17 @@ +import { diagnosticToTerminalLog } from '../../src/logger' +import { error1 as eslintError1, warning1 as eslintWarning1 } from './fixtures/eslintDiagnostic' +import strip from 'strip-ansi' + +describe('logger', () => { + describe('diagnosticToTerminalLog', () => { + it('get error', () => { + const receive = strip(diagnosticToTerminalLog(eslintError1, 'ESLint')) + expect(receive).toMatchSnapshot() + }) + + it('get warning', () => { + const receive = strip(diagnosticToTerminalLog(eslintWarning1, 'ESLint')) + expect(receive).toMatchSnapshot() + }) + }) +}) diff --git a/packages/vite-plugin-checker/src/logger.ts b/packages/vite-plugin-checker/src/logger.ts index d50b85a2..2b0253df 100644 --- a/packages/vite-plugin-checker/src/logger.ts +++ b/packages/vite-plugin-checker/src/logger.ts @@ -65,7 +65,7 @@ export function diagnosticToTerminalLog( [DiagnosticLevel.Message]: boldBlack.bgCyanBright(` MESSAGE${nameInLabel} `), } - const levelLabel = labelMap[d.level || DiagnosticLevel.Error] + const levelLabel = labelMap[d.level ?? DiagnosticLevel.Error] const fileLabel = boldBlack.bgCyanBright(' FILE ') + ' ' const position = d.loc ? chalk.yellow(d.loc.start.line) + ':' + chalk.yellow(d.loc.start.column)