Skip to content

Commit 07ba946

Browse files
committed
added async teardown option
1 parent cda961d commit 07ba946

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

lib/codecept.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Codecept {
2929
this.testFiles = [];
3030
}
3131

32-
init(dir, callback) {
32+
init(dir, callback) {
3333
// preparing globals
3434
global.codecept_dir = dir;
3535
global.output_dir = fsPath.resolve(dir, this.config.output);
@@ -50,7 +50,7 @@ class Codecept {
5050

5151
// loading bootstrap
5252
bootstrap(callback) {
53-
if (typeof this.config.bootstrap === 'string' && fileExists(fsPath.join(codecept_dir, this.config.bootstrap))) {
53+
if (typeof this.config.bootstrap === 'string' && fileExists(fsPath.join(codecept_dir, this.config.bootstrap))) {
5454
var bootstrap = require(fsPath.join(codecept_dir, this.config.bootstrap));
5555
if (typeof bootstrap === 'function') {
5656
bootstrap(callback);
@@ -64,15 +64,18 @@ class Codecept {
6464
}
6565

6666
// loading teardown
67-
teardown() {
67+
teardown(callback) {
6868
if (typeof this.config.teardown === 'string' && fileExists(fsPath.join(codecept_dir, this.config.teardown))) {
69-
var teardown = require(fsPath.join(codecept_dir, this.config.teardown));
70-
if (typeof teardown === 'function') {
71-
teardown();
72-
}
69+
var teardown = require(fsPath.join(codecept_dir, this.config.teardown));
70+
if (typeof teardown === 'function') {
71+
teardown(callback);
72+
return;
73+
}
7374
} else if (typeof this.config.teardown === 'function') {
74-
this.config.teardown();
75+
this.config.teardown(callback);
76+
return;
7577
}
78+
callback();
7679
}
7780

7881
loadTests(pattern) {
@@ -85,16 +88,16 @@ class Codecept {
8588
reporterOptions() {
8689
var reporterOptions = Object.assign(this.config.mocha.reporterOptions || {});
8790
if (this.opts.reporterOptions !== undefined) {
88-
this.opts.reporterOptions.split(",").forEach(function(opt) {
89-
var L = opt.split("=");
90-
if (L.length > 2 || L.length === 0) {
91-
throw new Error("invalid reporter option '" + opt + "'");
92-
} else if (L.length === 2) {
93-
reporterOptions[L[0]] = L[1];
94-
} else {
95-
reporterOptions[L[0]] = true;
96-
}
97-
});
91+
this.opts.reporterOptions.split(",").forEach(function (opt) {
92+
var L = opt.split("=");
93+
if (L.length > 2 || L.length === 0) {
94+
throw new Error("invalid reporter option '" + opt + "'");
95+
} else if (L.length === 2) {
96+
reporterOptions[L[0]] = L[1];
97+
} else {
98+
reporterOptions[L[0]] = true;
99+
}
100+
});
98101
}
99102
return reporterOptions;
100103

@@ -105,10 +108,11 @@ class Codecept {
105108
if (test) {
106109
this.mocha.files = this.mocha.files.filter((t) => fsPath.basename(t, '_test.js') === test || fsPath.basename(t, '.js') === test || fsPath.basename(t) === test);
107110
}
108-
this.mocha.run(() => {
109-
event.emit(event.all.result, this);
110-
}).on('end', () => {
111-
this.teardown();
111+
this.mocha.run().on('end', () => {
112+
let done = () => {
113+
event.emit(event.all.result, this);
114+
};
115+
this.teardown(done);
112116
});
113117
}
114118

lib/command/run.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ let getConfig = require('./utils').getConfig;
33
let getTestRoot = require('./utils').getTestRoot;
44
let Codecept = require('../codecept');
55
let output = require('../output');
6-
let codecept;
76

87
module.exports = function (suite, test, options) {
98
// registering options globally to use in config
109
process.profile = options.profile;
11-
let configfile = options.config;
10+
let configFile = options.config;
11+
let codecept;
1212

1313
let testRoot = getTestRoot(suite);
14-
let config = getConfig(testRoot, configfile);
14+
let config = getConfig(testRoot, configFile);
1515
try {
1616
codecept = new Codecept(config, options);
1717
codecept.init(testRoot, function (err) {

lib/command/utils.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,34 @@ module.exports.getTestRoot = function (currentPath) {
1010
return testsPath;
1111
};
1212

13-
module.exports.getConfig = function (testRoot, configfile) {
13+
module.exports.getConfig = function (testRoot, configFile) {
1414

1515
let config,
16-
manualConfigFile = configfile && path.resolve(configfile),
16+
manualConfigFile = configFile && path.resolve(configFile),
1717
jsConfigFile = path.join(testRoot, 'codecept.conf.js'),
1818
jsConfigFileDeprecated = path.join(testRoot, 'codecept.js'),
1919
jsonConfigFile = path.join(testRoot, 'codecept.json');
2020

21-
2221
if (isFile(manualConfigFile)) { // --config option provided
2322
if (path.extname(manualConfigFile) === '.js') {
2423
return configWithDefaults(require(manualConfigFile).config);
2524
}
2625
return configWithDefaults(JSON.parse(fs.readFileSync(manualConfigFile, 'utf8')));
2726
}
2827

29-
if (fileExists(jsConfigFile)) { // js config file
28+
if (isFile(jsConfigFile)) { // js config file
3029
return configWithDefaults(require(jsConfigFile).config);
3130
}
3231

33-
if (fileExists(jsConfigFileDeprecated)) { // deprecated js config file
32+
if (isFile(jsConfigFileDeprecated)) { // deprecated js config file
3433
console.log('Using codecept.js as configuration is deprecated, please rename it to codecept.conf.js');
3534
return configWithDefaults(require(jsConfigFileDeprecated).config);
3635
}
3736

38-
if (fileExists(jsonConfigFile)) { // json config provided
37+
if (isFile(jsonConfigFile)) { // json config provided
3938
return configWithDefaults(JSON.parse(fs.readFileSync(jsonConfigFile, 'utf8')));
4039
}
41-
output.error(`Can not load config from ${jsConfigFile}, ${jsonConfigFile} or ${manualConfigFile}\nCodeceptJS is not initialized in this dir. Execute 'codeceptjs init' to start`);
40+
output.error(`Can not load config from ${jsConfigFile}, ${jsonConfigFile} or ${manualConfigFile || 'manual config'}\nCodeceptJS is not initialized in this dir. Execute 'codeceptjs init' to start`);
4241
process.exit(1);
4342
};
4443

0 commit comments

Comments
 (0)