How to run Jasmine tests on Node.js from command line
Asked Answered
M

4

52

How do I run Jasmine tests on Node.js from command line? I have installed jasmine-node via npm and written some tests. I want to run tests inside the spec directory and get results in the terminal, is this possible?

Morgen answered 27/1, 2014 at 21:46 Comment(1)
Try this as well github.com/jasmine-contrib/grunt-jasmine-nodeGardel
H
23

EDIT

It seems this is no longer the current best answer as the package is unmaintained. Please see the answer below


You can do this

from your test directory

sudo npm install jasmine-node

This installs jasmine into ../node_modules/jasmine-node

then

../node_modules/jasmine-node/bin/jasmine-node --verbose --junitreport --noColor spec

which from my demo does this

Player - 5 ms
    should be able to play a Song - 2 ms

    when song has been paused - 1 ms
        should indicate that the song is currently paused - 0 ms
        should be possible to resume - 0 ms
    tells the current song if the user has made it a favorite - 1 ms

    #resume - 0 ms
        should throw an exception if song is already playing - 0 ms

Player - 5 ms
    should be able to play a Song - 2 ms

    when song has been paused - 1 ms
        should indicate that the song is currently paused - 0 ms
        should be possible to resume - 0 ms
    tells the current song if the user has made it a favorite - 1 ms

    #resume - 0 ms
        should throw an exception if song is already playing - 0 ms

Finished in 0.01 seconds
5 tests, 8 assertions, 0 failures, 0 skipped
Harville answered 27/1, 2014 at 23:8 Comment(2)
To those arriving via google: jasmine-node has had no new commits since 2014, only runs jasmine 1.3, and appears to have been abandoned. The official jasmine CLI in user64141's answer below is more up to date.Arellano
Avoid installing NPM packages with sudo. Only when really necessary, happens rarely.Jackijackie
U
95

This should get you going quickly:

  1. install Node.js (obviously).
  2. Next install Jasmine. Open a command prompt and run:

    npm install -g jasmine

  3. Next, cd to any directory and set up an example 'project':

    jasmine init
    jasmine examples

  4. Now run your unit tests:

    jasmine

If your jasmine.json file is somewhere else besides spec/support/jasmine.json, simply run:

jasmine JASMINE_CONFIG_PATH=relative/path/to/your/jasmine.json

For more info see:

Untrue answered 14/4, 2015 at 23:16 Comment(4)
This info should definitely be more prominently included on the jasmine homepage. At the moment you only find out if you go to the github project.Tugman
Doing exactly this on a blank "npm init" project still yields no results for me.Anagnorisis
This produces no output whatsoever.Unfriended
I think that last link wants to point here now: jasmine.github.io/setup/nodejs.htmlFiretrap
H
23

EDIT

It seems this is no longer the current best answer as the package is unmaintained. Please see the answer below


You can do this

from your test directory

sudo npm install jasmine-node

This installs jasmine into ../node_modules/jasmine-node

then

../node_modules/jasmine-node/bin/jasmine-node --verbose --junitreport --noColor spec

which from my demo does this

Player - 5 ms
    should be able to play a Song - 2 ms

    when song has been paused - 1 ms
        should indicate that the song is currently paused - 0 ms
        should be possible to resume - 0 ms
    tells the current song if the user has made it a favorite - 1 ms

    #resume - 0 ms
        should throw an exception if song is already playing - 0 ms

Player - 5 ms
    should be able to play a Song - 2 ms

    when song has been paused - 1 ms
        should indicate that the song is currently paused - 0 ms
        should be possible to resume - 0 ms
    tells the current song if the user has made it a favorite - 1 ms

    #resume - 0 ms
        should throw an exception if song is already playing - 0 ms

Finished in 0.01 seconds
5 tests, 8 assertions, 0 failures, 0 skipped
Harville answered 27/1, 2014 at 23:8 Comment(2)
To those arriving via google: jasmine-node has had no new commits since 2014, only runs jasmine 1.3, and appears to have been abandoned. The official jasmine CLI in user64141's answer below is more up to date.Arellano
Avoid installing NPM packages with sudo. Only when really necessary, happens rarely.Jackijackie
N
2

The easiest way is to run the command in your project root:

$ npx humile

It founds all your specs which name ends with .spec.js.

If you think humile is fine for your project, just install it as dev dependency. It speeds up the command.

$ npm install -D humile

Northrop answered 22/11, 2019 at 8:46 Comment(0)
S
-9

Try Karma (formerly Testacular), it is a testing library agnostic test runner done by Angular.js team

http://karma-runner.github.io/0.12/index.html

Jasmine support is well baked.

http://karma-runner.github.io/0.12/intro/how-it-works.html

Substitutive answered 19/3, 2014 at 9:49 Comment(1)
Karma doesn't run the tests in Node. See #16661170.Facial

© 2022 - 2024 — McMap. All rights reserved.