カスタムレポーター

組み込み jasmine レポーターの見た目が気に入らない場合は、いつでも独自のものを記述できます。

レポーターを作成する

jasmine レポーターは、適切な機能が使用可能な単なるオブジェクトです。カスタムレポーターを作成するときに、ここに示す関数のどれも必須ではありません。レポーターで指定されていない関数はすべて無視されます。

さらに、Jasmine 3.0 では、これらの関数はすべてスイートのコールバックと同じように非同期にすることができます。done コールバックを受け取るか Promise を返したり、async キーワードを使用できます。詳細については、非同期チュートリアルを参照してください。

レポーターインターフェースの詳細については、API ドキュメントを参照してください。

const myReporter = {
  jasmineStarted: function(suiteInfo) {
    console.log('Running suite with ' + suiteInfo.totalSpecsDefined);
  },

  suiteStarted: function(result) {
    console.log('Suite started: ' + result.description
      + ' whose full description is: ' + result.fullName);
  },

  specStarted: async function(result) {
    await somethingAsync();
    console.log('Spec started: ' + result.description
      + ' whose full description is: ' + result.fullName);
  },

  specDone: function(result) {
    console.log('Spec: ' + result.description + ' was ' + result.status);

    for (const expectation of result.failedExpectations) {
      console.log('Failure: ' + expectation.message);
      console.log(expectation.stack);
    }

    console.log(result.passedExpectations.length);
  },

  suiteDone: function(result) {
    console.log('Suite: ' + result.description + ' was ' + result.status);
    for (const expectation of result.failedExpectations) {
      console.log('Suite ' + expectation.message);
      console.log(expectation.stack);
    }
  },

  jasmineDone: function(result) {
    console.log('Finished suite: ' + result.overallStatus);
    for (const expectation of result.failedExpectations) {
      console.log('Global ' + expectation.message);
      console.log(expectation.stack);
    }
  }
};

レポーターを jasmine に登録する

jasmine.getEnv().addReporter(myReporter);

追加のメモ

カスタムレポーターを作成する場合は、Jasmine が報告できるすべての失敗モードを処理していることを確認する必要があります。注意: 一部の失敗の報告方法が Jasmine 3.0 で変更されました。

これを支援するために、レポーターに対してチェックできるいくつかの例 Jasmine スイートを用意しています。

失敗タイプには、jasmineDone に報告されるグローバルな失敗、suiteDone のスイートレベルの失敗、および specDone のスペックレベルの失敗があるはずです。レポーターは、ユーザーに対して合計 5 つのエラーを表示する必要があります。

除外は、fdescribed 以外のスペックを実行していないものとして報告する必要があります。Jasmine 3.0 でこのレポーターの用語は変更され、スイートの状態をより正確に表現するために excluded と呼ばれるようになりました。