Skip to content

Commit 5fb0f6a

Browse files
VorobeykoDavertMik
authored andcommitted
fix(multiple): async bootstrapAll must be called before creating chunks (codeceptjs#1738)
1 parent c81aa73 commit 5fb0f6a

File tree

4 files changed

+87
-39
lines changed

4 files changed

+87
-39
lines changed

lib/command/run-multiple.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,52 @@ module.exports = function (selectedRuns, options) {
6262
fail('No runs provided. Use --all option to run all configured runs');
6363
}
6464

65-
const done = () => event.emit(event.multiple.before, null);
66-
runHook(config.bootstrapAll, done, 'bootstrapAll');
67-
68-
if (options.config) { // update paths to config path
69-
if (config.tests) {
70-
config.tests = path.resolve(testRoot, config.tests);
65+
const done = () => {
66+
event.emit(event.multiple.before, null);
67+
if (options.config) { // update paths to config path
68+
if (config.tests) {
69+
config.tests = path.resolve(testRoot, config.tests);
70+
}
71+
if (config.gherkin && config.gherkin.features) {
72+
config.gherkin.features = path.resolve(testRoot, config.gherkin.features);
73+
}
7174
}
72-
if (config.gherkin && config.gherkin.features) {
73-
config.gherkin.features = path.resolve(testRoot, config.gherkin.features);
75+
76+
if (options.features) {
77+
config.tests = '';
7478
}
75-
}
7679

77-
if (options.features) {
78-
config.tests = '';
79-
}
80+
if (options.tests && config.gherkin) {
81+
config.gherkin.features = '';
82+
}
8083

81-
if (options.tests && config.gherkin) {
82-
config.gherkin.features = '';
83-
}
84+
const childProcessesPromise = new Promise((resolve, reject) => {
85+
processesDone = resolve;
86+
});
8487

85-
const childProcessesPromise = new Promise((resolve, reject) => {
86-
processesDone = resolve;
87-
});
88+
const runsToExecute = [];
89+
collection.createRuns(selectedRuns, config).forEach((run) => {
90+
const runName = run.getOriginalName() || run.getName();
91+
const runConfig = run.getConfig();
92+
runsToExecute.push(executeRun(runName, runConfig));
93+
});
8894

89-
const runsToExecute = [];
90-
collection.createRuns(selectedRuns, config).forEach((run) => {
91-
const runName = run.getOriginalName() || run.getName();
92-
const runConfig = run.getConfig();
93-
runsToExecute.push(executeRun(runName, runConfig));
94-
});
95+
if (!runsToExecute.length) {
96+
fail('Nothing scheduled for execution');
97+
}
9598

96-
if (!runsToExecute.length) {
97-
fail('Nothing scheduled for execution');
98-
}
99+
// Execute all forks
100+
totalSubprocessCount = runsToExecute.length;
101+
runsToExecute.forEach(runToExecute => runToExecute.call(this));
99102

100-
// Execute all forks
101-
totalSubprocessCount = runsToExecute.length;
102-
runsToExecute.forEach(runToExecute => runToExecute.call(this));
103+
return childProcessesPromise.then(() => {
104+
// fire hook
105+
const done = () => event.emit(event.multiple.after, null);
106+
runHook(config.teardownAll, done, 'teardownAll');
107+
});
108+
};
103109

104-
return childProcessesPromise.then(() => {
105-
// fire hook
106-
const done = () => event.emit(event.multiple.after, null);
107-
runHook(config.teardownAll, done, 'teardownAll');
108-
});
110+
runHook(config.bootstrapAll, done, 'bootstrapAll');
109111
};
110112

111113
function executeRun(runName, runConfig) {

lib/hooks.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ function loadCustomHook(module) {
4040

4141
function callSync(callable, done) {
4242
if (isAsync(callable)) {
43-
callAsync(callable, done);
44-
if (!hasArguments(callable)) done();
43+
callAsync(callable, done, hasArguments(callable));
4544
} else if (hasArguments(callable)) {
4645
callable(done);
4746
} else {
@@ -50,8 +49,16 @@ function callSync(callable, done) {
5049
}
5150
}
5251

53-
function callAsync(callable, done = undefined) {
54-
const called = done ? callable(done) : callable();
52+
function callAsync(callable, done, hasArgs = false) {
53+
let called = new Promise(() => {});
54+
55+
if (done) {
56+
if (hasArgs) called = callable(done);
57+
else called = callable().then(() => done());
58+
} else {
59+
called = callable();
60+
}
61+
5562
called.catch((err) => {
5663
output.print('');
5764
output.error(err.message);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const event = require('../../../lib/event');
2+
3+
exports.config = {
4+
tests: './*_test.js',
5+
timeout: 10000,
6+
output: './output',
7+
helpers: {
8+
FileSystem: {},
9+
},
10+
include: {},
11+
bootstrap: false,
12+
mocha: {},
13+
name: 'require test',
14+
multiple: {
15+
default: {
16+
browsers: ['chrome', { browser: 'firefox' }],
17+
},
18+
},
19+
bootstrapAll: async (done) => {
20+
Promise.resolve('inside Promise').then(res => console.log(`Results: ${res}`)).then(() => done());
21+
event.dispatcher.on(event.multiple.before, () => {
22+
console.log('"event.multiple.before" is called');
23+
});
24+
},
25+
teardownAll: async (done) => {
26+
console.log('"teardownAll" is called.');
27+
done();
28+
},
29+
};

test/runner/run_multiple_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ describe('CodeceptJS Multiple Runner', function () {
174174

175175
describe('bootstrapAll and teardownAll', () => {
176176
const _codecept_run = `run-multiple --config ${codecept_dir}`;
177+
it('should be executed from async function in config', (done) => {
178+
exec(`${runner} ${_codecept_run}/codecept.async.bootstrapall.multiple.code.js default`, (err, stdout, stderr) => {
179+
console.log(stdout);
180+
stdout.should.include('CodeceptJS'); // feature
181+
stdout.should.include('Results: inside Promise\n"event.multiple.before" is called');
182+
stdout.should.include('"teardownAll" is called.');
183+
assert(!err);
184+
done();
185+
});
186+
});
177187

178188
it('should be executed from function in config', (done) => {
179189
exec(`${runner} ${_codecept_run}/codecept.bootstrapall.multiple.code.js default`, (err, stdout, stderr) => {

0 commit comments

Comments
 (0)