Skip to content

Commit cde7850

Browse files
committed
test: optimize the use of just-mock-vscode
1 parent 5c8f7e6 commit cde7850

12 files changed

Lines changed: 52 additions & 82 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { vi } from 'vitest'
33

44
const vscode = createVSCodeMock(vi)
55

6-
module.exports = { ...vscode }
6+
module.exports = vscode

tests/api/run.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
33
import { TestRunner } from '../../src/api/run.js'
44
import { TEST_ID_SEPARATOR } from '../../src/constants.js'
55
import { createTestItem } from '../utils.js'
6-
// import type * as vscode from 'vscode'
6+
77
import type { WdioExtensionWorkerInterface } from '../../src/api/types.js'
88
import type { ResultSet } from '../../src/reporter/types.js'
99

1010
// Mock dependencies
11-
vi.mock('vscode', () => {
12-
return {}
13-
})
11+
vi.mock('vscode', () => import('../__mocks__/vscode.cjs'))
1412

1513
vi.mock('../../src/utils/logger', () => ({
1614
log: {

tests/api/utils.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { loggingFn } from '../../src/api/utils.js'
44
import { LOG_LEVEL } from '../../src/constants.js'
55
import { log } from '../../src/utils/logger.js'
66

7-
vi.mock('vscode', () => {
8-
return {}
9-
})
7+
vi.mock('vscode', () => import('../__mocks__/vscode.cjs'))
108

119
vi.mock('../../src/utils/logger.js', () => ({
1210
log: {

tests/api/worker.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type * as WebSocket from 'ws'
1212
import type { ExtensionApi } from '../../src/api/types.js'
1313

1414
// Mock dependencies
15-
vi.mock('vscode')
1615
vi.mock('node:child_process')
1716
vi.mock('node:http')
1817
vi.mock('ws', () => {

tests/config/index.test.ts

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ import { ExtensionConfigManager } from '../../src/config/index.js'
66
import { DEFAULT_CONFIG_VALUES, EXTENSION_ID } from '../../src/constants.js'
77
import { log } from '../../src/utils/logger.js'
88

9+
import type { MockWorkspace } from 'jest-mock-vscode'
10+
911
// Mock dependencies
10-
vi.mock('vscode', () => {
11-
return {
12-
workspace: {
13-
getConfiguration: vi.fn(),
14-
workspaceFolders: undefined,
15-
},
16-
ConfigurationChangeEvent: vi.fn(),
17-
WorkspaceFolder: vi.fn(),
18-
EventEmitter: vi.fn(),
19-
Disposable: vi.fn(),
20-
}
21-
})
12+
vi.mock('vscode', async () => import('../__mocks__/vscode.cjs'))
2213
vi.mock('../../src/config/find.js')
2314
vi.mock('../../src/config/watcher.js', () => {
2415
return {}
@@ -180,9 +171,6 @@ describe('ExtensionConfigManager', () => {
180171

181172
describe('initialize', () => {
182173
it('should return empty array when no workspace folders', async () => {
183-
// Setup
184-
Object.defineProperty(vscode.workspace, 'workspaceFolders', { value: [] })
185-
186174
// Execute
187175
const result = await configManager.initialize()
188176

@@ -194,12 +182,9 @@ describe('ExtensionConfigManager', () => {
194182

195183
it('should initialize workspaces and find config files', async () => {
196184
// Setup
197-
const mockWorkspaceFolder1 = { uri: { fsPath: '/workspace1' } } as vscode.WorkspaceFolder
198-
const mockWorkspaceFolder2 = { uri: { fsPath: '/workspace2' } } as vscode.WorkspaceFolder
199-
200-
Object.defineProperty(vscode.workspace, 'workspaceFolders', {
201-
value: [mockWorkspaceFolder1, mockWorkspaceFolder2],
202-
})
185+
const mockWorkspaceFolder1 = { uri: vscode.Uri.file('/workspace1') } as vscode.WorkspaceFolder
186+
const mockWorkspaceFolder2 = { uri: vscode.Uri.file('/workspace2') } as vscode.WorkspaceFolder
187+
;(vscode.workspace as MockWorkspace).setWorkspaceFolders([mockWorkspaceFolder1, mockWorkspaceFolder2])
203188

204189
const wdioConfigFiles1 = ['/workspace1/wdio.conf.js', '/workspace1/wdio.e2e.conf.js']
205190
const wdioConfigFiles2 = ['/workspace2/wdio.conf.js']
@@ -229,8 +214,9 @@ describe('ExtensionConfigManager', () => {
229214

230215
it('should handle workspaces with no config files', async () => {
231216
// Setup
232-
const mockWorkspaceFolder = { uri: { fsPath: '/workspace' } } as vscode.WorkspaceFolder
233-
Object.defineProperty(vscode.workspace, 'workspaceFolders', { value: [mockWorkspaceFolder] })
217+
const mockWorkspaceFolder = { uri: vscode.Uri.file('/workspace') } as vscode.WorkspaceFolder
218+
;(vscode.workspace as MockWorkspace).setWorkspaceFolders([mockWorkspaceFolder])
219+
234220
vi.mocked(findWdioConfig).mockResolvedValueOnce([])
235221

236222
// Execute
@@ -255,8 +241,8 @@ describe('ExtensionConfigManager', () => {
255241

256242
it('should return all config paths after initialization', async () => {
257243
// Setup
258-
const mockWorkspaceFolder = { uri: { fsPath: '/workspace' } } as vscode.WorkspaceFolder
259-
Object.defineProperty(vscode.workspace, 'workspaceFolders', { value: [mockWorkspaceFolder] })
244+
const mockWorkspaceFolder = { uri: vscode.Uri.file('/workspace') } as vscode.WorkspaceFolder
245+
;(vscode.workspace as MockWorkspace).setWorkspaceFolders([mockWorkspaceFolder])
260246

261247
const wdioConfigFiles = ['/workspace/wdio.conf.js', '/workspace/wdio.e2e.conf.js']
262248
vi.mocked(findWdioConfig).mockResolvedValueOnce(wdioConfigFiles)
@@ -279,13 +265,6 @@ describe('ExtensionConfigManager', () => {
279265

280266
describe('dispose', () => {
281267
it('should clear workspaces data', async () => {
282-
// Setup
283-
const mockWorkspaceFolder = { uri: { fsPath: '/workspace' } } as vscode.WorkspaceFolder
284-
Object.defineProperty(vscode.workspace, 'workspaceFolders', { value: [mockWorkspaceFolder] })
285-
286-
const wdioConfigFiles = ['/workspace/wdio.conf.js']
287-
vi.mocked(findWdioConfig).mockResolvedValueOnce(wdioConfigFiles)
288-
289268
await configManager.initialize()
290269
expect(configManager.workspaces).toBeDefined()
291270

tests/config/watcher.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { normalize } from 'node:path'
22

33
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
4+
import * as vscode from 'vscode'
45

56
import { ConfigFileWatcher } from '../../src/config/watcher.js'
67
import { FileWatcherManager } from '../../src/utils/watcher.js'
7-
import type * as vscode from 'vscode'
88
import type { ServerManager } from '../../src/api/index.js'
99
import type { ExtensionConfigManager } from '../../src/config/index.js'
1010
import type { RepositoryManager } from '../../src/test/index.js'
1111

12-
vi.mock('vscode')
12+
vi.mock('vscode', async () => await import('../__mocks__/vscode.cjs'))
1313

1414
// Mock dependencies
1515
vi.mock('../../src/utils/logger.js', () => ({
@@ -20,13 +20,13 @@ vi.mock('../../src/utils/logger.js', () => ({
2020

2121
// Mock functions from test/index.js
2222
vi.mock('../../src/test/index.js', () => {
23-
const TestfileWatcher=vi.fn()
23+
const TestfileWatcher = vi.fn()
2424
TestfileWatcher.prototype.enable = vi.fn()
2525
TestfileWatcher.prototype.refreshWatchers = vi.fn()
26-
return ({
26+
return {
2727
TestfileWatcher,
2828
convertUriToPath: vi.fn((uri) => uri.fsPath),
29-
})
29+
}
3030
})
3131

3232
// Mock normalizePath function
@@ -58,9 +58,7 @@ describe('ConfigFileWatcher', () => {
5858
vi.clearAllMocks()
5959

6060
// Create mock URI
61-
mockUri = {
62-
fsPath: '/path/to/wdio.conf.js',
63-
} as unknown as vscode.Uri
61+
mockUri = vscode.Uri.file('/path/to/wdio.conf.js')
6462

6563
// Create mock repositories
6664
mockRepo1 = {
@@ -150,7 +148,7 @@ describe('ConfigFileWatcher', () => {
150148
const result = watcher['getFilePatterns']()
151149

152150
// Verify
153-
expect(result).toEqual([{ pattern:'**/wdio.conf.{js,ts}' }])
151+
expect(result).toEqual([{ pattern: '**/wdio.conf.{js,ts}' }])
154152
})
155153

156154
it('should return empty array if no patterns are defined', () => {

tests/test/reporter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import type { TestRepository } from '../../src/test/repository.js'
99

1010
// Mock dependencies
1111
vi.mock('vscode', async () => {
12-
const mockModule = await import('../__mocks__/vscode.js')
12+
const mockModule = await import('../__mocks__/vscode.cjs')
1313
return {
14-
TestMessage: vi.fn(),
1514
...mockModule,
15+
TestMessage: vi.fn(),
1616
}
1717
})
1818
vi.mock('../../src/test/repository')

tests/test/runHandler.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import type { ExtensionConfigManager } from '../../src/config/index.js'
1111
import type { RepositoryManager } from '../../src/test/index.js'
1212

1313
vi.mock('vscode', async () => {
14+
const mockVscode = await import('../__mocks__/vscode.cjs')
1415
return {
16+
...mockVscode,
1517
TestMessage: vi.fn(),
1618
}
1719
})

tests/test/utils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import type * as vscode from 'vscode'
66
import type { RepositoryManager } from '../../src/test/manager.js'
77

88
vi.mock('vscode', async () => {
9+
const mockVscode = await import('../__mocks__/vscode.cjs')
910
return {
11+
...mockVscode,
1012
TestRunProfileKind: {
1113
Run: 1,
1214
},

tests/test/watcher.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import { FileWatcherManager } from '../../src/utils/watcher.js'
88
import type * as vscode from 'vscode'
99
import type { RepositoryManager } from '../../src/test/manager.js'
1010

11-
vi.mock('vscode', () => {
12-
return {
13-
Uri: {
14-
file: vi.fn((f) => ({ fsPath: f })),
15-
},
16-
}
17-
})
11+
vi.mock('vscode', () => import('../__mocks__/vscode.cjs'))
1812

1913
// Mock dependencies
2014
vi.mock('../../src/utils/logger.js', () => ({

0 commit comments

Comments
 (0)