Skip to content

Commit e3e8ded

Browse files
authored
fix(cli): properly check and find the config file (webdriverio#14229)
* fix: add checks for file paths containing extensions * test: add test for `canAccessConfigPath` * test: adjust mocking for run command
1 parent dc41a90 commit e3e8ded

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

packages/wdio-cli/src/commands/config.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,25 @@ export async function formatConfigFilePaths(config: string) {
193193

194194
/**
195195
* Helper utility used in `run` and `install` command to check whether a config file currently exists
196-
* @param configPath the file path to the WDIO config file
196+
* @param configPathNoExtension the file path to the WDIO config file without extension
197+
* @param configPath the file path to the WDIO config file that is checked first if set
197198
* @returns {string} the path to the config file that exists, otherwise undefined
198199
*/
199-
export async function canAccessConfigPath(configPath: string) {
200-
return Promise.all(SUPPORTED_CONFIG_FILE_EXTENSION.map(async (supportedExtension) => {
201-
const configPathWithExtension = `${configPath}.${supportedExtension}`
200+
export async function canAccessConfigPath(configPathNoExtension:string, configPath?:string) {
201+
return new Promise<string | undefined>((resolve, reject)=>{
202+
if (configPath) {
203+
const _configPath = configPath
204+
fs.access(_configPath).then(()=>resolve(_configPath), reject)
205+
} else {
206+
reject()
207+
}
208+
}).catch(()=>Promise.all(SUPPORTED_CONFIG_FILE_EXTENSION.map(async (supportedExtension) => {
209+
const configPathWithExtension = `${configPathNoExtension}.${supportedExtension}`
202210
return fs.access(configPathWithExtension).then(() => configPathWithExtension, () => undefined)
203211
})).then(
204212
(configFilePaths) => configFilePaths.find(Boolean),
205213
() => undefined
206-
)
214+
))
207215
}
208216

209217
/**

packages/wdio-cli/src/commands/run.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export async function handler(argv: RunCommandArguments) {
176176
const { configPath = 'wdio.conf.js', ...params } = argv
177177

178178
const wdioConf = await formatConfigFilePaths(configPath)
179-
const confAccess = await canAccessConfigPath(wdioConf.fullPathNoExtension)
179+
const confAccess = await canAccessConfigPath(wdioConf.fullPathNoExtension, wdioConf.fullPath)
180180
if (!confAccess) {
181181
try {
182182
await missingConfigurationPrompt('run', wdioConf.fullPathNoExtension)

packages/wdio-cli/tests/commands/config.test.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,45 @@ test('missingConfigurationPrompt does run config if user agrees', async () => {
172172
expect(runConfigCmd).toBeCalledTimes(1)
173173
})
174174

175-
test('canAccessConfigPath', async () => {
176-
vi.mocked(fs.access)
177-
.mockRejectedValueOnce(new Error('not found'))
178-
.mockRejectedValueOnce(new Error('not found'))
179-
.mockRejectedValueOnce(new Error('not found'))
180-
.mockResolvedValue('Yay' as any)
181-
expect(await canAccessConfigPath('/foo/bar')).toBe('/foo/bar.mts')
182-
expect(fs.access).toBeCalledWith('/foo/bar.js')
183-
expect(fs.access).toBeCalledWith('/foo/bar.ts')
184-
expect(fs.access).toBeCalledWith('/foo/bar.mjs')
185-
expect(fs.access).toBeCalledWith('/foo/bar.mts')
175+
describe('canAccessConfigPath', () => {
176+
beforeEach(()=>{
177+
vi.mocked(fs.access).mockClear()
178+
})
179+
test('no extension only', async () => {
180+
vi.mocked(fs.access)
181+
.mockRejectedValueOnce(new Error('not found'))
182+
.mockRejectedValueOnce(new Error('not found'))
183+
.mockRejectedValueOnce(new Error('not found'))
184+
.mockResolvedValue('Yay' as any)
185+
expect(await canAccessConfigPath('/foo/bar')).toBe('/foo/bar.mts')
186+
expect(fs.access).not.toBeCalledWith('/foo/bar')
187+
expect(fs.access).toBeCalledWith('/foo/bar.js')
188+
expect(fs.access).toBeCalledWith('/foo/bar.ts')
189+
expect(fs.access).toBeCalledWith('/foo/bar.mjs')
190+
expect(fs.access).toBeCalledWith('/foo/bar.mts')
191+
})
192+
193+
test('with full path of the existed file', async () => {
194+
vi.mocked(fs.access)
195+
.mockResolvedValue('Yay' as any)
196+
expect(await canAccessConfigPath('/foo/bar', '/foo/bar.ts')).toBe('/foo/bar.ts')
197+
expect(fs.access).toHaveBeenCalledTimes(1)
198+
expect(fs.access).toBeCalledWith('/foo/bar.ts')
199+
})
200+
201+
test('with full path of the not existed file', async () => {
202+
vi.mocked(fs.access)
203+
.mockRejectedValueOnce(new Error('not found'))
204+
.mockRejectedValueOnce(new Error('not found'))
205+
.mockRejectedValueOnce(new Error('not found'))
206+
.mockRejectedValueOnce(new Error('not found'))
207+
.mockResolvedValue('Yay' as any)
208+
expect(await canAccessConfigPath('/foo/bar', '/foo/bar.ts')).toBe('/foo/bar.mts')
209+
expect(fs.access).toBeCalledWith('/foo/bar.js')
210+
expect(fs.access).toBeCalledWith('/foo/bar.ts')
211+
expect(fs.access).toBeCalledWith('/foo/bar.mjs')
212+
expect(fs.access).toBeCalledWith('/foo/bar.mts')
213+
})
186214
})
187215

188216
describe('Serenity/JS project generation', () => {

packages/wdio-cli/tests/commands/run.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ describe('Command: run', () => {
6666
})
6767

6868
it('should check for js and ts default config files', async () => {
69-
vi.mocked(fs.access).mockRejectedValueOnce('not found')
69+
vi.mocked(fs.access)
70+
.mockRejectedValueOnce('not found')
71+
.mockRejectedValueOnce('not found')
7072
const result = await runCmd.handler({ configPath: 'sample.conf.js' } as any)
7173
expect(result).toContain('sample.conf.ts')
7274
})

0 commit comments

Comments
 (0)