Skip to content

Commit

Permalink
Merge branch 'fix/stale-process' into ensure-scenario-termination
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Jun 7, 2024
2 parents 89275b2 + cbe818a commit 2f3134f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
26 changes: 15 additions & 11 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,24 @@ class Cli extends Base {
}
}

let stack = err.stack ? err.stack.split('\n') : [];
if (stack[0] && stack[0].includes(err.message)) {
stack.shift();
}
try {
let stack = err.stack ? err.stack.split('\n') : [];
if (stack[0] && stack[0].includes(err.message)) {
stack.shift();
}

if (output.level() < 3) {
stack = stack.slice(0, 3);
}
if (output.level() < 3) {
stack = stack.slice(0, 3);
}

err.stack = `${stack.join('\n')}\n\n${output.colors.blue(log)}`;
err.stack = `${stack.join('\n')}\n\n${output.colors.blue(log)}`;

// clone err object so stack trace adjustments won't affect test other reports
test.err = err;
return test;
// clone err object so stack trace adjustments won't affect test other reports
test.err = err;
return test;
} catch (e) {
throw Error(e);
}
});

const originalLog = Base.consoleLog;
Expand Down
2 changes: 1 addition & 1 deletion lib/command/gherkin/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = function (genPath) {
}

config.gherkin = {
features: './features/*.feature',
features: "./features/*.feature",
steps: [`./step_definitions/steps.${extension}`],
};

Expand Down
51 changes: 30 additions & 21 deletions lib/plugin/retryTo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const recorder = require('../recorder');
const store = require('../store');
const { debug } = require('../output');

const defaultConfig = {
Expand Down Expand Up @@ -73,40 +72,44 @@ const defaultConfig = {
* const retryTo = codeceptjs.container.plugins('retryTo');
* ```
*
*/
*/
module.exports = function (config) {
config = Object.assign(defaultConfig, config);
function retryTo(callback, maxTries, pollInterval = config.pollInterval) {
return new Promise((done, reject) => {
let tries = 1;

if (config.registerGlobal) {
global.retryTo = retryTo;
}
return retryTo;
function handleRetryException(err) {
recorder.throw(err);
reject(err);
}

function retryTo(callback, maxTries, pollInterval = undefined) {
let tries = 1;
if (!pollInterval) pollInterval = config.pollInterval;

let err = null;

return new Promise((done) => {
const tryBlock = async () => {
tries++;
recorder.session.start(`retryTo ${tries}`);
await callback(tries);
try {
await callback(tries);
} catch (err) {
handleRetryException(err);
}

// Call done if no errors
recorder.add(() => {
recorder.session.restore(`retryTo ${tries}`);
done(null);
});
recorder.session.catch((e) => {
err = e;

// Catch errors and retry
recorder.session.catch((err) => {
recorder.session.restore(`retryTo ${tries}`);
tries++;
if (tries <= maxTries) {
debug(`Error ${err}... Retrying`);
err = null;

recorder.add(`retryTo ${tries}`, () => setTimeout(tryBlock, pollInterval));
recorder.add(`retryTo ${tries}`, () =>
setTimeout(tryBlock, pollInterval)
);
} else {
done(null);
// if maxTries reached
handleRetryException(err);
}
});
};
Expand All @@ -121,4 +124,10 @@ module.exports = function (config) {
if (err) recorder.throw(err);
});
}

if (config.registerGlobal) {
global.retryTo = retryTo;
}

return retryTo;
};
12 changes: 12 additions & 0 deletions test/acceptance/retryTo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ Scenario('Should be succeed', async ({ I }) => {
I.see('.doesNotMatter');
}, 10);
});

Scenario('Should fail after reached max retries', async () => {
await retryTo(() => {
throw new Error('Custom pluginRetryTo Error');
}, 3);
});

Scenario('Should succeed at the third attempt @plugin', async () => {
await retryTo(async (tryNum) => {
if (tryNum < 2) throw new Error('Custom pluginRetryTo Error');
}, 3);
});
13 changes: 13 additions & 0 deletions test/plugin/plugin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@ describe('CodeceptJS plugin', function () {
done();
});
});

it('should retry to failure', (done) => {
exec(
`${config_run_config('codecept.Playwright.retryTo.js', 'Should fail after reached max retries')} --verbose`, (err, stdout) => {
const lines = stdout.split('\n');
expect(lines).toEqual(
expect.arrayContaining([expect.stringContaining('Custom pluginRetryTo Error')])
);
expect(err).toBeTruthy();
done();
}
);
});
});
8 changes: 4 additions & 4 deletions test/unit/plugin/retryto_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ describe('retryTo plugin', () => {
it('should execute few times command on fail', async () => {
let counter = 0;
let errorCaught = false;
await retryTo(() => {
recorder.add(() => counter++);
recorder.add(() => { throw new Error('Ups'); });
}, 5, 10);
try {
await retryTo(() => {
recorder.add(() => counter++);
recorder.add(() => { throw new Error('Ups'); });
}, 5, 10);
await recorder.promise();
} catch (err) {
errorCaught = true;
Expand Down

0 comments on commit 2f3134f

Please sign in to comment.