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.

Using Mocha framework with Appium for automation, I am also using chai should for assertions, but when they fail mocha waits for the time out and does not throw the assertion error

See original GitHub issue

Sample code:

File 1 where should is being called:

var wd = require("wd");

require('colors');
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var should = chai.should();
chaiAsPromised.transferPromiseness = wd.transferPromiseness;

exports.should = should;

File 2 where I am using a should assertion:

'use strict';

require( 'colors' );
require( '../../helpers/setup' );
var alerts   = require( '../../helpers/alerts' );
var assert   = require( 'assert' );
//var expect   = require( 'expect' );
var caps     = require( '../../helpers/caps' );
var config   = require( '../../helpers/Config' );
var commons  = require( '../../helpers/Commons' );
var elements = require( '../../helpers/elements' );
var login    = require( '../../helpers/loginTable' );
var Store    = require( '../../helpers/Store' );

var driver = config.driver;

var truckOption;
var clockInOption;
var userRole;
var userName;
var name;
var permissionGranted;
var newJob;

describe( 'Start login Process'.green, function() {

    it( 'should login'.green, function( done ) {
        config.loginTest = true;
        return driver
        .elementByName( elements.loginScreen.client_account )
        .isDisplayed().should.eventually.be.true
        .elementByName( elements.loginScreen.user_name )
        .isDisplayed().should.eventually.be.true
        .elementByName( elements.loginScreen.password )
        .isDisplayed().should.eventually.be.true
        .elementByName( elements.loginScreen.login_button )
        .isDisplayed().should.eventually.be.true
        .elementByName( elements.loginScreen.accept_terms )
        .isDisplayed().should.eventually.be.true
        .elementByName( elements.loginScreen.client_account

So lets say 1st element is not displayed or returns false, should sees that and does not move on with the test, but at the same time does not throw the error why it failed or didnt move on

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
meebercommented, Jun 2, 2016

@keithamus @lucasfcosta I’d love your guys opinion on this one. Seems like a tricky issue.

Here are some tests with the latest versions of node, mocha, chai, chai-as-promised, and bluebird.

Note that the only combination that doesn’t produce a useful stack trace is native promises + chai-as-promised (regardless of chai.config.includeStack setting).

var BBPromise = require('bluebird');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');

BBPromise.config({longStackTraces: true});

chai.use(chaiAsPromised);
chai.should();

var expect = chai.expect;

describe('promise', function () {
  afterEach(function () {
    chai.config.includeStack = false;
  });

  it('native Promise', function () {
    var promise = Promise.resolve(42);
    return promise.then(function (val) {
      expect(val).to.equal(43);
    });
  });

  it('native Promise; chai-as-promised', function () {
    var promise = Promise.resolve(42);
    return expect(promise).to.eventually.equal(43);
  });

  it('native Promise; chai-as-promised; chai.config.includeStack', function () {
    chai.config.includeStack = true;

    var promise = Promise.resolve(42);
    return expect(promise).to.eventually.equal(43);
  });

  it('bluebird Promise; longStackTraces', function () {
    var promise = BBPromise.resolve(42);
    return promise.then(function (val) {
      expect(val).to.equal(43);
    });
  });

  it('bluebird Promise; chai-as-promised; longStackTraces', function () {
    var promise = BBPromise.resolve(42);
    return expect(promise).to.eventually.equal(43);
  });
});

Results:

promise
    1) native Promise
    2) native Promise; chai-as-promised
    3) native Promise; chai-as-promised; chai.config.includeStack
    4) bluebird Promise; longStackTraces
    5) bluebird Promise; chai-as-promised; longStackTraces


  0 passing (26ms)
  5 failing

  1) promise native Promise:

      AssertionError: expected 42 to equal 43
      + expected - actual

      -42
      +43

      at test/index.js:20:22

  2) promise native Promise; chai-as-promised:

      AssertionError: expected 42 to equal 43
      + expected - actual

      -42
      +43



  3) promise native Promise; chai-as-promised; chai.config.includeStack:

      AssertionError: expected 42 to equal 43
      + expected - actual

      -42
      +43

      at assertEqual (node_modules/chai/lib/chai/core/assertions.js:487:12)
      at ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
      at node_modules/chai-as-promised/lib/chai-as-promised.js:308:26

  4) promise bluebird Promise; longStackTraces:

      AssertionError: expected 42 to equal 43
      + expected - actual

      -42
      +43

      at test/index.js:39:22
  From previous event:
      at Context.<anonymous> (test/index.js:38:20)

  5) promise bluebird Promise; chai-as-promised; longStackTraces:

      AssertionError: expected 42 to equal 43
      + expected - actual

      -42
      +43

      at doAsserterAsyncAndAddThen (node_modules/chai-as-promised/lib/chai-as-promised.js:307:16)
      at .<anonymous> (node_modules/chai-as-promised/lib/chai-as-promised.js:255:21)
      at ctx.(anonymous function) [as equal] (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33)
      at Context.<anonymous> (test/index.js:45:42)



npm ERR! Test failed.  See above for more details.
1reaction
blueice349commented, May 31, 2016

Let me work on that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mochajs/mocha - Gitter
Anyone know how to throw an assertion error when using driver.waitForElementByName( element, 180000 ); I need it to wait the 180000 and if...
Read more >
Mocha Assertions: What They Are and How to Use Them
There are different things we can verify using assertions. In this post, you'll learn about assertions in Mocha and how to use them....
Read more >
node.js - Verify that an exception is thrown using Mocha / Chai ...
The problem with this approach is that (await fails()).should.throw(Error) doesn't make sense. await resolves a Promise .
Read more >
Errors on previously running Javascript/Mocha iOS Automation
“before each” hook for “Onboarding Login Test”: TypeError: driver.init is not a function at Context.beforeEach (login.test. · “after each” hook ...
Read more >
Selenium Wait Tutorial with All Strategies!
If we do not pay attention to Selenium Webdriver Wait in our projects, this will generally lead to non-reliable, intermittent, slow, and non- ......
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