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

Allow to retry failed test from test context for #1773 #1989

Merged
merged 1 commit into from Dec 28, 2015

Conversation

longlho
Copy link
Contributor

@longlho longlho commented Nov 30, 2015

  • make retries run proper hooks
  • allow retries override at different levels

Review on Reviewable

@@ -108,6 +110,25 @@ Suite.prototype.timeout = function(ms) {
};

/**
* Set timeout `ms` or short-hand such as "2s".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc comments need updating?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@danielstjules
Copy link
Contributor

Looks like you may have accidentally deleted mocha.js as part of that commit? Could you amend and restore it?

@longlho
Copy link
Contributor Author

longlho commented Dec 1, 2015

@danielstjules my bad :) commit ammended

* @param {number} retry times
* @return {Mocha}
*/
Mocha.prototype.retires = function(retries) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here retires -> retries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the parameter name should be n? At least it is on your other methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah my bad, fixed

@longlho
Copy link
Contributor Author

longlho commented Dec 5, 2015

ping @boneskull @danielstjules

@longlho longlho force-pushed the 1773-retries branch 2 times, most recently from cadf34e to b09dc2a Compare December 9, 2015 15:46
@@ -0,0 +1,11 @@
describe('retries', function() {
this.retries(4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.retries(1) probably makes sense here?

@danielstjules
Copy link
Contributor

Great job on the tests :)

@longlho
Copy link
Contributor Author

longlho commented Dec 10, 2015

@danielstjules tks :) changes made

@danielstjules
Copy link
Contributor

Thanks! And can you git checkout 67d0facb5153feb7db4d8e161778436a75117189 mocha.js && git commit --amend Not sure why it's got a diff :)

@longlho longlho force-pushed the 1773-retries branch 3 times, most recently from d7aeaf6 to bdedd85 Compare December 10, 2015 05:38
@longlho
Copy link
Contributor Author

longlho commented Dec 10, 2015

done :)

@danielstjules
Copy link
Contributor

Playing around with this branch today and will let you know if I have anymore feedback :)

* Set the number of times to retry failed tests.
*
* @param {Number} retry times
* @return {Mocha}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate annotations here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I fixed that, but turns out there're actually a bunch of other methods w/ dupe annotations (timeout, enableTimeout...)

@danielstjules
Copy link
Contributor

Comments aside, the feature looks good to me. :) It behaves exactly as I'd expect:

  • afterEach hooks are ran after test failures, before a subsequent retry
  • Despite multiple retries, tests are only emitted once to reporters
  • .retries() precedence makes sense: cli < suite < test. As an example, this.retries(3); within a test body will take precedence over this.retries(1); at the suite level, or any --retries n cli arg
  • Works well with --bail

Integration tests validate that hook behavior is correct with retries. Unless @mochajs/mocha has any objections, I hope to merge this after the comments are fixed. I think it'll fit nicely with a new minor release! :)

@tinganho
Copy link
Contributor

I think mutating the tests variable in here is quite bad. Just think about it, an outside developer now need to learn this special rule. Although, IMHO this is an "ugly hack" to support retries.

@danielstjules
Copy link
Contributor

Just think about it, an outside developer now need to learn this special rule.

What special rule? A developer just needs to read runTests, and it's clear: var tests = suite.tests.slice(); https://github.com/longlho/mocha/blob/1773-retries/lib/runner.js#L436 It should be quite evident that it's a shallow copy of the suite.tests array :)

- make retries run proper hooks
- allow retries override at different levels
- expose currentRetry to reporters
@longlho
Copy link
Contributor Author

longlho commented Dec 28, 2015

@danielstjules changes made :)

@danielstjules
Copy link
Contributor

Awesome! Thanks again for all your work on this! I'm sure a lot of people are looking forward to using it :) Should be pretty helpful for browser-based acceptance test suites.

danielstjules added a commit that referenced this pull request Dec 28, 2015
Allow to retry failed test from test context for #1773
@danielstjules danielstjules merged commit d811eb9 into mochajs:master Dec 28, 2015
@longlho longlho deleted the 1773-retries branch December 28, 2015 17:55
@tinganho
Copy link
Contributor

tinganho commented Jan 6, 2016

Does this support retrying beforeEach and afterEach hooks too?

I have tests that does HTTP calls in beforeEach hook and afterEach hook and they stall sometimes.

@tinganho
Copy link
Contributor

tinganho commented Jan 6, 2016

Also maybe try to retry after and before hooks as well. I have some HTTP calls there too that have got stalled.

@danielstjules
Copy link
Contributor

Does this support retrying beforeEach and afterEach hooks too?

No, just tests, and I'd personally be opposed to the idea of retries for hooks. Hooks serve as setup/teardowns for tests, and can thus be used to cleanup before/after a failed test. For failed hooks (afterEach, after), you can't be sure of the state when retrying. If you have brittle logic in hooks, I'd suggest making a helper function and moving it to be invoked from the specs.

@ajaks328
Copy link
Contributor

Awesome! Just saw that this work has been merged and released. Been looking forward to this one. Thanks guys.

@aaronabramov
Copy link

regarding retries in before and after hooks.
would an API like this work?

afterEach(function() {
  if(this.isThereAnyErrorsInTheConsole()) {
    this.retry(); // fails the test and retries it again
  }
});

that's just an explicit retry call that doesn't happen automatically.

in my project, we check for react propTypes and other stuff validation errors after the test passed.
When running e2e tests it becomes very flaky, mostly because of some random errors (like an image didn't load, or some third party ads library freaked out and threw up in the console)
the current options are:

  1. Use an external helper and call it after every test (cons: people will forget to do it and there is not much you can do about it)
  2. Create a global wrapper global.it = wrapInConsoleOutputValidator(global.it) (cons: hacky as hell)

this is also related to this issue, i assume... #2120

@kashinross
Copy link

I'm currently using this on some of my tests and I love the functionality. I'm currently using this.retries(1) at the describe level but I noticed the retry logic always attempts to run the current it(). Is there any way to retry the entire describe block on a failure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants