zombie icon indicating copy to clipboard operation
zombie copied to clipboard

Tests still pass when there's an AssertionError

Open chizmeeple opened this issue 11 years ago • 6 comments

This is somewhat unexpected. I expect assert() failures to cause test failures.

The test is:

browser.assert.element('form input[type="text"][id="email"]');

The test output is:

 Login Page
    ✓ should show login form
    ✓ should refuse empty submissions (93ms)
    ✓ should login successfully (1300ms)

  Account Create
    ✓ should be showing admin console screen
    ✓ should have «Accounts:Create» button
    ✓ should show input form when clicked

  Account Search
Unhandled rejection AssertionError: Expected 1 elements matching "form input[type="text"][id="email"]", found 0
    at Assert.<anonymous> (/Users/c.wright/node_modules/zombie/lib/assert.js:162:35)
    at Assert.elements (/Users/c.wright/node_modules/zombie/lib/assert.js:151:26)
    at Assert.element (/Users/c.wright/node_modules/zombie/lib/assert.js:136:12)
    at /Users/c.wright/development/COMM/cliff/test/mocha-zombie.js:74:28
    at tryCatcher (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/util.js:24:31)
    at Promise._settlePromiseFromHandler (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/promise.js:454:31)
    at Promise._settlePromiseAt (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/promise.js:530:18)
    at Promise._settlePromises (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/promise.js:646:14)
    at Async._drainQueue (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/async.js:177:16)
    at Async._drainQueues (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/async.js:187:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/c.wright/node_modules/zombie/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:358:17)
    ✓ should be showing admin console screen
    ✓ should have «Account search:View» button
    ✓ should show search form when clicked (50ms)

  Account View
    ✓ should be showing admin console screen
    ✓ should have «Account:View» button
    ✓ should ask for accountId when clicked
    ✓ should have asked one question; accountId


  13 passing (3s)

screen shot 2015-05-18 at 16 43 56

Am I doing something obviously wrong?

chizmeeple avatar May 18 '15 15:05 chizmeeple

If your Mocha test is it('some text', function() { assertion }) then you shouldn't have a problem. If it's anything more complicated, consider moving that code to before setup.

assaf avatar May 18 '15 18:05 assaf

I don't think I'm doing anything overly complicated. Is this something you would recommend refactoring in any way?

describe('Account Create', function() {
    before(function(done) {
        browser.visit('/', done);
    });

    const buttonSelector =
        'button[data-http-method="POST"][data-url-template="/accounts"]';

    it('should be showing admin console screen', function () {
        browser.assert.success();
        browser.assert.text('h1', 'Cliff - Service Admin Console');
    });

    it('should have «Accounts:Create» button', function () {
        browser.assert.element(buttonSelector);
        browser.assert.text(buttonSelector, 'Create');
    });


    it('should show input form when clicked', function() {
        browser.pressButton(buttonSelector).then(function () {
            // we expect the form to (still) exist
            browser.assert.element('form[id=schemaForm]');
            // it should have these elements
            // (it should have more, but this is a reasonable sanity check)
            browser.assert.element('form input[type="text"][id="email"]');
            browser.assert.element('form input[type="text"][id="firstName"]');
            browser.assert.element('form input[type="text"][id="lastName"]');
            browser.assert.element('form input[type="text"][id="gender"]');

            console.log(browser.html());

        });
    });
});

chizmeeple avatar May 19 '15 08:05 chizmeeple

That simple restructure would make it even simpler, and it will be easier to see where it goes wrong. You can compare it the samples at the top of the README and notice that your code is either missing a callback or not returning a promise.

assaf avatar May 19 '15 15:05 assaf

I am having a similar problem, but I'm using cucumber-js instead of mocha. Basically I am testing whether a user can logout, which means that he first has to login, then press a logout button. Pressing this button makes zombie print an AssertionError object, but my tests succeed. This is the output of the tests:

Feature: Student-logout

  As Student
  I want to be able to log out in order to be secure.

  Scenario:
    Given I am logged in with username "student001" and password "hunter2hunter2"
    Before assertion error...
    { [AssertionError: This browser has been destroyed]
    name: 'AssertionError',
    actual: null,
    expected: true,
    operator: '==',
    message: 'This browser has been destroyed',
    generatedMessage: false }
    When I press the indicator to logout
    Then I can log out

This is my code:

   this.Given(/^I am logged in with my username "([^"]*)" and password "([^"]*)"$/, function (username, password, callback) {
    var browser = this.browser;
    this.visit('/#/login', function() {
        browser
            .fill('username', username)
            .fill('password', password);
        browser.pressButton('login-submit', callback);
    });
});

this.When(/^I press the indicator to logout$/, function (callback) {
    console.log('Before assertion error...');
    this.browser.clickLink('Logout', callback);
});

this.Then(/^I can log out$/, function (callback) {
    this.browser.assert.className('#login-form-link', 'active');
    callback();
});

I hope you can help me with this!

lentus avatar Apr 28 '16 09:04 lentus

I guess this.visit closes the browser?

assaf avatar Apr 28 '16 15:04 assaf

I'm closing this issue, because none of these have anything to do with Zombie. Asynchronous code is hard, pay close attention to the control flow and when things are happening.

assaf avatar Apr 28 '16 15:04 assaf