5

ノードボイラープレートを作成しようとしており、Jasmine テストを実行するタスクを作成しようとしています。Gruntfile.js に次の構成があります。

jasmine: {
  src : ['static/test/spec/**/*.js'],
  options: {
    host: 'http://localhost:<%= connect.test.port %>/',
    // specs : 'static/test/spec/**/*.js',
    template: require('grunt-template-jasmine-requirejs'),
    templateOptions: {
      requireConfigFile: 'static/test/SpecRunner.js',
      requireConfig: {
        baseUrl: './'
      }
    }
  }
},
connect: {
  test: {
    port: 8082
  }
}
....
grunt.registerTask('jasmine-test', ['connect', 'jasmine']);

タスクを実行してもエラーは発生しませんが、これ以上のことはありません。

Running "connect:test" (connect) task
Started connect web server on localhost:8000.

Running "jasmine:src" (jasmine) task
Testing jasmine specs via phantom

_SpecRunner.html ファイルが作成され、ブラウザーでファイルを表示すると、エラーが表示されないだけでなく、ジャスミン テストが適切に実行されていることがわかります。面倒なタスクがハングする原因は何ですか?

乾杯、

キアノシュ

4

1 に答える 1

1

私はあなたの例をうまく機能させることができました.ここにあるものはうまくいきます. いくつかの変更を加えましたが、同じ結果が得られます。使用しているのは、ローカル Web サーバーを生成する接続であり、テストはブラウザーで実行されます。したがって、タスクはハングしていません。サーバーを実行しているだけです。

しかし、どう思われるかというと、ターミナルでテストを実行したいのではないでしょうか? もしそうなら、私はあなたのためにかなりまともな解決策を持っています:

パッケージ.json

{

"name": "Jasmine Tests",
  "description": "Jasmine Testing",
  "version": "0.0.1",
  "devDependencies": {
    "grunt": "0.4.x",
    "grunt-contrib-watch": "~0.2.0",
    "grunt-contrib-jshint": "~0.4.3",
    "grunt-contrib-jasmine": "~0.4.2",
    "phantomjs": "1.8.2-0",
  }
}

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    watch: {
      grunt: {
        files: ["Gruntfile.js", "package.json"],
        tasks: "default"
      },
      javascript: {
        files: ["src/client/**/*.js", "specs/**/*Spec.js"],
        tasks: "test"
      }
    },
    jasmine: {
      src: "src/client/js/*.js",
      options: {
        specs: "specs/client/*Spec.js"
      }
    },
    jshint: {
      all: [
        "Gruntfile.js",
        "src/**/*.js",
        "spec/**/*.js"
      ],
      options: {
        jshintrc: ".jshintrc"
      }
    }
  });
  grunt.loadNpmTasks("grunt-contrib-watch");
  grunt.loadNpmTasks("grunt-contrib-jshint");
  grunt.loadNpmTasks("grunt-contrib-jasmine");
  grunt.registerTask("test", ["jshint", "jasmine"]);
  grunt.registerTask("default", ["test"]);
};

ファイル構造を自分に合ったものに変更できます。これらのファイルの両方をセットアップし、次のコマンドを実行します。

npm install

grunt test

また

grunt watch

jshint や watch など、いくつか追加しました... watch はオプションですが、あると便利です。jshint は私の意見では必須ですが、ソリューションから自由に削除してください。

鍵となるのは本当にphantomjsです。これにより、これらのテストを「ファントム」ブラウザーで実行して、ターミナルに出力できます。

また、好みに合わせてディレクトリをカスタマイズする必要があります。

私はこれについて良いブログ投稿を投稿しました (サーバー側のテストも行っています)。

編集:そのルートに行くことを選択した場合は、 .jshintrcファイルも必要になります。

.jsintrc

{
  "curly"   : true,
  "eqeqeq"  : true,
  "immed"   : true,
  "latedef" : true,
  "newcap"  : true,
  "noarg"   : true,
  "sub"     : true,
  "undef"   : true,
  "boss"    : true,
  "eqnull"  : true,
  "node"    : true,
  "es5"     : true,
  "globals" : {
    "it"         : false,
    "xit"        : false,
    "describe"   : false,
    "xdescribe"  : false,
    "beforeEach" : false,
    "afterEach"  : false,
    "expect"     : false,
    "spyOn"      : false
  }
}

お役に立てれば。

于 2013-06-22T15:02:22.707 に答える