question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complete example using Mocha

See original GitHub issue

Related 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:

  1. Visit this
  2. Click that
  3. Fill that
  4. Check this is displayed
  5. Click there
  6. 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:open
  • Created 9 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wernightcommented, Mar 11, 2015

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 the function(done) {... done();} usages to a minimum.

Good patterns:

  • Using Promises methods with async / await of Babel experimental (or similar library) like in the test/form_test.js
  • Using Promises methods directly in Mocha by having each method that returns a promise in a describe() before() or beforeEach() (not in the it()):
describe('Visit home page', function() {
  before(function() {
    return browser.visit('/');
  });
  describe('Click link', function() {
    before(function() {
      return browser.clickLink('a[class"add"]')
        .then(function() {
          return browser.clickLink('#something-else');
        });
    });
    it('should something', function() {
      browser.assert.text('title', 'something');
    });
  });
});
1reaction
wernightcommented, Oct 21, 2014

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:

describe('something', function() {
  it('test', function() {
    return browser.visit('/')
      .then(function() {
        browser.fill(...);
      }.then(function() {
        assert ....
      };
    }
  }
}

The same written in CoffeeScript is pretty lean. Not as lean as your async example though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started with Node.js and Mocha - Semaphore Tutorial
Mocha is a simple, extensible and fast testing library for Node.js. This article will walk you through its installation, configuration and usage.
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run ......
Read more >
Testing Node.js with Mocha and Chai - LogRocket Blog
In this comprehensive tutorial, we'll demonstrate how to test a Node.js app with Mocha, including writing our own test suites and running Mocha...
Read more >
Automated testing with Mocha - The Modern JavaScript Tutorial
Here in the tutorial we'll be using the following JavaScript libraries for tests: Mocha – the core framework: it provides common testing ...
Read more >
Working examples of common configurations using mocha ☕️
Examples · Apollo-Server GraphQL API · Async setup with --delay · Async setup with --file · Babel application · Karma · Programmatic usage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found