Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine, Grunt, RequireJS Stalling on the test output

I am trying to create a node boilerplate and am trying to create a task to run Jasmine test. I have the following configuration in my Gruntfile.js:

jasmine: {
  src : ['static/test/spec/**/*.js'],
  options: {
    host: 'http://localhost:<%= connect.test.port %>/',
    // specs : 'static/test/spec/**/*.js',
    template: require('grunt-template-jasmine-requirejs'),
    templateOptions: {
      requireConfigFile: 'static/test/SpecRunner.js',
      requireConfig: {
        baseUrl: './'
      }
    }
  }
},
connect: {
  test: {
    port: 8082
  }
}
....
grunt.registerTask('jasmine-test', ['connect', 'jasmine']);

When I run the task, I do not get any errors, however I do not get any further than this:

Running "connect:test" (connect) task
Started connect web server on localhost:8000.

Running "jasmine:src" (jasmine) task
Testing jasmine specs via phantom

The _SpecRunner.html file is created and when I view the file in the browser, I not only do not see any errors but I also see my jasmine test has run properly. What am I missing that makes the grunt task hang?

Cheers,

Kianosh

like image 700
Kianosh Avatar asked Nov 13 '22 03:11

Kianosh


1 Answers

I was able to get your example working just fine, and what you have here works ok. I did make a few modifications but I get the same result. What you are using is connect, which is spawning a local webserver, then the tests run in the browser. So, your task is not hanging, it's just running the sever.

But from what it sounds like, you probably want your tests to run in the terminal? If so, I have a pretty decent solution for you:

package.json

{

"name": "Jasmine Tests",
  "description": "Jasmine Testing",
  "version": "0.0.1",
  "devDependencies": {
    "grunt": "0.4.x",
    "grunt-contrib-watch": "~0.2.0",
    "grunt-contrib-jshint": "~0.4.3",
    "grunt-contrib-jasmine": "~0.4.2",
    "phantomjs": "1.8.2-0",
  }
}

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    watch: {
      grunt: {
        files: ["Gruntfile.js", "package.json"],
        tasks: "default"
      },
      javascript: {
        files: ["src/client/**/*.js", "specs/**/*Spec.js"],
        tasks: "test"
      }
    },
    jasmine: {
      src: "src/client/js/*.js",
      options: {
        specs: "specs/client/*Spec.js"
      }
    },
    jshint: {
      all: [
        "Gruntfile.js",
        "src/**/*.js",
        "spec/**/*.js"
      ],
      options: {
        jshintrc: ".jshintrc"
      }
    }
  });
  grunt.loadNpmTasks("grunt-contrib-watch");
  grunt.loadNpmTasks("grunt-contrib-jshint");
  grunt.loadNpmTasks("grunt-contrib-jasmine");
  grunt.registerTask("test", ["jshint", "jasmine"]);
  grunt.registerTask("default", ["test"]);
};

You can change the file stucture to whatever suits you. Setup both of these files run the following commands:

npm install

and

grunt test

or

grunt watch

Now I did add a few things, like jshint, and watch... watch is optional, but it's really nice to have. jshint is a must have in my opinon, but feel free to take it out of the solution.

The key really is phantomjs, which enables you to run these tests in a "phantom" browser, which outputs to the terminal.

You will also need to customize your directories to your likings.

I posted a good blog post on this (I go server side tests as well).

EDIT: You make also need a .jshintrc file if you opt to go that route.

.jshintrc

{
  "curly"   : true,
  "eqeqeq"  : true,
  "immed"   : true,
  "latedef" : true,
  "newcap"  : true,
  "noarg"   : true,
  "sub"     : true,
  "undef"   : true,
  "boss"    : true,
  "eqnull"  : true,
  "node"    : true,
  "es5"     : true,
  "globals" : {
    "it"         : false,
    "xit"        : false,
    "describe"   : false,
    "xdescribe"  : false,
    "beforeEach" : false,
    "afterEach"  : false,
    "expect"     : false,
    "spyOn"      : false
  }
}

Hope this helps.

like image 87
Patrick Avatar answered Dec 01 '22 00:12

Patrick