Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine and node.js

Have some Jasmine+Rhino combo to test the javascript code and trying to shift to node.js. However, couldn't find any setup instructions on the net (but only this link, with nearly zero instructions). Any help on how to make it true (on Ubuntu) will be highly appreciated.

like image 774
BreakPhreak Avatar asked Oct 02 '11 09:10

BreakPhreak


People also ask

What is Jasmine in node JS?

NODE AND BROWSER Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

Can Jasmine be integrated with node js?

We can install Jasmine for node. js by running npm install jasmine-node -g . We provide the -g option to install it globally, so we're able to run jasmine-node from the command line.

How do I test in node js with Jasmine?

In order to test a Node. js application, the jasmine framework needs to be installed first. This is done by using the Node package manager. The test code needs to be written in a separate file, and the word 'spec' should be appended to the file name.

Is Jasmine BDD or TDD?

The current home page of Jasmine says that it's “a behavior-driven development framework for testing JavaScript code.” So the intent of Jasmine is to be a BDD testing framework, per its authors. So, while the authors of Jasmine have intended it as a BDD testing framework, it can also be used with TDD and unit testing.


1 Answers

I thought the same thing (regarding documentation) when I first tried to use jasmine-node. As it turns out, though, there's virtually nothing to set up--it works just like RSpec or other testing tools you might be used to. To use Jasmine with your Node project, do the following:

  1. Make sure jasmine-node is installed and that you can run its executable.
  2. Write your specs! I have a sample spec below these steps.
  3. Run your specs with the command jasmine-node specs/ (where specs/ points to the directory with your specs).

That's it! You may find it beneficial to use some sort of build tool, like cake for CoffeeScript or jake.

Here's a quick example of part of a spec from a small project I used jasmine-node on recently; apologies that it's in CoffeeScript. (As an aside: to run jasmine-node against CoffeeScript specs, pass it the --coffee option.)

Chess   = require('../lib/chess')
Board   = Chess.Board
jasmine = require('jasmine-node')

describe "A chess board", ->
  beforeEach ->
    @board = new Board

  it "should convert a letter/number position into an array index", ->
    expect(Board.squares["a1"]).toEqual(0)
    expect(Board.squares["b1"]).toEqual(1)
    expect(Board.squares["a2"]).toEqual(16)
    expect(Board.squares["h8"]).toEqual(119)

  it "should know if an array index represents a valid square", ->
    expect(Board.is_valid_square 0).toBeTruthy()
    expect(Board.is_valid_square 7).toBeTruthy()
    expect(Board.is_valid_square 8).toBeFalsy()
    expect(Board.is_valid_square 15).toBeFalsy()
    expect(Board.is_valid_square 119).toBeTruthy()
    expect(Board.is_valid_square 120).toBeFalsy()
    expect(Board.is_valid_square 129).toBeFalsy()
    expect(Board.is_valid_square -1).toBeFalsy()

  it "should start off clear", ->
    for i in [0..127]
      if Board.is_valid_square(i)
        expect(@board.piece_on(i)).toBeNull()

  describe "#place_piece", ->
    it "should place a piece on the board", ->
      piece = jasmine.createSpy("piece")
      @board.place_piece "a1", piece
      expect(@board.piece_on "a1").toEqual(piece)

    it "should set the piece's location to the given square's index", ->
      piece = jasmine.createSpyObj(Piece, ["position"])
      @board.place_piece "b5", piece
      expect(piece.position).toEqual(65)

[Edit]

You can also add a spec_helper file (with the appropriate extension for your project) at the root of your specs/ directory. Here's the contents of mine, which adds a new matcher to Jasmine:

jasmine = require('jasmine-node')

beforeEach ->
 this.addMatchers
   toInclude: (should_include) ->
     for value in @actual
       return true if value == should_include
     false
like image 139
Michelle Tilley Avatar answered Sep 19 '22 18:09

Michelle Tilley