There are times when I'm debugging a flakey test and I can't get it to fail. Yes, that's a real problem!
If you're working on a test that intermittently fails, it's useful to have a snippet that will run your test again and again until it fails. This is both to get to the state that it's failing easier, and to prove that most likely, the bug is fixed (if it doesn't fail after 10 test runs, chances are it's no longer an issue).
While you could just manually re-run your test again and again until it fails, what if you're lazy like me and want to automatically restart the test if it passed?
Thanks to an answer via Stack Overflow, you can do just this.
This shell function can be added to your .bashrc
file (or whatever file your terminal program auto-loads on startup)
function untilfail() {
while $@; do :; done
}
Then, after sourcing that file (or restarting your terminal), you can use it to run your WDIO script:
$ untilfail npx wdio --spec=failingTest.js
Want to just run it a set number of times? Here's a different function you can use:
function run() {
number=$1
shift
for i in `seq $number`; do
$@
done
}
To use it to run wdio 5 times, pass in the number after the command:
$ run 5 npx wdio --spec=failingTest.js
Note that this second version won't stop running if there's a test failure. It'll keep running until you force it to stop, or it goes through the number of runs you requested.
Header Photo by Nathan Dumlao on Unsplash