Skip to content

Commit a33145c

Browse files
deprecate defineSupportCode (#1005)
1 parent 48aac9a commit a33145c

File tree

65 files changed

+1067
-1406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1067
-1406
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
66

77
* cucumber now waits for the event loop to drain before exiting. To exit immediately when the tests finish running use `--exit`. Use of this flag is discouraged. See [here](/docs/cli.md#exiting) for more information
88

9+
#### Deprecations
10+
11+
* `defineSupportCode` is deprecated. Require/import the individual methods instead
12+
```js
13+
var {defineSupportCode} = require('cucumber');
14+
15+
defineSupportCode(function({Given}) {
16+
Given(/^a step$/, function() {});
17+
});
18+
19+
// Should be updated to
20+
21+
var {Given} = require('cucumber');
22+
23+
Given(/^a step$/, function() {});
24+
```
25+
926
### [3.2.1](https://github.com/cucumber/cucumber-js/compare/v3.2.0...v3.2.1) (2017-01-03)
1027

1128
#### Bug Fixes

docs/support_files/api_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## API Reference
44

5-
The function passed to `defineSupportCode` is called with an object as the first argument that exposes the following methods:
5+
Each method can be destructed from the object returned by `require('cucumber')`.
66

77
---
88

docs/support_files/attachments.md

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ which the default world constructor assigns to `this.attach`. If using a custom
66
you need to do this as well if you want to add attachments.
77

88
```javascript
9-
var {defineSupportCode} = require('cucumber');
9+
var {After} = require('cucumber');
1010

11-
defineSupportCode(function({After}) {
12-
After(function () {
13-
this.attach('Some text');
14-
});
11+
After(function () {
12+
this.attach('Some text');
1513
});
1614
```
1715

1816
By default, text is saved with a MIME type of `text/plain`. You can also specify
1917
a different MIME type:
2018

2119
```javascript
22-
var {defineSupportCode} = require('cucumber');
20+
var {After} = require('cucumber');
2321

24-
defineSupportCode(function({After}) {
25-
After(function () {
26-
this.attach('{"name": "some JSON"}', 'application/json');
27-
});
22+
After(function () {
23+
this.attach('{"name": "some JSON"}', 'application/json');
2824
});
2925
```
3026

@@ -33,61 +29,55 @@ The data will be `base64` encoded in the output.
3329
You should wait for the stream to be read before continuing by providing a callback or waiting for the returned promise to resolve.
3430

3531
```javascript
36-
var {defineSupportCode, Status} = require('cucumber');
37-
38-
defineSupportCode(function({After}) {
39-
// Passing a callback
40-
After(function (testCase, callback) {
41-
if (testCase.result.status === Status.FAILED) {
42-
var stream = getScreenshotOfError();
43-
this.attach(stream, 'image/png', callback);
44-
}
45-
else {
46-
callback();
47-
}
48-
});
49-
50-
// Returning the promise
51-
After(function (testCase) {
52-
if (testCase.result.status === Status.FAILED) {
53-
var stream = getScreenshotOfError();
54-
return this.attach(stream, 'image/png');
55-
}
56-
});
32+
var {After, Status} = require('cucumber');
33+
34+
// Passing a callback
35+
After(function (testCase, callback) {
36+
if (testCase.result.status === Status.FAILED) {
37+
var stream = getScreenshotOfError();
38+
this.attach(stream, 'image/png', callback);
39+
}
40+
else {
41+
callback();
42+
}
43+
});
44+
45+
// Returning the promise
46+
After(function (testCase) {
47+
if (testCase.result.status === Status.FAILED) {
48+
var stream = getScreenshotOfError();
49+
return this.attach(stream, 'image/png');
50+
}
5751
});
5852
```
5953

6054
Images and binary data can also be attached using a [Buffer](https://nodejs.org/api/buffer.html).
6155
The data will be `base64` encoded in the output.
6256

6357
```javascript
64-
var {defineSupportCode} = require('cucumber');
65-
66-
defineSupportCode(function({After}) {
67-
After(function (testCase) {
68-
if (testCase.result.status === Status.FAILED) {
69-
var buffer = getScreenshotOfError();
70-
this.attach(buffer, 'image/png');
71-
}
72-
});
58+
var {After, Status} = require('cucumber');
59+
60+
After(function (testCase) {
61+
if (testCase.result.status === Status.FAILED) {
62+
var buffer = getScreenshotOfError();
63+
this.attach(buffer, 'image/png');
64+
}
7365
});
7466
```
7567

7668
Here is an example of saving a screenshot using [Selenium WebDriver](https://www.npmjs.com/package/selenium-webdriver)
7769
when a scenario fails:
7870

7971
```javascript
80-
var {defineSupportCode} = require('cucumber');
81-
82-
defineSupportCode(function({After}) {
83-
After(function (testCase) {
84-
var world = this;
85-
if (testCase.result.status === Status.FAILED) {
86-
return webDriver.takeScreenshot().then(function(screenShot) {
87-
// screenShot is a base-64 encoded PNG
88-
world.attach(screenShot, 'image/png');
89-
});
90-
}
91-
});
72+
var {After} = require('cucumber');
73+
74+
After(function (testCase) {
75+
var world = this;
76+
if (testCase.result.status === Status.FAILED) {
77+
return webDriver.takeScreenshot().then(function(screenShot) {
78+
// screenShot is a base-64 encoded PNG
79+
world.attach(screenShot, 'image/png');
80+
});
81+
}
9282
});
9383
```

docs/support_files/hooks.md

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@
33
Hooks are used for setup and teardown the environment before and after each scenario. See the [API reference](./api_reference.md) for the specification of the first argument passed to hooks. Multiple *Before* hooks are executed in the order that they were defined. Multiple *After* hooks are executed in the **reverse** order that they were defined.
44

55
```javascript
6-
var {defineSupportCode} = require('cucumber');
6+
var {After, Before} = require('cucumber');
77

8-
defineSupportCode(function({After, Before}) {
9-
// Synchronous
10-
Before(function () {
11-
this.count = 0;
12-
});
8+
// Synchronous
9+
Before(function () {
10+
this.count = 0;
11+
});
1312

14-
// Asynchronous Callback
15-
Before(function (testCase, callback) {
16-
var world = this;
17-
tmp.dir({unsafeCleanup: true}, function(error, dir) {
18-
if (error) {
19-
callback(error);
20-
} else {
21-
world.tmpDir = dir;
22-
callback();
23-
}
24-
});
13+
// Asynchronous Callback
14+
Before(function (testCase, callback) {
15+
var world = this;
16+
tmp.dir({unsafeCleanup: true}, function(error, dir) {
17+
if (error) {
18+
callback(error);
19+
} else {
20+
world.tmpDir = dir;
21+
callback();
22+
}
2523
});
24+
});
2625

27-
// Asynchronous Promise
28-
After(function () {
29-
// Assuming this.driver is a selenium webdriver
30-
return this.driver.quit();
31-
});
26+
// Asynchronous Promise
27+
After(function () {
28+
// Assuming this.driver is a selenium webdriver
29+
return this.driver.quit();
3230
});
3331
```
3432

@@ -37,29 +35,27 @@ defineSupportCode(function({After, Before}) {
3735
Hooks can be conditionally selected for execution based on the tags of the scenario.
3836

3937
```javascript
40-
var {defineSupportCode} = require('cucumber');
38+
var {After, Before} = require('cucumber');
4139

42-
defineSupportCode(function({After, Before}) {
43-
Before(function () {
44-
// This hook will be executed before all scenarios
45-
});
40+
Before(function () {
41+
// This hook will be executed before all scenarios
42+
});
4643

47-
Before({tags: "@foo"}, function () {
48-
// This hook will be executed before scenarios tagged with @foo
49-
});
44+
Before({tags: "@foo"}, function () {
45+
// This hook will be executed before scenarios tagged with @foo
46+
});
5047

51-
Before({tags: "@foo and @bar"}, function () {
52-
// This hook will be executed before scenarios tagged with @foo and @bar
53-
});
48+
Before({tags: "@foo and @bar"}, function () {
49+
// This hook will be executed before scenarios tagged with @foo and @bar
50+
});
5451

55-
Before({tags: "@foo or @bar"}, function () {
56-
// This hook will be executed before scenarios tagged with @foo or @bar
57-
});
52+
Before({tags: "@foo or @bar"}, function () {
53+
// This hook will be executed before scenarios tagged with @foo or @bar
54+
});
5855

59-
// You can use the following shorthand when only specifying tags
60-
Before("@foo", function () {
61-
// This hook will be executed before scenarios tagged with @foo
62-
});
56+
// You can use the following shorthand when only specifying tags
57+
Before("@foo", function () {
58+
// This hook will be executed before scenarios tagged with @foo
6359
});
6460
```
6561

@@ -72,12 +68,10 @@ If you need to imperatively skip a test using a `Before` hook, this can be done
7268
This includes using: a synchronous return, an asynchronous callback, or an asynchronous promise
7369

7470
```javascript
75-
defineSupportCode(({After, Before}) => {
76-
// Synchronous
77-
Before(function() {
78-
// perform some runtime check to decide whether to skip the proceeding scenario
79-
return 'skipped'
80-
});
71+
// Synchronous
72+
Before(function() {
73+
// perform some runtime check to decide whether to skip the proceeding scenario
74+
return 'skipped'
8175
});
8276
```
8377

@@ -88,25 +82,23 @@ If you have some setup / teardown that needs to be done before or after all scen
8882
Unlike `Before` / `After` these methods will not have a world instance as `this`. This is becauce each scenario gets its own world instance and these hooks run before / after **all** scenarios.
8983

9084
```javascript
91-
var {defineSupportCode} = require('cucumber');
85+
var {AfterAll, BeforeAll} = require('cucumber');
9286

93-
defineSupportCode(function({AfterAll, BeforeAll}) {
94-
// Synchronous
95-
BeforeAll(function () {
96-
// perform some shared setup
97-
});
87+
// Synchronous
88+
BeforeAll(function () {
89+
// perform some shared setup
90+
});
9891

99-
// Asynchronous Callback
100-
BeforeAll(function (callback) {
101-
// perform some shared setup
92+
// Asynchronous Callback
93+
BeforeAll(function (callback) {
94+
// perform some shared setup
10295

103-
// execute the callback (optionally passing an error when done)
104-
});
96+
// execute the callback (optionally passing an error when done)
97+
});
10598

106-
// Asynchronous Promise
107-
AfterAll(function () {
108-
// perform some shared teardown
109-
return Promise.resolve()
110-
});
99+
// Asynchronous Promise
100+
AfterAll(function () {
101+
// perform some shared teardown
102+
return Promise.resolve()
111103
});
112104
```

0 commit comments

Comments
 (0)