Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable asyc bootsrap file #303

Merged
merged 2 commits into from Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/basics.md
Expand Up @@ -151,6 +151,16 @@ you can place it into your bootstrap file and provide a relative path to it in `
"bootstrap": "./run_server.js"
```

To run `bootstrap` async, just export a function in your bootstrap file

```js
module.exports = function(done) {
// async instructions
// call done() to continue execution
// otherwise call done('error description')
}
```

## teardown

In case you need to execute arbitrary code after the tests have run,
Expand Down
13 changes: 9 additions & 4 deletions lib/codecept.js
Expand Up @@ -29,7 +29,7 @@ class Codecept {
this.testFiles = [];
}

init(dir) {
init(dir, callback) {
// preparing globals
global.codecept_dir = dir;
global.output_dir = fsPath.resolve(dir, this.config.output);
Expand All @@ -45,13 +45,18 @@ class Codecept {
require('./listener/exit');
require('./listener/trace');

this.bootstrap();
this.bootstrap(callback);
}

// loading bootstrap
bootstrap() {
bootstrap(callback) {
if (this.config.bootstrap && fileExists(fsPath.join(codecept_dir, this.config.bootstrap))) {
require(fsPath.join(codecept_dir, this.config.bootstrap));
var bootstrap = require(fsPath.join(codecept_dir, this.config.bootstrap));
if (typeof bootstrap === 'function') {
bootstrap(callback);
return;
}
callback();
}
}

Expand Down
79 changes: 43 additions & 36 deletions lib/command/definitions.js
Expand Up @@ -36,42 +36,49 @@ module.exports = function (genPath) {
if (!config) return;

let codecept = new Codecept(config, {});
codecept.init(testsPath);
let helpers = container.helpers();
let suppportI = container.support('I');
let translations = container.translation();
let methods = [];
let actions = [];
for (let name in helpers) {
let helper = helpers[name];
methodsOfObject(helper).forEach((action) => {
let actionAlias = container.translation() ? container.translation().actionAliasFor(action) : action;
if (!actions[actionAlias]) {
let params = getParamNames(helper[action]);
if (params) params = params.join(', ');
if (!params) params = '';
methods.push(` ${(actionAlias)}: (${params}) => any; \n`);
actions[actionAlias] = 1
}
});
}
for (let name in suppportI) {
if (actions[name]) {
continue
}
let actor = suppportI[name];
let params = getParamNames(actor);
if (params) params = params.join(', ');
if (!params) params = '';
methods.push(` ${(name)}: (${params}) => any; \n`);
}
let definitionsTemplate = template.replace('{{methods}}', methods.join(''));
definitionsTemplate = definitionsTemplate.replace(/\{\{I\}\}/g, container.translation().I);
codecept.init(testsPath, function(err) {
if (err) {
output.error('Error while running bootstrap file :' + err);
return;
}

fs.writeFileSync(path.join(testsPath, 'steps.d.ts'), definitionsTemplate);
output.print('TypeScript Definitions provide autocompletion in Visual Studio Code and other IDEs');
output.print('Definitions were generated in steps.d.ts');
output.print('Load them by adding at the top of a test file:');
output.print(output.colors.grey(`\n/// <reference path="./steps.d.ts" />`));
let helpers = container.helpers();
let suppportI = container.support('I');
let translations = container.translation();
let methods = [];
let actions = [];
for (let name in helpers) {
let helper = helpers[name];
methodsOfObject(helper).forEach((action) => {
let actionAlias = container.translation() ? container.translation().actionAliasFor(action) : action;
if (!actions[actionAlias]) {
let params = getParamNames(helper[action]);
if (params) params = params.join(', ');
if (!params) params = '';
methods.push(` ${(actionAlias)}: (${params}) => any; \n`);
actions[actionAlias] = 1
}
});
}
for (let name in suppportI) {
if (actions[name]) {
continue
}
let actor = suppportI[name];
let params = getParamNames(actor);
if (params) params = params.join(', ');
if (!params) params = '';
methods.push(` ${(name)}: (${params}) => any; \n`);
}
let definitionsTemplate = template.replace('{{methods}}', methods.join(''));
definitionsTemplate = definitionsTemplate.replace(/\{\{I\}\}/g, container.translation().I);

fs.writeFileSync(path.join(testsPath, 'steps.d.ts'), definitionsTemplate);
output.print('TypeScript Definitions provide autocompletion in Visual Studio Code and other IDEs');
output.print('Definitions were generated in steps.d.ts');
output.print('Load them by adding at the top of a test file:');
output.print(output.colors.grey(`\n/// <reference path="./steps.d.ts" />`));

codecept.teardown();
});
}
26 changes: 16 additions & 10 deletions lib/command/interactive.js
Expand Up @@ -14,15 +14,21 @@ module.exports = function (path, options) {
let testsPath = getTestRoot(path);
let config = getConfig(testsPath);
let codecept = new Codecept(config, options);
codecept.init(testsPath);
if (options.verbose) output.level(3);
codecept.init(testsPath, function(err) {
if (err) {
output.error('Error while running bootstrap file :' + err);
return;
}

output.print("String interactive shell for current suite...");
recorder.start();
event.emit(event.suite.before, {});
event.emit(event.test.before);
require('../pause')();
recorder.add(() => event.emit(event.test.after));
recorder.add(() => event.emit(event.suite.after, {}));
recorder.add(() => codecept.teardown());
if (options.verbose) output.level(3);

output.print("String interactive shell for current suite...");
recorder.start();
event.emit(event.suite.before, {});
event.emit(event.test.before);
require('../pause')();
recorder.add(() => event.emit(event.test.after));
recorder.add(() => event.emit(event.suite.after, {}));
recorder.add(() => codecept.teardown());
});
};
56 changes: 32 additions & 24 deletions lib/command/list.js
Expand Up @@ -13,30 +13,38 @@ module.exports = function (path) {
let testsPath = getTestRoot(path);
let config = getConfig(testsPath);
let codecept = new Codecept(config, {});
codecept.init(testsPath);
output.print('List of test actions: -- ');
let helpers = container.helpers();
let suppportI = container.support('I');
let actions = [];
for (let name in helpers) {
let helper = helpers[name];
methodsOfObject(helper).forEach((action) => {
let params = getParamNames(helper[action]);
codecept.init(testsPath, function(err) {
if (err) {
output.error('Error while running bootstrap file :' + err);
return;
}

output.print('List of test actions: -- ');
let helpers = container.helpers();
let suppportI = container.support('I');
let actions = [];
for (let name in helpers) {
let helper = helpers[name];
methodsOfObject(helper).forEach((action) => {
let params = getParamNames(helper[action]);
if (params) params = params.join(', ');
actions[action] = 1;
output.print(` ${output.colors.grey(name)} I.${output.colors.bold(action)}(${params})`);
});
}
for (let name in suppportI) {
if (actions[name]) {
continue
}
let actor = suppportI[name];
let params = getParamNames(actor);
if (params) params = params.join(', ');
actions[action] = 1;
output.print(` ${output.colors.grey(name)} I.${output.colors.bold(action)}(${params})`);
});
}
for (let name in suppportI) {
if (actions[name]) {
continue
if (!params) params = '';
output.print(` I.${output.colors.bold(name)}(${params})`);
}
let actor = suppportI[name];
let params = getParamNames(actor);
if (params) params = params.join(', ');
if (!params) params = '';
output.print(` I.${output.colors.bold(name)}(${params})`);
}
output.print('PS: Actions are retrieved from enabled helpers. ')
output.print('Implement custom actions in your helper classes.');
output.print('PS: Actions are retrieved from enabled helpers. ')
output.print('Implement custom actions in your helper classes.');

codecept.teardown();
});
};
9 changes: 6 additions & 3 deletions lib/command/run.js
Expand Up @@ -13,9 +13,12 @@ module.exports = function (suite, test, options) {
let config = getConfig(testRoot, configfile);
try {
let codecept = new Codecept(config, options);
codecept.init(testRoot);
codecept.loadTests();
codecept.run(test);
codecept.init(testRoot, function(err) {
if (err) throw new Error('Error while running bootstrap file :' + err);

codecept.loadTests();
codecept.run(test);
});
} catch (err) {
output.print('');
output.error(err.message);
Expand Down