From 8b37c30db2fd4ee7943ffd4264e2b6eb12d23733 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 27 Jul 2023 00:46:45 +0800 Subject: [PATCH] test: pass ut --- .../__snapshots__/logger.spec.ts.snap | 68 ++++++ .../__tests__/fixtures/eslintDiagnostic.ts | 55 +++++ .../checker-eslint/__tests__/logger.spec.ts | 13 ++ .../__snapshots__/vlsConfig.spec.ts.snap | 2 +- .../__tests__/vlsConfig.spec.ts | 8 +- .../__snapshots__/logger.spec.ts.snap | 69 +----- .../__tests__/fixtures/eslintDiagnostic.ts | 39 ---- .../__tests__/fixtures/tsDiagnostic.ts | 200 ------------------ .../__tests__/logger.spec.ts | 15 +- 9 files changed, 146 insertions(+), 323 deletions(-) create mode 100644 packages/checker-eslint/__tests__/__snapshots__/logger.spec.ts.snap create mode 100644 packages/checker-eslint/__tests__/fixtures/eslintDiagnostic.ts create mode 100644 packages/checker-eslint/__tests__/logger.spec.ts rename packages/{vite-plugin-checker => checker-vls}/__tests__/__snapshots__/vlsConfig.spec.ts.snap (97%) rename packages/{vite-plugin-checker => checker-vls}/__tests__/vlsConfig.spec.ts (79%) delete mode 100644 packages/vite-plugin-checker/__tests__/fixtures/tsDiagnostic.ts diff --git a/packages/checker-eslint/__tests__/__snapshots__/logger.spec.ts.snap b/packages/checker-eslint/__tests__/__snapshots__/logger.spec.ts.snap new file mode 100644 index 00000000..76a58276 --- /dev/null +++ b/packages/checker-eslint/__tests__/__snapshots__/logger.spec.ts.snap @@ -0,0 +1,68 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`eslint logger > normalizeEslintDiagnostic > get multiple diagnostics 1`] = ` +[ + { + "checker": "ESLint", + "codeFrame": "   1 | import { text } from './text' +   2 | + > 3 | var hello = 'Hello' +   | ^^^^^^^^^^^^^^^^^^^ +   4 | var hello1 = 'Hello1' +   5 | +   6 | const rootDom = document.querySelector('#root')!", + "conclusion": "", + "id": "/Users/vite-plugin-checker/playground/eslint/src/main.ts", + "level": 1, + "loc": { + "end": { + "column": 20, + "line": 3, + }, + "start": { + "column": 1, + "line": 3, + }, + }, + "message": "Unexpected var, use let or const instead. (no-var)", + "stripedCodeFrame": " 1 | import { text } from './text' + 2 | + > 3 | var hello = 'Hello' + | ^^^^^^^^^^^^^^^^^^^ + 4 | var hello1 = 'Hello1' + 5 | + 6 | const rootDom = document.querySelector('#root')!", + }, + { + "checker": "ESLint", + "codeFrame": "   2 | +   3 | var hello = 'Hello' + > 4 | var hello1 = 'Hello1' +   | ^^^^^^^^^^^^^^^^^^^^^ +   5 | +   6 | const rootDom = document.querySelector('#root')! +   7 | rootDom.innerHTML = hello + text", + "conclusion": "", + "id": "/Users/vite-plugin-checker/playground/eslint/src/main.ts", + "level": 1, + "loc": { + "end": { + "column": 22, + "line": 4, + }, + "start": { + "column": 1, + "line": 4, + }, + }, + "message": "Unexpected var, use let or const instead. (no-var)", + "stripedCodeFrame": " 2 | + 3 | var hello = 'Hello' + > 4 | var hello1 = 'Hello1' + | ^^^^^^^^^^^^^^^^^^^^^ + 5 | + 6 | const rootDom = document.querySelector('#root')! + 7 | rootDom.innerHTML = hello + text", + }, +] +`; diff --git a/packages/checker-eslint/__tests__/fixtures/eslintDiagnostic.ts b/packages/checker-eslint/__tests__/fixtures/eslintDiagnostic.ts new file mode 100644 index 00000000..2bd020e9 --- /dev/null +++ b/packages/checker-eslint/__tests__/fixtures/eslintDiagnostic.ts @@ -0,0 +1,55 @@ +import type { NormalizedDiagnostic } from './../../src/logger' +import type { ESLint } from 'eslint' + +export const error1: NormalizedDiagnostic = { + 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/eslint/src/main.ts', + checker: 'ESLint', + loc: { start: { line: 3, column: 1 }, end: { line: 3, column: 20 } }, + level: 1, +} + +export const warning1: NormalizedDiagnostic = { ...error1, level: 0 } + +export const eslintResult1: ESLint.LintResult = { + filePath: '/Users/vite-plugin-checker/playground/eslint/src/main.ts', + messages: [ + { + ruleId: 'no-var', + severity: 2, + message: 'Unexpected var, use let or const instead.', + line: 3, + column: 1, + nodeType: 'VariableDeclaration', + messageId: 'unexpectedVar', + endLine: 3, + endColumn: 20, + fix: { range: [31, 34], text: 'let' }, + }, + { + ruleId: 'no-var', + severity: 2, + message: 'Unexpected var, use let or const instead.', + line: 4, + column: 1, + nodeType: 'VariableDeclaration', + messageId: 'unexpectedVar', + endLine: 4, + endColumn: 22, + fix: { range: [51, 54], text: 'let' }, + }, + ], + errorCount: 2, + fatalErrorCount: 2, + warningCount: 0, + fixableErrorCount: 2, + fixableWarningCount: 0, + source: + "import { text } from './text'\n\nvar hello = 'Hello'\nvar hello1 = 'Hello1'\n\nconst rootDom = document.querySelector('#root')!\nrootDom.innerHTML = hello + text\n\nexport {}\n", + usedDeprecatedRules: [], +} diff --git a/packages/checker-eslint/__tests__/logger.spec.ts b/packages/checker-eslint/__tests__/logger.spec.ts new file mode 100644 index 00000000..0404b75e --- /dev/null +++ b/packages/checker-eslint/__tests__/logger.spec.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from 'vitest' + +import { normalizeEslintDiagnostic } from '../src/logger.js' +import { eslintResult1 } from './fixtures/eslintDiagnostic.js' + +describe('eslint logger', () => { + describe('normalizeEslintDiagnostic', () => { + it('get multiple diagnostics', () => { + const received = normalizeEslintDiagnostic(eslintResult1) + expect(received).toMatchSnapshot() + }) + }) +}) diff --git a/packages/vite-plugin-checker/__tests__/__snapshots__/vlsConfig.spec.ts.snap b/packages/checker-vls/__tests__/__snapshots__/vlsConfig.spec.ts.snap similarity index 97% rename from packages/vite-plugin-checker/__tests__/__snapshots__/vlsConfig.spec.ts.snap rename to packages/checker-vls/__tests__/__snapshots__/vlsConfig.spec.ts.snap index 801e4112..a0d7649d 100644 --- a/packages/vite-plugin-checker/__tests__/__snapshots__/vlsConfig.spec.ts.snap +++ b/packages/checker-vls/__tests__/__snapshots__/vlsConfig.spec.ts.snap @@ -1,4 +1,4 @@ -// Vitest Snapshot v1 +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`VLS config > customized config 1`] = ` { diff --git a/packages/vite-plugin-checker/__tests__/vlsConfig.spec.ts b/packages/checker-vls/__tests__/vlsConfig.spec.ts similarity index 79% rename from packages/vite-plugin-checker/__tests__/vlsConfig.spec.ts rename to packages/checker-vls/__tests__/vlsConfig.spec.ts index 89ca0272..6d3e3e94 100644 --- a/packages/vite-plugin-checker/__tests__/vlsConfig.spec.ts +++ b/packages/checker-vls/__tests__/vlsConfig.spec.ts @@ -1,9 +1,13 @@ import path from 'path' import { describe, expect, it } from 'vitest' -import { ShutdownRequest } from 'vscode-languageserver/node' +import { ShutdownRequest } from 'vscode-languageserver/node.js' import { URI } from 'vscode-uri' +import { fileURLToPath } from 'node:url' +import { dirname } from 'node:path' -import { prepareClientConnection, logLevel2Severity } from '../../checker-vls/src/diagnostics' +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) +import { prepareClientConnection, logLevel2Severity } from '../src/diagnostics.js' async function testVslConfig(overrideConfig?: any) { const workspaceUri = URI.file(path.join(__dirname, 'fixtures')) diff --git a/packages/vite-plugin-checker/__tests__/__snapshots__/logger.spec.ts.snap b/packages/vite-plugin-checker/__tests__/__snapshots__/logger.spec.ts.snap index 8c0c68d2..e1ddd070 100644 --- a/packages/vite-plugin-checker/__tests__/__snapshots__/logger.spec.ts.snap +++ b/packages/vite-plugin-checker/__tests__/__snapshots__/logger.spec.ts.snap @@ -1,4 +1,4 @@ -// Vitest Snapshot v1 +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`logger > diagnosticToTerminalLog > get error 1`] = ` " ERROR(ESLint) Unexpected var, use let or const instead. @@ -27,70 +27,3 @@ exports[`logger > diagnosticToTerminalLog > get warning 1`] = ` 6 | rootDom.innerHTML = hello + text " `; - -exports[`logger > normalizeEslintDiagnostic > get multiple diagnostics 1`] = ` -[ - { - "checker": "ESLint", - "codeFrame": "   1 | import { text } from './text' -   2 | - > 3 | var hello = 'Hello' -   | ^^^^^^^^^^^^^^^^^^^ -   4 | var hello1 = 'Hello1' -   5 | -   6 | const rootDom = document.querySelector('#root')!", - "conclusion": "", - "id": "/Users/vite-plugin-checker/playground/eslint/src/main.ts", - "level": 1, - "loc": { - "end": { - "column": 20, - "line": 3, - }, - "start": { - "column": 1, - "line": 3, - }, - }, - "message": "Unexpected var, use let or const instead. (no-var)", - "stripedCodeFrame": " 1 | import { text } from './text' - 2 | - > 3 | var hello = 'Hello' - | ^^^^^^^^^^^^^^^^^^^ - 4 | var hello1 = 'Hello1' - 5 | - 6 | const rootDom = document.querySelector('#root')!", - }, - { - "checker": "ESLint", - "codeFrame": "   2 | -   3 | var hello = 'Hello' - > 4 | var hello1 = 'Hello1' -   | ^^^^^^^^^^^^^^^^^^^^^ -   5 | -   6 | const rootDom = document.querySelector('#root')! -   7 | rootDom.innerHTML = hello + text", - "conclusion": "", - "id": "/Users/vite-plugin-checker/playground/eslint/src/main.ts", - "level": 1, - "loc": { - "end": { - "column": 22, - "line": 4, - }, - "start": { - "column": 1, - "line": 4, - }, - }, - "message": "Unexpected var, use let or const instead. (no-var)", - "stripedCodeFrame": " 2 | - 3 | var hello = 'Hello' - > 4 | var hello1 = 'Hello1' - | ^^^^^^^^^^^^^^^^^^^^^ - 5 | - 6 | const rootDom = document.querySelector('#root')! - 7 | rootDom.innerHTML = hello + text", - }, -] -`; diff --git a/packages/vite-plugin-checker/__tests__/fixtures/eslintDiagnostic.ts b/packages/vite-plugin-checker/__tests__/fixtures/eslintDiagnostic.ts index 2bd020e9..3c057913 100644 --- a/packages/vite-plugin-checker/__tests__/fixtures/eslintDiagnostic.ts +++ b/packages/vite-plugin-checker/__tests__/fixtures/eslintDiagnostic.ts @@ -1,5 +1,4 @@ import type { NormalizedDiagnostic } from './../../src/logger' -import type { ESLint } from 'eslint' export const error1: NormalizedDiagnostic = { message: 'Unexpected var, use let or const instead.', @@ -15,41 +14,3 @@ export const error1: NormalizedDiagnostic = { } export const warning1: NormalizedDiagnostic = { ...error1, level: 0 } - -export const eslintResult1: ESLint.LintResult = { - filePath: '/Users/vite-plugin-checker/playground/eslint/src/main.ts', - messages: [ - { - ruleId: 'no-var', - severity: 2, - message: 'Unexpected var, use let or const instead.', - line: 3, - column: 1, - nodeType: 'VariableDeclaration', - messageId: 'unexpectedVar', - endLine: 3, - endColumn: 20, - fix: { range: [31, 34], text: 'let' }, - }, - { - ruleId: 'no-var', - severity: 2, - message: 'Unexpected var, use let or const instead.', - line: 4, - column: 1, - nodeType: 'VariableDeclaration', - messageId: 'unexpectedVar', - endLine: 4, - endColumn: 22, - fix: { range: [51, 54], text: 'let' }, - }, - ], - errorCount: 2, - fatalErrorCount: 2, - warningCount: 0, - fixableErrorCount: 2, - fixableWarningCount: 0, - source: - "import { text } from './text'\n\nvar hello = 'Hello'\nvar hello1 = 'Hello1'\n\nconst rootDom = document.querySelector('#root')!\nrootDom.innerHTML = hello + text\n\nexport {}\n", - usedDeprecatedRules: [], -} diff --git a/packages/vite-plugin-checker/__tests__/fixtures/tsDiagnostic.ts b/packages/vite-plugin-checker/__tests__/fixtures/tsDiagnostic.ts deleted file mode 100644 index 7d7fe581..00000000 --- a/packages/vite-plugin-checker/__tests__/fixtures/tsDiagnostic.ts +++ /dev/null @@ -1,200 +0,0 @@ -export const diagnostic1 = { - file: { - // file: SourceFileObject { - // pos: 0, - // end: 1113, - // flags: 0, - // modifierFlagsCache: 0, - // transformFlags: 579, - // parent: undefined, - // kind: 298, - // statements: [ - // [NodeObject], - // [NodeObject], - // [NodeObject], - // [NodeObject], - // [NodeObject], - // pos: 0, - // end: 1112, - // hasTrailingComma: false, - // transformFlags: 579 - // ], - // endOfFileToken: TokenObject { - // pos: 1112, - // end: 1113, - // flags: 0, - // modifierFlagsCache: 0, - // transformFlags: 0, - // parent: [Circular *1], - // kind: 1 - // }, - fileName: '/Users/fi3ework/vite-plugin-checker/playground/react-ts/src/App.tsx', - text: - "import React, { useState } from 'react'\n" + - "import logo from './logo.svg'\n" + - "import './App.css'\n" + - '\n' + - 'function App() {\n' + - ' // Try change to and the overlay will gone.\n' + - ' const [count, setCount] = useState(1)\n' + - ' return (\n' + - '
\n' + - '
\n' + - ' logo\n' + - '

Hello Vite + React!

\n' + - '

\n' + - ' \n' + - '

\n' + - '

\n' + - ' Edit App.tsx and save to test HMR updates.\n' + - '

\n' + - '

\n' + - ' \n' + - ' Learn React\n' + - ' \n' + - " {' | '}\n" + - ' \n' + - ' Vite Docs\n' + - ' \n' + - '

\n' + - '
\n' + - '
\n' + - ' )\n' + - '}\n' + - '\n' + - 'export default App\n', - languageVersion: 99, - languageVariant: 1, - scriptKind: 4, - isDeclarationFile: false, - hasNoDefaultLib: false, - // externalModuleIndicator: NodeObject { - // pos: 0, - // end: 39, - // flags: 0, - // modifierFlagsCache: 536870912, - // transformFlags: 0, - // parent: [Circular *1], - // kind: 262, - // decorators: undefined, - // modifiers: undefined, - // symbol: undefined, - // localSymbol: undefined, - // locals: undefined, - // nextContainer: undefined, - // importClause: [NodeObject], - // moduleSpecifier: [TokenObject] - // }, - bindDiagnostics: [], - bindSuggestionDiagnostics: undefined, - // pragmas: Map(0) {}, - checkJsDirective: undefined, - referencedFiles: [], - typeReferenceDirectives: [], - libReferenceDirectives: [], - amdDependencies: [], - commentDirectives: undefined, - nodeCount: 177, - identifierCount: 48, - // identifiers: Map(23) { - // 'React' => 'React', - // 'useState' => 'useState', - // 'react' => 'react', - // 'logo' => 'logo', - // './logo.svg' => './logo.svg', - // './App.css' => './App.css', - // 'App' => 'App', - // 'count' => 'count', - // 'setCount' => 'setCount', - // 'div' => 'div', - // 'className' => 'className', - // 'header' => 'header', - // 'img' => 'img', - // 'src' => 'src', - // 'alt' => 'alt', - // 'p' => 'p', - // 'button' => 'button', - // 'onClick' => 'onClick', - // 'code' => 'code', - // 'a' => 'a', - // 'href' => 'href', - // 'target' => 'target', - // 'rel' => 'rel' - // }, - parseDiagnostics: [], - version: 'e38e5afa683ca0a1613c6d335a73db12ae58b69c4555ac645ccc8f6a15dce3d6', - path: '/users/fi3ework/vite-plugin-checker/playground/react-ts/src/app.tsx', - resolvedPath: '/users/fi3ework/vite-plugin-checker/playground/react-ts/src/app.tsx', - originalFileName: '/Users/fi3ework/vite-plugin-checker/playground/react-ts/src/App.tsx', - // imports: [ [TokenObject], [TokenObject], [TokenObject] ], - moduleAugmentations: [], - ambientModuleNames: [], - // resolvedModules: Map(3) { - // 'react' => [Object], - // './logo.svg' => undefined, - // './App.css' => undefined - // }, - // symbol: SymbolObject { - // flags: 512, - // escapedName: '"/Users/fi3ework/vite-plugin-checker/playground/react-ts/src/App"', - // declarations: [Array], - // exports: [Map], - // valueDeclaration: [Circular *1], - // id: 7114 - // }, - // locals: Map(4) { - // 'App' => [SymbolObject], - // 'React' => [SymbolObject], - // 'useState' => [SymbolObject], - // 'logo' => [SymbolObject] - // }, - // nextContainer: NodeObject { - // pos: 88, - // end: 1092, - // flags: 0, - // modifierFlagsCache: 536870912, - // transformFlags: 2228803, - // parent: [Circular *1], - // kind: 252, - // decorators: undefined, - // modifiers: undefined, - // symbol: [SymbolObject], - // localSymbol: undefined, - // locals: [Map], - // nextContainer: [NodeObject], - // name: [IdentifierObject], - // typeParameters: undefined, - // parameters: [Array], - // type: undefined, - // body: [NodeObject], - // asteriskToken: undefined, - // id: 14743 - // }, - endFlowNode: { flags: 2 }, - symbolCount: 36, - // classifiableNames: Set(4) { 'React', 'useState', 'logo', 'default' }, - id: 14728, - lineMap: [ - 0, 40, 70, 89, 90, 107, 171, 219, 230, 256, 294, 353, 388, 400, 492, 505, 517, 583, 596, 608, - 621, 654, 693, 721, 759, 771, 795, 810, 828, 841, 874, 932, 960, 998, 1010, 1032, 1047, 1060, - 1076, 1087, 1091, 1093, 1094, 1113, - ], - }, - start: 216, - length: 1, - code: 2345, - category: 1, - messageText: - "Argument of type 'number' is not assignable to parameter of type 'string | (() => string)'.", - relatedInformation: undefined, -} diff --git a/packages/vite-plugin-checker/__tests__/logger.spec.ts b/packages/vite-plugin-checker/__tests__/logger.spec.ts index a292d53e..1dcaba9d 100644 --- a/packages/vite-plugin-checker/__tests__/logger.spec.ts +++ b/packages/vite-plugin-checker/__tests__/logger.spec.ts @@ -1,12 +1,8 @@ import { describe, expect, it } from 'vitest' import strip from 'strip-ansi' -import { diagnosticToTerminalLog, normalizeEslintDiagnostic } from '../src/logger' -import { - error1 as eslintError1, - warning1 as eslintWarning1, - eslintResult1, -} from './fixtures/eslintDiagnostic' +import { diagnosticToTerminalLog } from '../src/logger' +import { error1 as eslintError1, warning1 as eslintWarning1 } from './fixtures/eslintDiagnostic' describe('logger', () => { describe('diagnosticToTerminalLog', () => { @@ -20,11 +16,4 @@ describe('logger', () => { expect(received).toMatchSnapshot() }) }) - - describe('normalizeEslintDiagnostic', () => { - it('get multiple diagnostics', () => { - const received = normalizeEslintDiagnostic(eslintResult1) - expect(received).toMatchSnapshot() - }) - }) })