Skip to content

Commit

Permalink
test: 单元测试提取公共函数
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Mar 30, 2024
1 parent 51f9e47 commit b8c0ad3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 60 deletions.
4 changes: 1 addition & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 100
max_line_length = 120
13 changes: 13 additions & 0 deletions src/test/globalMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { vi } from 'vitest';

export function mockLogger() {
return {
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
};
}
21 changes: 3 additions & 18 deletions src/test/unit/commands/commentAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { commentAllLogMessagesCommand } from '@/commands/commentAllLogMessages';
import { ExtensionProperties } from '@/typings';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
Position,
Range,
Selection,
TextDocument,
TextEditor,
TextEditorEdit,
window,
} from 'vscode';
import { Position, Range, Selection, TextDocument, TextEditor, TextEditorEdit, window } from 'vscode';

vi.mock('vscode');
vi.mock('@/extension', () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
const { mockLogger } = await vi.hoisted(() => import('@/test/globalMocks'));
vi.mock('@/extension', mockLogger);

describe('commentAllLogMessagesCommand', () => {
let mockEditor: TextEditor | undefined;
Expand Down
11 changes: 2 additions & 9 deletions src/test/unit/commands/deleteAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ import {
} from 'vscode';

vi.mock('vscode');
vi.mock('@/extension', () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
const { mockLogger } = await vi.hoisted(() => import('@/test/globalMocks'));
vi.mock('@/extension', mockLogger);

describe('deleteAllLogMessagesCommand', () => {
let mockEditor: TextEditor | undefined;
Expand Down
11 changes: 2 additions & 9 deletions src/test/unit/commands/displayLogMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ import {
} from 'vscode';

vi.mock('vscode');
vi.mock('@/extension', () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
const { mockLogger } = await vi.hoisted(() => import('@/test/globalMocks'));
vi.mock('@/extension', mockLogger);

describe('displayLogMessageCommand', () => {
let mockEditor: TextEditor | undefined;
Expand Down
11 changes: 2 additions & 9 deletions src/test/unit/commands/uncommentAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ import {
} from 'vscode';

vi.mock('vscode');
vi.mock('@/extension', () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
const { mockLogger } = await vi.hoisted(() => import('@/test/globalMocks'));
vi.mock('@/extension', mockLogger);

describe('uncommentAllLogMessagesCommand', () => {
let mockEditor: TextEditor | undefined;
Expand Down
11 changes: 2 additions & 9 deletions src/test/unit/commands/updateLineNumAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ import {
} from 'vscode';

vi.mock('vscode');
vi.mock('@/extension', () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
const { mockLogger } = await vi.hoisted(() => import('@/test/globalMocks'));
vi.mock('@/extension', mockLogger);

describe('updateLineNumAllLogMessagesCommand', () => {
let mockEditor: TextEditor | undefined;
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"module": "ESNext",
"target": "ESNext",
"outDir": "out",
"lib": ["es2020"],
"lib": ["ESNext"],
"sourceMap": true,
"rootDir": "src",
"baseUrl": "./",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"skipLibCheck": true,
"paths": {
Expand Down

0 comments on commit b8c0ad3

Please sign in to comment.