11import fs from 'node:fs/promises'
22import path from 'node:path'
33
4- import { glob } from 'glob'
54import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest'
5+ import * as vscode from 'vscode'
66
77import { findWdioConfig } from '../src/find.js'
88
99// Mock dependencies
1010vi . mock ( 'node:fs/promises' )
1111vi . mock ( 'glob' )
1212
13- vi . mock ( 'vscode' , async ( ) => import ( '../../../tests/__mocks__/vscode.cjs' ) )
13+ vi . mock ( 'vscode' , async ( ) => {
14+ const vscode = await import ( '../../../tests/__mocks__/vscode.cjs' )
15+ return {
16+ ...vscode ,
17+ RelativePattern : vi . fn ( ) ,
18+ workspace : {
19+ findFiles : vi . fn ( ) ,
20+ } ,
21+ }
22+ } )
1423vi . mock ( '@vscode-wdio/logger' , ( ) => import ( '../../../tests/__mocks__/logger.js' ) )
1524
1625describe ( 'findWdioConfig' , ( ) => {
@@ -19,6 +28,13 @@ describe('findWdioConfig', () => {
1928
2029 beforeEach ( ( ) => {
2130 vi . resetAllMocks ( )
31+ vi . mocked ( vscode . RelativePattern ) . mockImplementation ( function ( this : any , r : unknown , p : string ) {
32+ this . path = p
33+ this . root = r
34+ this . toString = function ( ) {
35+ return path . join ( this . root , this . path )
36+ }
37+ } as any )
2238 } )
2339
2440 afterEach ( ( ) => {
@@ -30,9 +46,9 @@ describe('findWdioConfig', () => {
3046 const mockConfigFiles = [ 'wdio.conf.js' , 'wdio.conf.ts' ]
3147 const expectedPaths = mockConfigFiles . map ( ( file ) => path . join ( mockWorkspaceRoot , file ) )
3248
33- // Mock glob to return the config files
34- vi . mocked ( glob ) . mockResolvedValue ( mockConfigFiles )
35-
49+ vi . mocked ( vscode . workspace . findFiles ) . mockImplementation ( async ( p : vscode . GlobPattern ) => {
50+ return [ vscode . Uri . file ( p . toString ( ) ) ]
51+ } )
3652 // Mock fs.access to succeed for all files
3753 vi . mocked ( fs . access ) . mockResolvedValue ( undefined )
3854
@@ -41,21 +57,16 @@ describe('findWdioConfig', () => {
4157
4258 // Verify
4359 expect ( result ) . toEqual ( expectedPaths )
44- expect ( glob ) . toHaveBeenCalledWith ( defaultConfigFilePattern , {
45- cwd : mockWorkspaceRoot ,
46- withFileTypes : false ,
47- ignore : '**/node_modules/**' ,
48- } )
4960 expect ( fs . access ) . toHaveBeenCalledTimes ( 2 )
5061 } )
5162
5263 it ( 'should filter out inaccessible files' , async ( ) => {
5364 // Setup
54- const mockConfigFiles = [ 'wdio.conf.js' , 'wdio.conf.ts' ]
5565 const accessibleFile = path . join ( mockWorkspaceRoot , 'wdio.conf.js' )
5666
57- // Mock glob to return the config files
58- vi . mocked ( glob ) . mockResolvedValue ( mockConfigFiles )
67+ vi . mocked ( vscode . workspace . findFiles ) . mockImplementation ( async ( p : vscode . GlobPattern ) => {
68+ return [ vscode . Uri . file ( p . toString ( ) ) ]
69+ } )
5970
6071 // Mock fs.access to succeed only for the JS file
6172 vi . mocked ( fs . access ) . mockImplementation ( async ( filePath ) => {
@@ -75,10 +86,9 @@ describe('findWdioConfig', () => {
7586
7687 it ( 'should return empty array when no config files are accessible' , async ( ) => {
7788 // Setup
78- const mockConfigFiles = [ 'wdio.conf.js' , 'wdio.conf.ts' ]
79-
80- // Mock glob to return the config files
81- vi . mocked ( glob ) . mockResolvedValue ( mockConfigFiles )
89+ vi . mocked ( vscode . workspace . findFiles ) . mockImplementation ( async ( p : vscode . GlobPattern ) => {
90+ return [ vscode . Uri . file ( p . toString ( ) ) ]
91+ } )
8292
8393 // Mock fs.access to fail for all files
8494 vi . mocked ( fs . access ) . mockRejectedValue ( new Error ( 'File not found' ) )
@@ -93,8 +103,7 @@ describe('findWdioConfig', () => {
93103
94104 it ( 'should handle empty glob results' , async ( ) => {
95105 // Setup
96- // Mock glob to return no files
97- vi . mocked ( glob ) . mockResolvedValue ( [ ] )
106+ vi . mocked ( vscode . workspace . findFiles ) . mockResolvedValue ( [ ] )
98107
99108 // Execute
100109 const result = await findWdioConfig ( mockWorkspaceRoot , defaultConfigFilePattern )
@@ -103,28 +112,4 @@ describe('findWdioConfig', () => {
103112 expect ( result ) . toEqual ( [ ] )
104113 expect ( fs . access ) . not . toHaveBeenCalled ( )
105114 } )
106-
107- it ( 'should use custom config file pattern when provided' , async ( ) => {
108- // Setup
109- const customConfigPattern = [ 'custom.wdio.conf.js' ]
110- const mockConfigFiles = [ 'custom.wdio.conf.js' ]
111- const expectedPaths = mockConfigFiles . map ( ( file ) => path . join ( mockWorkspaceRoot , file ) )
112-
113- // Mock glob to return the config files
114- vi . mocked ( glob ) . mockResolvedValue ( mockConfigFiles )
115-
116- // Mock fs.access to succeed
117- vi . mocked ( fs . access ) . mockResolvedValue ( undefined )
118-
119- // Execute
120- const result = await findWdioConfig ( mockWorkspaceRoot , customConfigPattern )
121-
122- // Verify
123- expect ( result ) . toEqual ( expectedPaths )
124- expect ( glob ) . toHaveBeenCalledWith ( customConfigPattern , {
125- cwd : mockWorkspaceRoot ,
126- withFileTypes : false ,
127- ignore : '**/node_modules/**' ,
128- } )
129- } )
130115} )
0 commit comments