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,22 @@ 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+ // if (p.toString() === accessibleFile) {
69+ // return [vscode.Uri.file(p.toString())]
70+ // }
71+ // return []
72+ // })
73+ vi . mocked ( vscode . workspace . findFiles ) . mockImplementation ( async ( p : vscode . GlobPattern ) => {
74+ return [ vscode . Uri . file ( p . toString ( ) ) ]
75+ } )
5976
6077 // Mock fs.access to succeed only for the JS file
6178 vi . mocked ( fs . access ) . mockImplementation ( async ( filePath ) => {
@@ -75,10 +92,9 @@ describe('findWdioConfig', () => {
7592
7693 it ( 'should return empty array when no config files are accessible' , async ( ) => {
7794 // 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 )
95+ vi . mocked ( vscode . workspace . findFiles ) . mockImplementation ( async ( p : vscode . GlobPattern ) => {
96+ return [ vscode . Uri . file ( p . toString ( ) ) ]
97+ } )
8298
8399 // Mock fs.access to fail for all files
84100 vi . mocked ( fs . access ) . mockRejectedValue ( new Error ( 'File not found' ) )
@@ -93,8 +109,7 @@ describe('findWdioConfig', () => {
93109
94110 it ( 'should handle empty glob results' , async ( ) => {
95111 // Setup
96- // Mock glob to return no files
97- vi . mocked ( glob ) . mockResolvedValue ( [ ] )
112+ vi . mocked ( vscode . workspace . findFiles ) . mockResolvedValue ( [ ] )
98113
99114 // Execute
100115 const result = await findWdioConfig ( mockWorkspaceRoot , defaultConfigFilePattern )
@@ -103,28 +118,4 @@ describe('findWdioConfig', () => {
103118 expect ( result ) . toEqual ( [ ] )
104119 expect ( fs . access ) . not . toHaveBeenCalled ( )
105120 } )
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- } )
130121} )
0 commit comments