Skip to content

Commit b374c57

Browse files
baruchvlzchristian-bromann
authored andcommitted
wdio-cli: allow for custom path for WDIO config (webdriverio#4911)
* wdio-cli: allow for custom path for WDIO config * add documentation for install config flag
1 parent 88a99cd commit b374c57

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

docs/CLI.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,16 @@ wdio install reporter dot # installs @wdio/dot-reporter
135135
wdio install framework mocha # installs @wdio/mocha-framework
136136
```
137137

138-
If your project is using `package-lock.json` instead of `yarn.lock`, you can pass a `--npm` flag to make sure the packages are installed via NPM.
138+
If you want to install the packages using `yarn` instead, you can pass the `--yarn` flag to the command:
139139

140140
```bash
141-
wdio install service sauce --npm
141+
wdio install service sauce --yarn
142+
```
143+
144+
You could also pass a custom configuration path if your WDIO config file is not in the same folder you're working on:
145+
146+
```bash
147+
wdio install service sauce --config="./path/to/wdio.conf.js"
142148
```
143149

144150
#### List of supported services

packages/wdio-cli/src/commands/install.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ export const cmdArgs = {
3131
desc: 'Install packages using yarn',
3232
type: 'boolean',
3333
default: false
34-
}
34+
},
35+
config: {
36+
desc: 'Location of your WDIO configuration',
37+
default: './wdio.conf.js',
38+
},
3539
}
3640

3741
export const builder = (yargs) => {
@@ -55,7 +59,7 @@ export async function handler(argv) {
5559
* name = names for the supported service or reporter
5660
* yarn = optional flag to install package using yarn instead of default yarn
5761
*/
58-
const { type, name, yarn } = argv
62+
const { type, name, yarn, config } = argv
5963

6064
/**
6165
* verify for supported types via `supportedInstallations` keys
@@ -75,7 +79,7 @@ export async function handler(argv) {
7579
return
7680
}
7781

78-
const localConfPath = path.join(process.cwd(), 'wdio.conf.js')
82+
const localConfPath = path.join(process.cwd(), config)
7983
if (!fs.existsSync(localConfPath)) {
8084
try {
8185
const promptMessage = `Cannot install packages without a WebdriverIO configuration.

packages/wdio-cli/tests/commands/install.test.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('Command: install', () => {
2929
})
3030

3131
it('should log out when an unknown installation type is passed', async () => {
32-
await installCmd.handler({ type: 'foobar' })
32+
await installCmd.handler({ type: 'foobar', config: './wdio.conf.js' })
3333

3434
expect(console.log).toHaveBeenCalled()
3535
expect(console.log).toHaveBeenCalledWith('Type foobar is not supported.')
3636
})
3737

3838
it('should log out when unknown package name is used', async () => {
39-
await installCmd.handler({ type: 'service', name: 'foobar' })
39+
await installCmd.handler({ type: 'service', name: 'foobar', config: './wdio.conf.js' })
4040

4141
expect(console.log).toHaveBeenCalled()
4242
expect(console.log).toHaveBeenCalledWith('foobar is not a supported service.')
@@ -46,7 +46,7 @@ describe('Command: install', () => {
4646
jest.spyOn(utils, 'missingConfigurationPrompt').mockImplementation(() => Promise.reject())
4747
fs.existsSync.mockReturnValue(false)
4848

49-
await installCmd.handler({ type: 'service', name: 'chromedriver' })
49+
await installCmd.handler({ type: 'service', name: 'chromedriver', config: './wdio.conf.js' })
5050

5151
expect(utils.missingConfigurationPrompt).toHaveBeenCalledWith('install', `Cannot install packages without a WebdriverIO configuration.
5252
You can create one by running 'wdio config'`, undefined)
@@ -56,7 +56,7 @@ You can create one by running 'wdio config'`, undefined)
5656
findInConfigMock.mockReturnValue(['chromedriver'])
5757
fs.existsSync.mockReturnValue(true)
5858

59-
await installCmd.handler({ type: 'service', name: 'chromedriver' })
59+
await installCmd.handler({ type: 'service', name: 'chromedriver', config: './wdio.conf.js' })
6060

6161
expect(console.log).toHaveBeenCalledWith('The service chromedriver is already part of your configuration.')
6262
})
@@ -67,7 +67,22 @@ You can create one by running 'wdio config'`, undefined)
6767
fs.readFileSync.mockReturnValue('module.config = {}')
6868
yarnInstall.mockImplementation(() => ({ status: 0 }))
6969

70-
await installCmd.handler({ type: 'service', name: 'chromedriver' })
70+
await installCmd.handler({ type: 'service', name: 'chromedriver', config: './wdio.conf.js' })
71+
72+
expect(console.log).toHaveBeenCalledWith('Installing "wdio-chromedriver-service".')
73+
expect(console.log).toHaveBeenCalledWith('Package "wdio-chromedriver-service" installed successfully.')
74+
expect(utils.replaceConfig).toHaveBeenCalled()
75+
expect(fs.writeFileSync).toHaveBeenCalled()
76+
expect(console.log).toHaveBeenCalledWith('Your wdio.conf.js file has been updated.')
77+
expect(process.exit).toHaveBeenCalledWith(0)
78+
})
79+
it('allows for custom config location', async () => {
80+
findInConfigMock.mockReturnValue([''])
81+
fs.existsSync.mockReturnValue(true)
82+
fs.readFileSync.mockReturnValue('module.config = {}')
83+
yarnInstall.mockImplementation(() => ({ status: 0 }))
84+
85+
await installCmd.handler({ type: 'service', name: 'chromedriver', config: './path/to/wdio.conf.js' })
7186

7287
expect(console.log).toHaveBeenCalledWith('Installing "wdio-chromedriver-service".')
7388
expect(console.log).toHaveBeenCalledWith('Package "wdio-chromedriver-service" installed successfully.')
@@ -83,7 +98,7 @@ You can create one by running 'wdio config'`, undefined)
8398
yarnInstall.mockImplementation(() => ({ status: 1, stderr: 'test error' }))
8499
jest.spyOn(console, 'error')
85100

86-
await installCmd.handler({ type: 'service', name: 'chromedriver' })
101+
await installCmd.handler({ type: 'service', name: 'chromedriver', config: './wdio.conf.js' })
87102

88103
expect(console.error).toHaveBeenCalledWith('Error installing packages', 'test error')
89104
expect(process.exit).toHaveBeenCalledWith(1)

0 commit comments

Comments
 (0)