Skip to content

Commit 5a9d03d

Browse files
Support fully qualified paths to plugins (webdriverio#4959)
* Support fully qualified paths to plugins - closes webdriverio#4867 * better check of absolute path
1 parent 806f940 commit 5a9d03d

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

docs/CustomReporter.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,20 @@ const CustomReporter = require('./reporter/my.custom.reporter')
3737

3838
exports.config = {
3939
// ...
40-
reporters: [[CustomReporter, {
41-
someOption: 'foobar'
42-
}]],
40+
reporters: [
41+
/**
42+
* use imported reporter class
43+
*/
44+
[CustomReporter, {
45+
someOption: 'foobar'
46+
}]
47+
/**
48+
* use absolute path to reporter
49+
*/
50+
['/path/to/reporter.js', {
51+
someOption: 'foobar'
52+
}]
53+
],
4354
// ...
4455
}
4556
```

docs/CustomServices.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ id: customservices
33
title: Custom Services
44
---
55

6-
You can write your own custom service for the WDIO test runner to custom-fit your needs.
6+
You can write your own custom service for the WDIO test runner to custom-fit your needs.
77

8-
<dfn>Services</dfn> are add-ons that are created for reusable logic to simplify tests, manage your test suite, and integrate results. Services have access to all the same [`before`/`after` hooks](ConfigurationFile.md) available in the `wdio.conf.js`.
8+
<dfn>Services</dfn> are add-ons that are created for reusable logic to simplify tests, manage your test suite, and integrate results. Services have access to all the same [`before`/`after` hooks](ConfigurationFile.md) available in the `wdio.conf.js`.
99

1010
The basic construction of a custom service should look like this:
1111

@@ -23,7 +23,7 @@ export default class CustomService {
2323
}
2424
```
2525

26-
The only thing to do now in order to use this service is to assign it to the `services` property.
26+
The only thing to do now in order to use this service is to assign it to the `services` property.
2727

2828
Modify your `wdio.conf.js` file to look like this:
2929

@@ -32,9 +32,20 @@ import CustomService from './service/my.custom.service'
3232

3333
exports.config = {
3434
// ...
35-
services: [[CustomService, {
36-
someOption: true
37-
}]],
35+
services: [
36+
/**
37+
* use imported service class
38+
*/
39+
[CustomService, {
40+
someOption: true
41+
}],
42+
/**
43+
* use absolute path to service
44+
*/
45+
['/path/to/service.js', {
46+
someOption: true
47+
}]
48+
],
3849
// ...
3950
}
4051
```
@@ -60,13 +71,13 @@ exports.config = {
6071
```
6172

6273
> **Note:** Services that are added by name behave slightly differently compared to your own imported services. Instead of the service handling all the hooks, as in the example above, the service needs to export a launcher that handles `onPrepare` and `onComplete`. The rest of the hooks will be handled by the service (the default export), as normal.
63-
>
74+
>
6475
> Example:
6576
>
6677
> ```
6778
> import Launcher from './launcher'
6879
> import Service from './service'
69-
>
80+
>
7081
> export default Service
7182
> export const launcher = Launcher
7283
> ```

packages/wdio-utils/src/initialisePlugin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import { safeRequire } from './utils'
23

34
/**
@@ -8,9 +9,9 @@ import { safeRequire } from './utils'
89
*/
910
export default function initialisePlugin (name, type, target = 'default') {
1011
/**
11-
* directly import packages that are scoped
12+
* directly import packages that are scoped or start with an absolute path
1213
*/
13-
if (name[0] === '@') {
14+
if (name[0] === '@' || path.isAbsolute(name)) {
1415
const service = safeRequire(name)
1516
return service[target]
1617
}

packages/wdio-utils/tests/initialisePlugin.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe('initialisePlugin', () => {
2525
expect(service.foo).toBe('barfoo')
2626
})
2727

28+
it('should allow to load service referenced with an absolute path', () => {
29+
const Service = initialisePlugin(require.resolve(__dirname + '/__mocks__/@saucelabs/wdio-foobar-reporter'))
30+
const service = new Service()
31+
expect(service.foo).toBe('barfoo')
32+
})
33+
2834
it('should prefer scoped over unscoped packages', () => {
2935
const Service = initialisePlugin('scoped', 'service')
3036
const service = new Service()

0 commit comments

Comments
 (0)