Skip to content

Commit

Permalink
test: 尝试使用 vitest 进行集成测试
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Feb 11, 2024
1 parent c0d4541 commit ba42b09
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 56 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
132 changes: 132 additions & 0 deletions src/test/suite/displayLogMessage.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
53 changes: 0 additions & 53 deletions src/test/suite/extension.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
Expand Down

0 comments on commit ba42b09

Please sign in to comment.