How to setup my existing project with Jasmine?

1,343 views
Skip to first unread message

Josi

unread,
Jun 27, 2014, 5:35:01 PM6/27/14
to jasmi...@googlegroups.com
I'm trying to add `grunt-contrib-jasmine` into an existing project that's already using Grunt and having some trouble.

The Jasmine docs aren't super clear about how I should structure my project. And `npm install  grunt-contrib-jasmine --save-dev` didn't quite seem to do what it's supposed to.

It adds a `.grunt` directory (which the docs say nothing about), and then running `grunt jasmine` like the docs say to do next, fails and says `>> No "jasmine" targets found.` instead of what the docs say; "Automatically builds and maintains your spec runner and runs your tests headlessly through PhantomJS."

I figure that means I need to put something in my Gruntfile, but I can't find anything consistent in the docs about what to put there. I found a few snippets, but nothing seems to work quite right. I ended up adding;

grunt.initConfig({
    jasmine: {
      pivotal: {
        src: 'src/**/*.js',
        options: {
          specs: 'spec/*Spec.js',
          helpers: 'spec/*Helper.js'
        }
      }
    }
  });

Which kind of works. After running `grunt jasmine` again, it creates a `_SpecRunner.html` in the project root that links to a bunch of JS files (most of the things in the `.grunt` directory and all of the .js files in the `src` directory.) but the fails with a warning; "Warning: No specs executed, is there a configuration error?"

So, I manually created a `spec` directory with a `StarterSpec.js` that contains;

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

Now, running `grunt jasmine` runs that test and passes. Yay! But I'm still not testing my application, because it's source is in the project root, not the `src` directory. If I change the Jasmine config in my Gruntfile and set `src` to the root instead, `grunt jasmine` just returns pages of errors for all of my project files and dependencies.

I'm completely lost.

How do I get this set up correctly?

What should my project structure look like?

What's with the .grunt, and src directories?

What is the Spec Runner and what is it supposed to do? (Other than running specs, obviously. I want to run Jasmine from Grunt, so do I even need this HTML file?)

Any help would be greatly appreciated! I've already scoured the following docs;

Joshua Clanton

unread,
Jun 29, 2014, 11:14:33 AM6/29/14
to jasmi...@googlegroups.com
Hi Josi,

The `src` property should contain either a string representing the path to your source files, or an array of strings with the path to your source files. If your source files are in the root of your project, then you probably need to change to this: `src: '*.js'`. But since you also don't want to run tests on your gruntfile, you should add `'!Gruntfile.js'`. The final configuration would look something like this:

grunt.initConfig({
  jasmine: {
    pivotal: {
      src: ['*.js', '!Gruntfile.js'],

      options: {
        specs: 'spec/*Spec.js',
        helpers: 'spec/*Helper.js'
      }
    }
  }
});

The `.grunt` directory is a place for Grunt to keep temporary files if necessary. The Spec Runner html file is generated so that grunt-contrib-jasmine can load the HTML into phantomjs (a web browser without a visual interface) and run all of the specs. After a successful run, the Spec Runner html file will be deleted.

Hope this helps!

Josh Clanton
http://www.designpepper.com
Reply all
Reply to author
Forward
0 new messages