Mocha and ZombieJS
Asked Answered
H

3

20

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!

Hereat answered 19/2, 2012 at 14:53 Comment(2)
Was my answer what you had in mind?Minima
Yup, that's what I wanted. Thanks!Hereat
M
37

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.

Minima answered 25/2, 2012 at 13:51 Comment(2)
be aware that zombiejs fails if used with nodejs 0.10.xx because of this issue github.com/assaf/zombie/issues/487 cheersYellowish
Be aware that issue seems to be fixed!Bedwarmer
R
7

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

Righteous answered 15/1, 2013 at 13:11 Comment(4)
Did you ever move this post?Philemon
Ah, I really wanted to read this too! Don't supposed you moved it to Posthaven, did you?Renita
Tee hee, zombie thread. Article moved: redotheweb.com/2013/01/15/…Brahmi
Fixed the broken link.Plourde
K
1

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;
      });
Keeler answered 5/6, 2014 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.