如何从命令行在Node.js上运行Jasmine测试

时间:2014-01-27 21:46:15

标签: node.js jasmine jasmine-node

如何从命令行在Node.js上运行Jasmine测试?我已经通过npm安装了jasmine-node并编写了一些测试。我想在spec目录中运行测试并在终端中获得结果,这可能吗?

4 个答案:

答案 0 :(得分:90)

这可以让你快速前进:

  1. 安装Node.js(显然)。
  2. 接下来安装Jasmine。打开命令提示符并运行:

    npm install -g jasmine

  3. 接下来,cd到任意目录并设置示例'项目':

    jasmine init
    jasmine examples

  4. 现在运行您的单元测试:

    jasmine

  5. 如果您的jasmine.json文件在spec / support / jasmine.json之外的其他地方,只需运行:

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

    有关详细信息,请参阅:

答案 1 :(得分:24)

修改

似乎这不再是目前最好的答案,因为包没有维护。请参阅下面的答案


你可以这样做

来自您的测试目录

sudo npm install jasmine-node

这会将jasmine安装到../ node_modules / jasmine-node

然后

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

来自我的demo执行此操作

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

答案 2 :(得分:0)

最简单的方法是在项目根目录中运行命令:

$ npx humile

它会找到您所有以.spec.js结尾的规范。

如果您认为humile适合您的项目,只需将其安装为dev依赖项即可。它加快了命令的速度。

$ npm install -D humile

答案 3 :(得分:-9)

尝试Karma(以前的Testacular),它是由Angular.js团队完成的测试库不可知测试运行器

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

茉莉花的支持很好。

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