jndvdcn jndvdcn - 6 months ago 39
Node.js Question

Can zombie test that an element does not exist?

I'm trying to use zombie.js (with mocha) on an express app to make sure some elements don't show on a page. Here's how I try to do this:

var app = require('../app).app, // this is express but you don't care
chai = require('chai'),
should = chai.should(),
Browser = require('zombie'),
browser = new Browser();

describe("page", function() {

it('should not have a the whatever element', function(done) {
browser.visit('http://localhost:3000', function() {
browser.query('#whatever').should.not.exist;
done();
});
});

});


Now when I run this test, it always fails:


  • if #whatever exists, I get this:

    expected <div class="whatever">whatever</div> to not exist

  • if #whatever doesn't exist, I would like the test to pass, but I also get an error

    TypeError: Cannot read property 'should' of null



Maybe this is a stupid test, but is there a way to make such a test in order to make it pass? Where am I doing wrong ?

Thx.

Answer

If anyone else encounters the same situation, I have found a solution to get around the problem: use chai expect instead of chai should.

The above code would be transformed this way:

var app = require('../app).app, // this is express but you don't care
    chai = require('chai'),
    expect = chai.expect,
    Browser = require('zombie'),
    browser = new Browser();

describe("page", function() {

    it('should not have a the whatever element', function(done) {
        browser.visit('http://localhost:3000', function() {
            expect(browser.query('#whatever')).not.to.exist;
            done();
        });
    });

});

The expect assertion will fail if the #whatever exists, otherwise it will pass.