nathanboktae / mocha-casperjs

Write CasperJS tests using Mocha
MIT License
120 stars 30 forks source link

Run mocha-casper in Gulp #91

Closed dschinkel closed 8 years ago

dschinkel commented 8 years ago

I'm struggling to figure out how I'd setup a gulp task to run this for me essentially:

mocha-casperjs test/features/specs/*-spec.js

I want it as a gulp task for obvious reasons, automation:

1) so I can run it on watch of my js file changes 2) I don't want to run it until the server has started and I've bundled files or on watch, I've re-bundled files. Instead I want my mocha-casper tests to run after bundling or start of server + bundling.

I've tried this in my gulpfile:

but then now, my tests bomb saying they no longer can resolve my casper object in my spec file:

gulp.task('mocha-acceptance', function() {
    process.env.PORT = 8000;

    return gulp.src([config.test.src.acceptance], { read: false })
        .pipe(mocha({
            reporter: config.test.mocha.reporter,
            ui: 'bdd',
            timeout: 30000
        }))
        .pipe(exec('**mocha-casperjs test/features/specs/*-spec.js**'))
});

viewing-companies-spec.js (just part of my code, but shows you how I'm using the casper object)

"use strict";

var phantomPage = null;

describe('Feature: View List of Companies', function() {
    before(function () {
        casper.start('http://localhost:6000');
        phantomPage = this;
    });

    it('When I go to the main landing page', function () {
        casper.then(function () {
            expect(phantomPage).to.be.a('object');
        });
    });

    casper.run(function() {
        exitPhantomJS();
    });
});

function exitPhantomJS(){
    this.exit();
}

Error in console:

Ready on port 6000

events.js:141
      throw er; // Unhandled 'error' event
      ^
ReferenceError: casper is not defined
    at Suite.<anonymous> (/Users/dave/code/projects/we-do-tdd/test/features/specs/viewing-companies-spec.js:41:5)

The above tests run green when I run it as an npm script straight from the command-line manually, but I want to run it as a gulp task now instead.

nathanboktae commented 8 years ago

There is 0 node.js code in mocha-phantomjs. It runs in casper which runs in phantomjs. it is not node.js. don't run it in node. Just exect the process. Something like:

gulp.task('mocha-acceptance', function() {
    process.env.PORT = 8000;

    return gulp.src([config.test.src.acceptance], { read: false })
              .pipe(exec('**mocha-casperjs test/features/specs/*-spec.js**'))
});
dschinkel commented 8 years ago

tried that and get:

Error in plugin 'gulp-exec'
Message:
    Command failed: /bin/sh -c mocha-casperjs test/features/specs/*-spec.js

Details:
    killed: false
    code: 1
    signal: null
    cmd: /bin/sh -c mocha-casperjs test/features/specs/*-spec.js

ran it a second time and got

Message:
    Command failed: /bin/sh -c **mocha-casperjs test/features/specs/*-spec.js**
/bin/sh: **mocha-casperjs: command not found

Details:
    killed: false
    code: 127
    signal: null
    cmd: /bin/sh -c **mocha-casperjs test/features/specs/*-spec.js**

I tried to then for troubleshooting run /bin/sh -c mocha-casperjs test/features/specs/*-spec.js in the console and it seemed to run ok...why would it be failing from Gulp?

Also I'm wondering why you're referring to mocha-phantomjs and wondered what you referring to as in "don't run it in node, it's not node.js". What was I trying to run in node and why not ? Trying to understand more than is inferred here by your sentence, can you be more detailed?

dschinkel commented 8 years ago

I decided to try gulp-shell and had better luck

.pipe(shell(['mocha-casperjs test/features/specs/*-spec.js']))

all good now!