Skip to content

Commit 1b49f84

Browse files
committed
merged hooks
2 parents eb779c8 + 4fe9254 commit 1b49f84

File tree

9 files changed

+70
-157
lines changed

9 files changed

+70
-157
lines changed

docs/basics.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ AfterSuite((I) => {
110110
## Within
111111

112112
To specify the exact area on a page where actions can be performed you can use `within` function.
113-
Everything executed in its context will be narrowed to context specified by locator:
113+
Everything executed in its context will be narrowed to context specified by locator:
114114

115115
Usage: within('section', ()=>{} )
116116
```js
@@ -161,34 +161,6 @@ By default a custom console reporter is enabled.
161161

162162
We are currently working on improving reporters support.
163163

164-
## Bootstrap
165-
166-
In case you need to execute arbitrary code before the tests,
167-
you can place it into your bootstrap file and provide a relative path to it in `codecept.json`
168-
169-
```json
170-
"bootstrap": "./run_server.js"
171-
```
172-
173-
To run `bootstrap` async, just export a function in your bootstrap file
174-
175-
```js
176-
module.exports = function(done) {
177-
// async instructions
178-
// call done() to continue execution
179-
// otherwise call done('error description')
180-
}
181-
```
182-
183-
## Teardown
184-
185-
In case you need to execute arbitrary code after the tests have run,
186-
you can place it into your teardown file and provide a relative path to it in `codecept.json`
187-
188-
```json
189-
"teardown": "./stop_server.js"
190-
```
191-
192164
## Test Options
193165

194166
Features and Scenarios have their options that can be set by passing a hash after their names:

docs/configuration.md

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -57,83 +57,6 @@ exports.config = {
5757

5858
(Don't copy-paste this config, it's just demo)
5959

60-
## Hooks
61-
62-
Hooks are implemented as `bootstrap` and `teardown` options in config. You can use them to prepare test environment before execution and cleanup after.
63-
They can be used to launch stop webserver, selenium server, etc. There are different sync and async ways to define bootstrap and teardown functions.
64-
65-
`bootstrap` and `teardown` options can be:
66-
67-
* JS file, executed as is (synchronously).
68-
* JS file exporting a function; If function accepts a callback is executed asynchronously. See example:
69-
70-
Config (`codecept.json`):
71-
72-
```js
73-
"bootstrap": "./bootstrap.js"
74-
```
75-
76-
Bootstrap file (`bootstrap.js`):
77-
78-
```js
79-
// bootstrap.js
80-
var server = require('./app_server');
81-
module.exports = function(done) {
82-
// on error call done('error description') to stop
83-
if (!server.validateConfig()) {
84-
done("Can't execute server with invalid config, tests stopped");
85-
}
86-
// call done() to continue execution
87-
server.launch(done);
88-
}
89-
```
90-
91-
Pass error description inside a callback (`done('error')`) to prevent test execution on bootstrap.
92-
93-
* JS file exporting an object with `bootstrap` and (or) `teardown` methods for corresponding hooks.
94-
95-
Config (`codecept.json`):
96-
97-
```js
98-
"bootstrap": "./bootstrap.js"
99-
"teardown": "./bootstrap.js"
100-
```
101-
102-
Bootstrap file (`bootstrap.js`):
103-
104-
```js
105-
// bootstrap.js
106-
var server = require('./app_server');
107-
module.exports = {
108-
bootstrap: function(done) {
109-
server.launch(done);
110-
},
111-
teardown: function(done) {
112-
server.stop(done);
113-
}
114-
}
115-
```
116-
117-
* JS function in case of dynamic config. If function accepts a callback is executed asynchronously. See example:
118-
119-
Config JS (`codecept.conf.js`):
120-
121-
```js
122-
var server = require('./app_server');
123-
124-
exports.config = {
125-
bootstrap: function(done) {
126-
server.launch(done);
127-
},
128-
teardown: function(done) {
129-
server.stop(done);
130-
}
131-
// ...
132-
// other config options
133-
}
134-
135-
```
136-
13760
## Profile
13861

13962
Using values from `process.profile` you can change the config dynamically.

docs/hooks.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ module.exports = function() {
204204

205205
```
206206
207+
<<<<<<< HEAD
207208
Whenever you execute tests with `--verbose` option you will see registered events and promises executed by recorder.
209+
=======
210+
Whenever you execute tests with `--verbose` option you will see registered events and promises executed by a recorder.
211+
>>>>>>> hook-docs
208212
209213
### Output
210214
@@ -261,4 +265,12 @@ container.append({
261265
})
262266
```
263267
264-
## done()
268+
<<<<<<< HEAD
269+
## done()
270+
=======
271+
## done()
272+
273+
274+
275+
276+
>>>>>>> hook-docs

lib/command/utils.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,15 @@ module.exports.getConfig = function (configFile) {
3838
process.exit(1);
3939
};
4040

41-
function isObject(item) {
42-
return item && typeof item === 'object' && !Array.isArray(item);
43-
}
41+
// alias to deep merge
42+
module.exports.deepMerge = require('../utils').deepMerge;
4443

45-
function deepMerge(target, source) {
46-
if (isObject(target) && isObject(source)) {
47-
for (const key in source) {
48-
if (isObject(source[key])) {
49-
if (!target[key]) Object.assign(target, { [key]: {} });
50-
deepMerge(target[key], source[key]);
51-
} else {
52-
Object.assign(target, { [key]: source[key] });
53-
}
54-
}
55-
}
56-
return target;
44+
function getTestRoot(currentPath) {
45+
if (!currentPath) currentPath = '.';
46+
if (!path.isAbsolute(currentPath)) currentPath = path.join(process.cwd(), currentPath);
47+
return currentPath = !path.extname(currentPath) ? currentPath : path.dirname(currentPath);
5748
}
58-
module.exports.deepMerge = deepMerge;
59-
49+
module.exports.getTestRoot = getTestRoot;
6050

6151
function getTestRoot(currentPath) {
6252
if (!currentPath) currentPath = '.';

lib/container.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ module.exports = {
3636
},
3737

3838
append: (newContainer) => {
39-
this.container = Object.assign(this.container, newContainer);
39+
const deepMerge = require('./utils').deepMerge;
40+
container = deepMerge(container, newContainer);
4041
},
4142

4243
clear: (newHelpers, newSupport) => {

lib/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
},
9393

9494
say: (message) => {
95-
print(` ${colors.cyan.bold(message)}`);
95+
if (outputLevel >= 1) print(` ${colors.cyan.bold(message)}`);
9696
},
9797

9898
result: (passed, failed, skipped, duration) => {

lib/utils.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ var fs = require('fs');
22
var path = require('path');
33
var getParameterNames = require('get-parameter-names');
44

5+
function isObject(item) {
6+
return item && typeof item === 'object' && !Array.isArray(item);
7+
}
8+
9+
function deepMerge(target, source) {
10+
if (isObject(target) && isObject(source)) {
11+
for (const key in source) {
12+
if (isObject(source[key])) {
13+
if (!target[key]) Object.assign(target, { [key]: {} });
14+
deepMerge(target[key], source[key]);
15+
} else {
16+
Object.assign(target, { [key]: source[key] });
17+
}
18+
}
19+
}
20+
return target;
21+
}
22+
23+
module.exports.deepMerge = deepMerge;
24+
525
module.exports.fileExists = function (filePath) {
626
try {
727
fs.statSync(filePath);
@@ -74,7 +94,6 @@ module.exports.lcfirst = function (str) {
7494
return str.charAt(0).toLowerCase() + str.substr(1);
7595
};
7696

77-
7897
module.exports.hashCode = function (str) {
7998
/* Getting 32bit hashCode of string like in Java.
8099
* Used in Screenshot name to provide short screenshot names

test/data/sandbox/steps.d.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/unit/container_test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let should = require('chai').should();
44
let assert = require('assert');
55
let path = require('path');
66
let sinon = require('sinon');
7+
let FileSystem = require('../../lib/helper/FileSystem');
78

89
describe('Container', () => {
910

@@ -78,7 +79,6 @@ describe('Container', () => {
7879

7980
describe('#create', () => {
8081
it('should create container with helpers', () => {
81-
let FileSystem = require('../../lib/helper/FileSystem');
8282
let config = {
8383
helpers: {
8484
MyHelper: {
@@ -114,6 +114,31 @@ describe('Container', () => {
114114
});
115115
});
116116

117+
describe('#append', () => {
118+
it('should be able to add new helper', () => {
119+
let config = {
120+
helpers: {
121+
FileSystem: {}
122+
}
123+
};
124+
container.create(config);
125+
container.append({helpers: {
126+
AnotherHelper: { method: () => 'executed' }
127+
}});
128+
assert.ok(container.helpers('FileSystem'));
129+
container.helpers('FileSystem').should.be.instanceOf(FileSystem);
130+
131+
assert.ok(container.helpers('AnotherHelper'));
132+
container.helpers('AnotherHelper').method().should.eql('executed');
133+
});
117134

135+
it('should be able to add new support object', () => {
136+
container.create({});
137+
container.append({support: {userPage: { login: '#login' }}})
138+
assert.ok(container.support('I'));
139+
assert.ok(container.support('userPage'));
140+
container.support('userPage').login.should.eql('#login');
141+
});
142+
});
118143

119144
});

0 commit comments

Comments
 (0)