Skip to content

Commit 1dd6915

Browse files
authored
refactor(tests): use jest-mock-vscode and reorganize test structure (#45)
* refactor: re-export from VSCodeMock # Conflicts: # tests/__mocks__/vscode.ts # tests/utils/resolve.test.ts * chore: update * test: use real filesystem instead of custom mock * ci: try fix test in windows * chore: update
1 parent 52e8781 commit 1dd6915

7 files changed

Lines changed: 38 additions & 122 deletions

File tree

tests/__mocks__/filesystem.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/__mocks__/vscode.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ import { vi } from 'vitest'
33

44
const vscode = createVSCodeMock(vi)
55

6-
export const Uri = vscode.Uri
7-
export const workspace = vscode.workspace
8-
export const Range = vscode.Range
9-
export const Position = vscode.Position
10-
export const Location = vscode.Location
11-
export const Selection = vscode.Selection
12-
export const CodeAction = vscode.CodeAction
13-
export const CodeActionKind = vscode.CodeActionKind
14-
export const WorkspaceEdit = vscode.WorkspaceEdit
15-
export const ThemeColor = vscode.ThemeColor
16-
export const ThemeIcon = vscode.ThemeIcon
17-
export const TreeItem = vscode.TreeItem
18-
export const TreeItemCollapsibleState = vscode.TreeItemCollapsibleState
19-
export const Disposable = vscode.Disposable
6+
export const {
7+
Uri,
8+
workspace,
9+
Range,
10+
Position,
11+
Location,
12+
Selection,
13+
ThemeColor,
14+
ThemeIcon,
15+
TreeItem,
16+
TreeItemCollapsibleState,
17+
Disposable,
18+
MarkdownString,
19+
CompletionItem,
20+
CompletionItemKind,
21+
CodeAction,
22+
CodeActionKind,
23+
WorkspaceEdit,
24+
DiagnosticSeverity,
25+
DiagnosticTag,
26+
window,
27+
languages,
28+
} = vscode
29+
2030
export default vscode

tests/filesystem.test.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
2-
import { memoize } from '../src/utils/memoize'
2+
import { memoize } from '../../src/utils/memoize'
33

44
describe('memoize', () => {
55
it('should cache sync function result', () => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { encodePackageName } from '../src/utils/package'
2+
import { encodePackageName } from '../../src/utils/package'
33

44
describe('encodePackageName', () => {
55
it('should encode regular package name', () => {
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
22
import { Uri } from 'vscode'
3-
import { findNearestFile, walkAncestors } from '../src/utils/resolve'
4-
import { mockFileSystem } from './__mocks__/filesystem'
3+
import { findNearestFile, walkAncestors } from '../../src/utils/resolve'
4+
5+
const root = Uri.file(process.cwd())
56

67
describe('walkAncestors', () => {
78
it('should yield all ancestor directories', () => {
@@ -34,44 +35,26 @@ describe('walkAncestors', () => {
3435
})
3536

3637
describe('findNearestFile', () => {
37-
beforeEach(() => {
38-
vi.resetAllMocks()
39-
})
40-
4138
it('should find a file in a parent directory', async () => {
42-
mockFileSystem({
43-
'/a/b/target.txt': '',
44-
})
45-
46-
const result = await findNearestFile('target.txt', Uri.file('/a/b/c/d'))
39+
const result = await findNearestFile('package.json', Uri.joinPath(root, 'src/utils'))
4740
expect(result).toBeDefined()
48-
expect(result!.path).toBe('/a/b/target.txt')
41+
expect(result!.fsPath).toBe(Uri.joinPath(root, 'package.json').fsPath)
4942
})
5043

5144
it('should return the closest match', async () => {
52-
mockFileSystem({
53-
'/a/target.txt': '',
54-
'/a/b/c/target.txt': '',
55-
})
56-
57-
const result = await findNearestFile('target.txt', Uri.file('/a/b/c/d'))
45+
const result = await findNearestFile('package.json', Uri.joinPath(root, 'playground'))
5846
expect(result).toBeDefined()
59-
expect(result!.path).toBe('/a/b/c/target.txt')
47+
expect(result!.fsPath).toBe(Uri.joinPath(root, 'playground/package.json').fsPath)
6048
})
6149

6250
it('should return undefined when file is not found', async () => {
63-
mockFileSystem({})
64-
65-
const result = await findNearestFile('target.txt', Uri.file('/a/b/c'))
51+
const result = await findNearestFile('__nonexistent_file__', Uri.joinPath(root, 'src'))
6652
expect(result).toBeUndefined()
6753
})
6854

6955
it('should respect shouldStop', async () => {
70-
mockFileSystem({
71-
'/a/target.txt': '',
72-
})
73-
74-
const result = await findNearestFile('target.txt', Uri.file('/a/b/c'), (u) => u.path === '/a/b')
56+
const stop = Uri.joinPath(root, 'src')
57+
const result = await findNearestFile('package.json', Uri.joinPath(root, 'src/utils'), (u) => u.fsPath === stop.fsPath)
7558
expect(result).toBeUndefined()
7659
})
7760
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { getPrereleaseId, lt, parseVersion } from '../src/utils/version'
2+
import { getPrereleaseId, lt, parseVersion } from '../../src/utils/version'
33

44
describe('parseVersion', () => {
55
it('should parse plain version', () => {

0 commit comments

Comments
 (0)