Complete example using Mocha
See original GitHub issueRelated to #772 , I was looking at examples on https://github.com/assaf/zombie/tree/master/test for a long example doing multiple interactions in a clear manner and I found that node has just so many ways to handle it, like promises, async/await, non-promises with done…
Looking at test/promises_test.js my understanding of promises was that it should avoid having to call done()/error(). Mocha is supposed to handle promises (not sure how yet). The calls to done()/error() puzzle me.
It seems to me the cleanest way to write most tests—so that if something fails it’s reported, else my tests just don’t report failures!!—is either using .should.eventually.
of Mocha or async/await like on test/window_test.js.
If you have an example doing a few interactions using what you’d consider the recommended way:
- Visit this
- Click that
- Fill that
- Check this is displayed
- Click there
- Check that
Ideally that example would save the source code snapshot of the page where a test or a click/browser operation failed.
Even this may sound like a lot, I believe most people are trying to achieve something like that and that’s what I used to do in Java+WebDriver+PhantomJS (with a screenshot), and you can find online, but it’s not as fast/lean/clean as Zombie.
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
After making some tests I found two good patterns and one bad.
Bad pattern IMO is using
(done)
callback because it’s easy to miss the error callback and then your tests will not correctly fail or may even never end. So I’d keep thefunction(done) {... done();}
usages to a minimum.Good patterns:
async
/await
of Babel experimental (or similar library) like in the test/form_test.jsdescribe()
before()
orbeforeEach()
(not in theit()
):I found http://redotheweb.com/2013/01/15/functional-testing-for-nodejs-using-mocha-and-zombie-js.html which is not bad but not that good to report errors well.
Instead I found that the Promise version works fine, as long as you don’t handle
done()
. For example this seems to handles errors well:The same written in CoffeeScript is pretty lean. Not as lean as your async example though.