Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha and ZombieJS

I'm starting a nodejs project and would like to do BDD with Mocha and Zombiejs. Unfortunately I'm new to just about every buzzword in that sentence. I can get Mocha and Zombiejs running tests fine, but I can't seem to integrate the two - is it possible to use Mocha to run Zombiejs tests, and if so, how would that look?

Just looking for "hello world" to get me started, but a tutorial/example would be even better.

Thanks!

like image 915
Joel Avatar asked Feb 19 '12 14:02

Joel


3 Answers

Assuming you already have installed mocha, zombie and expect.js according to instructions, this should work for you:

// Put below in a file in your *test* folder, ie: test/sampletest.js:

var expect = require('expect.js'),
Browser = require('zombie'),
browser = new Browser();

describe('Loads pages', function(){

    it('Google.com', function(done){

        browser.visit("http://www.google.com", function () {
            expect(browser.text("title")).to.equal('Google');
            done();
        });
    });

});

Then you should be able to run the mocha command from your root application folder:

# mocha -R spec

  Loads pages
    ✓ Google.com (873ms)


  ✔ 1 tests complete (876ms)

Note: If your tests keep failing due to timeouts, it helps to increase mocha's timeout setting a bit by using the -t argument. Check out mocha's documentation for complete details.

like image 167
Industrial Avatar answered Nov 20 '22 11:11

Industrial


I wrote a lengthy reply to this question explaining important gotchas about asynchronous tests, good practices ('before()', 'after()', TDD, ...), and illustrated by a real world example.

http://redotheweb.com/2013/01/15/functional-testing-for-nodejs-using-mocha-and-zombie-js.html

like image 40
François Zaninotto Avatar answered Nov 20 '22 10:11

François Zaninotto


if you want to use cucumber-js for your acceptance tests and mocha for your "unit" tests for a page, you can use cuked-zombie (sorry for the advertising).

Install it like described in the readme on github, but place your world config in a file called world-config.js

`/* globals __dirname */
var os = require('os');
var path = require('path');

module.exports = {
  cli: null,
  domain: 'addorange-macbook': 'my-testing-domain.com',
  debug: false
};

Then use mocha with zombie in your unit tests like this:

var chai = require('chai'), expect = chai.expect;
var cukedZombie = require('cuked-zombie');

describe('Apopintments', function() {

  describe('ArrangeFormModel', function() {
    before(function(done) { // execute once
      var that = this;

      cukedZombie.infectWorld(this, require('../world-config'));

      this.world = new this.World(done);

      // this inherits the whole world api to your test
      _.merge(this, this.world);
    });

    describe("display", function() {

      before(function(done) { // executed once before all tests are run in the discribe display block
        var test = this;
        this.browser.authenticate().basic('maxmustermann', 'Ux394Ki');

        this.visitPage('/someurl', function() {
          test.helper = function() {

          };

          done();
        });
      });

      it("something on the /someurl page is returned", function() {
        expect(this.browser.html()).not.to.be.empty;
      });
like image 1
pscheit Avatar answered Nov 20 '22 10:11

pscheit