まだ Jasmine 1.x を使用している場合は、順次各メジャーバージョンにアップデートするよりも、直接最新のバージョン(現時点で 5.x)にスキップしたほうが簡単でしょう。特に

このアップグレードガイドの残りの部分は、2.0 リリース時の内容がほぼそのまま保持されています。

jasmine 2.0 のリリースでは、多くのことが変更されました

describe("Upgrading to jasmine 2.0", function() {

カスタムマッチャ

  describe("Custom Matchers", function() {
    beforeEach(function() {

addMatchers 関数は仕様(this)にはなくなり、グローバル jasmine オブジェクトになりました。

      /* was:
         this.addMatchers({
      */
      jasmine.addMatchers({

マッチャは少し異なる方法で設定されます。ファクトリは、jasmine の等価関数や登録されている customEqualityTesters などの要素を含む util オブジェクトを受け取ります。ファクトリは compare 関数を含むオブジェクトを返すことが想定されており、この関数は actualexpected を直接呼び出して、this に実際の値を置く代わりに呼び出されます

        /* was:
           toBeCustom: function(expected) {
             var passed = this.actual == expected;
        */
        toBeCustom: function(util, customEqualityTesters) {
          return {
            compare: function(actual, expected) {
              var passed = actual == expected

比較は passmessage 属性を持つオブジェクトを返す必要があります。

カスタムマッチャの使用の詳細については、こちらをご覧ください。このページは、1.x を 2.0 にアップグレードするために必要な変更を示すことを目的としています

              /* was:
                this.message = function() {
                  return [
                    'Expected ' + this.actual + ' to equal ' + expected,
                    'Expected ' + this.actual + ' not to equal ' + expected
                  ];
                };
                return passed;
                });
                */
              return {
                pass: passed,
                message: 'Expected ' + actual + (passed ? '' : ' not') + ' to equal ' + expected
              };
            }
          };
        }
      });
    });

カスタムマッチャの使用法は変わりません

    it("uses custom matchers", function() {
      expect(1).toBeCustom(1);
    });
  });

非同期仕様

  describe("Asynchronous Specs", function() {

以下のテストでは非同期とみなされます

    var asyncSetThing,
    somethingAsyncWithCallback = function(callback) {
      asyncSetThing = true;
      callback();
    };

runswaits、および waitsFor メソッドは、仕様の一部として実行される関数が done コールバックを受信して呼び出すことを可能にするために削除されました。

    /* was:
       it("calls an async thing and waits", function() {
         var asyncDone = false;
         somethingAsyncWithCallback(function() {
           asyncDone = true
         });

以前は仕様自体で非同期状態を追跡する必要がありました。

         waitsFor(function() {
           return asyncDone;
         });
     */

beforeEachafterEach、または itdone コールバックを受信することで、jasmine はその関数が呼び出されるまでキュー内の次の要素に移動しません。つまり、非同期ロジックも完了時にコールバックを取得した場合、jasmine の done はそのまま渡すことができ、jasmine は適切に待機します。

    beforeEach(function(done) {
      somethingAsyncWithCallback(done);
    });

    /*
       runs(function() {
         expect(asyncSetThing).toBeTruthy();
       });
     });
     */
    it("will wait until async completes and calls done", function() {
      expect(asyncSetThing).toBeTruthy();
    });
  });

スパイ

  describe("Spies", function() {
    it('should spy', function() {
      var spy = jasmine.createSpy('spy');

スパイに動作方法を指示する方法はすべて、スパイの属性ではなくなりました。スパイの動作がすべて記載されている単一の and 属性があるため、スパイされる関数に追加される属性は少なくなります。

       /* was:
        spy.andCallThrough();
        spy.andCallFake(function() {});
        spy.andThrow('error');
        spy.andReturn(1);
        */
      spy.and.callThrough();
      spy.and.callFake(function() {});
      spy.and.throwError('error');
      spy.and.returnValue(1);

基本的な設定とチェックは変わりません

      spy('foo');
      spy('bar');

      expect(spy).toHaveBeenCalledWith('foo');

同様に、より高度なコールチェックは calls 属性にあります

      /* was:
         expect(spy.mostRecentCall.args).toEqual(['bar']);
         expect(spy.callCount).toBe(2);
       */
      expect(spy.calls.mostRecent().args).toEqual(['bar']);
      expect(spy.calls.count()).toBe(2);
    });
  });

クロック

  describe("Clock", function() {

jasmineモッククロックはグローバルではなくインスタンス化されたオブジェクトで、useMockするように指示するのではなくinstallする必要があります。

    beforeEach(function() {
    /* was:
       jasmine.Clock.useMock();
     */
      jasmine.clock().install();
    });

クロックのtickは基本的には同じです

    it("uses the clock similarly", function() {
      /* was:
         jasmine.Clock.tick();
       */
      jasmine.clock().tick();
    });

Jasmine 2.0はアドオンから動的的にafterEachコールバックを追加する機能を削除しました。クロックが自分自身をアンインストールできる特殊なオブジェクトにならないように、手動でアンインストールする必要があります。

    afterEach(function() {
      jasmine.clock().uninstall();
    });
  });
});