51

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

4

4 回答 4

94

这应该让你快速前进:

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

    npm install -g jasmine

  3. 接下来,cd 到任何目录并设置一个示例“项目”:

    jasmine init
    jasmine examples

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

    jasmine

如果您的 jasmine.json 文件位于 spec/support/jasmine.json 之外的其他位置,只需运行:

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

有关更多信息,请参阅:

于 2015-04-14T23:16:46.690 回答
23

编辑

由于该软件包未维护,因此这似乎不再是当前的最佳答案。请看下面的答案


你可以这样做

从你的测试目录

sudo npm install jasmine-node

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

然后

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

从我的演示中可以做到这一点

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
于 2014-01-27T23:08:23.213 回答
1

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

$ npx humile

它会找到您所有名称以.spec.js.

如果您认为谦虚适合您的项目,只需将其安装为开发依赖项。它加快了命令的速度。

$ npm install -D humile

于 2019-11-22T08:46:48.257 回答
-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

于 2014-03-19T09:49:48.057 回答