From ba42b09056c40495ea497f6a67abc98b772c3c7d Mon Sep 17 00:00:00 2001 From: ygqygq2 Date: Sun, 11 Feb 2024 23:11:21 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=B0=9D=E8=AF=95=E4=BD=BF=E7=94=A8=20?= =?UTF-8?q?vitest=20=E8=BF=9B=E8=A1=8C=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ygqygq2 --- package.json | 5 +- src/test/suite/displayLogMessage.test.ts | 132 +++++++++++++++++++++++ src/test/suite/extension.test.ts | 53 --------- vite.config.ts | 2 +- 4 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 src/test/suite/displayLogMessage.test.ts delete mode 100644 src/test/suite/extension.test.ts diff --git a/package.json b/package.json index 5c30d6e..aee6214 100644 --- a/package.json +++ b/package.json @@ -152,8 +152,9 @@ "package": "vsce package --no-dependencies", "publish": "vsce publish --no-dependencies", "test": "tsc -p ./ && vscode-test", - "test:suite": "tsc -p ./ && node out/test/runTests.js", - "test:unit": "vitest --watch=false", + "test:suite:mocha": "tsc -p ./ && node out/test/runTests.js", + "test:suite": "vitest suite --watch=false", + "test:unit": "vitest unit --watch=false", "test:coverage": "vitest run --coverage" }, "devDependencies": { diff --git a/src/test/suite/displayLogMessage.test.ts b/src/test/suite/displayLogMessage.test.ts new file mode 100644 index 0000000..31e64f1 --- /dev/null +++ b/src/test/suite/displayLogMessage.test.ts @@ -0,0 +1,132 @@ +import { + Position, + Range, + Selection, + TextDocument, + TextEditor, + TextEditorEdit, + window, +} from 'vscode'; +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; +import { displayLogMessageCommand } from '@/commands/displayLogMessage'; +import { ExtensionProperties } from '@/typings'; + +vi.mock('vscode'); + +describe('displayLogMessageCommand', () => { + let mockEditor: TextEditor | undefined; + let mockDocument: TextDocument; + let mockSelection: Selection; + let mockSelections: Selection[]; + let mockEditBuilder: TextEditorEdit; + let mockExtensionProperties: ExtensionProperties; + + beforeEach(() => { + mockExtensionProperties = { + wrapLogMessage: false, + logMessagePrefix: '🚀', + logMessageSuffix: ':', + addSemicolonInTheEnd: true, + insertEmptyLineBeforeLogMessage: false, + insertEmptyLineAfterLogMessage: false, + quote: '"', + delimiterInsideMessage: '~', + includeFileNameAndLineNum: true, + logFunction: {}, + }; + + mockDocument = { + getWordRangeAtPosition: vi + .fn() + .mockReturnValue(new Range(new Position(0, 0), new Position(0, 0))) + .mockReturnValueOnce(new Range(new Position(0, 0), new Position(0, 4))), + getText: vi.fn((range: Range): string => { + if (range.isEmpty) { + return ''; // 未选中字符串,返回空字符串 + } + return 'myVar'; + }), + lineAt: vi.fn().mockImplementation((lineNumber: number) => { + return { + text: 'myVar', // 模拟行的文本内容 + firstNonWhitespaceCharacterIndex: 0, // 模拟行的第一个非空格字符的索引 + range: { + start: { line: lineNumber, character: 0 }, // 模拟行的起始位置 + end: { line: lineNumber, character: 10 }, // 模拟行的结束位置 + }, + }; + }), + lineCount: 10, // 模拟文档的行数 + fileName: 'test.js', + } as unknown as TextDocument; + + mockSelection = new Selection(new Position(0, 0), new Position(0, 4)); + mockSelections = [mockSelection]; + + mockEditBuilder = { + insert: vi.fn(), + } as unknown as TextEditorEdit; + + mockEditor = { + document: mockDocument, + selections: mockSelections, + edit: vi.fn((callback) => { + callback(mockEditBuilder); + return Promise.resolve(true); // 返回一个解析为 true 的 Promise + }), + options: { tabSize: 2 }, + } as unknown as TextEditor; + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('应该直接返回,当没有活动编辑器时', async () => { + window.activeTextEditor = undefined; + await displayLogMessageCommand().handler(mockExtensionProperties); + expect(mockEditor!.edit).not.toHaveBeenCalled(); + }); + + it('应该返回调用,当选中字符串时', async () => { + window.activeTextEditor = mockEditor; + await displayLogMessageCommand().handler(mockExtensionProperties); + expect(mockEditor!.edit).toHaveBeenCalledTimes(1); + expect(mockEditor!.edit).toHaveBeenCalledWith(expect.any(Function)); + + expect(mockEditBuilder.insert).toHaveBeenCalledTimes(1); + expect(mockEditBuilder.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String)); + }); + + it('应该插入调试日志,未选中,但光标放在变量名上时', async () => { + const mockSelection = new Selection(new Position(0, 0), new Position(0, 0)); + mockSelections = [mockSelection]; + window.activeTextEditor = { + ...mockEditor, + ...{ + ...mockDocument, + selections: mockSelections, + }, + } as unknown as TextEditor; + + await displayLogMessageCommand().handler(mockExtensionProperties); + expect(mockEditor!.edit).toHaveBeenCalledTimes(1); + expect(mockEditor!.edit).toHaveBeenCalledWith(expect.any(Function)); + expect(mockEditBuilder.insert).toHaveBeenCalledTimes(1); + expect(mockEditBuilder.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String)); + }); + + it('应该直接返回,当没有选中字符时和光标没有在字符串旁边时', async () => { + mockSelections = []; + window.activeTextEditor = { + ...mockEditor, + ...{ + ...mockDocument, + selections: mockSelections, + }, + } as unknown as TextEditor; + + await displayLogMessageCommand().handler(mockExtensionProperties); + expect(mockEditor!.edit).not.toHaveBeenCalled(); + }); +}); diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts deleted file mode 100644 index 2da9c04..0000000 --- a/src/test/suite/extension.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { displayLogMessageCommand } from '@/commands/displayLogMessage'; -import { ExtensionProperties } from '@/typings/extension/types'; -import { test, suite, expect } from 'vitest'; -import { Position, TextEditor, window, workspace } from 'vscode'; - -suite.todo('Extension Test Suite', () => { - let mockExtensionProperties: ExtensionProperties; - let testEditor: TextEditor; - suiteTeardown(() => { - window.showInformationMessage('All tests done!'); - }); - - beforeEach(async () => { - mockExtensionProperties = { - wrapLogMessage: false, - logMessagePrefix: '🚀', - logMessageSuffix: ':', - addSemicolonInTheEnd: true, - insertEmptyLineBeforeLogMessage: false, - insertEmptyLineAfterLogMessage: false, - quote: '"', - delimiterInsideMessage: '~', - includeFileNameAndLineNum: true, - logFunction: {}, - }; - - testEditor = await workspace.openTextDocument({ - content: '// sample content', - }); - testEditor = await window.showTextDocument(testEditor); - }); - - afterEach(async () => { - await testEditor.dispose(); - }); - - test.todo('Display Log Message', async () => { - await displayLogMessageCommand().handler(mockExtensionProperties); - - expect(window.activeTextEditor?.edit).toHaveBeenCalled(); - - expect(mockEditor.edit).toHaveBeenCalledWith(expect.any(Function)); - expect(mockEditBuilder.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String)); - }); - - test.todo('Comment All Log Message', () => {}); - - test.todo('Uncomment All Log Message', () => {}); - - test.todo('Delete All Log Message', () => {}); - - test.todo('Update Line Number For All Log Messages', async () => {}); -}); diff --git a/vite.config.ts b/vite.config.ts index f1bd8b6..7bec59b 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -17,7 +17,7 @@ export default defineConfig({ ], }, test: { - include: ['src/test/unit/**/*.spec.ts'], + include: ['src/test/unit/**/*.spec.ts', 'src/test/suite/**/*.test.ts'], coverage: { exclude: ['node_modules', 'out', 'src/test', 'src/typings', '.vscode-test'], },