123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.3.0/jasmine.min.css">
            
          
!
            
              "use strict";
describe("Jasmineのテスト", function() {

  beforeEach(function () {
    this.name = "CODEPREP";
    this.person = { 
      name: "佐藤",
      address: null,
    };
  });

  describe("基本構文のテスト", function() {
    it("サービス名はCODEPREPであること", function() {
      expect(this.name).toBe("CODEPREP");
    });
    xit("テストをスキップ", function(){
      
    });
    
    describe("Matherのテスト", function() {
      beforeEach(function () {
        this.error = function(){
          throw new TypeError("how are you?");
        }
      });
      
      it("真偽値が正しく評価されること", function() {
        expect(true).toBe(true);
      });
      // 
      it("objectの比較toBe", function() {
        expect(this.person).toBe({ name: "佐藤",address: null });
      });
      it("objectの比較toEqual", function() {
        expect(this.person).toEqual({ name: "佐藤",address: null });
      });
      it("ageが未定義undefined", function() {
        expect(this.person.age).toBeUndefined('age');
      });
      it("addressがnull", function() {
        expect(this.person.address).toBeNull();
      });
      it("'name=佐藤'が含まれていること", function() {
        expect(this.person).toEqual(jasmine.objectContaining({
          name: "佐藤"
        }));
      });
      // error
      it("呼び出すと例外が発生", function() {
        expect(this.error).toThrowError();
      });
      it("例外の型がTypeError", function() {
        expect(this.error).toThrowError(TypeError);
      });
      it("例外のメッセージが'how are you?'", function() {
        expect(this.error).toThrowError('how are you?');
      });
      it("例外の型がTypeErrorで,メッセージが'how are you?'", function() {
        expect(this.error).toThrowError(TypeError, 'how are you?');
      });
    });
  });

  describe("関数のテスト", function() {
    beforeEach(function () {
      this.greet = {
        greeting: function(name) {
          return "Hello " + name;
        },
        greeting2: function(name) {
          return "Hello " + name;
        },
        greeting3: function(name) {
          return "Hello " + name;
        },
      }
      // spyOnは設定のみで実行はしない
      // returnValueでダミー返り値       
      spyOn(this.greet, "greeting").and.returnValue("Hi");
    });
    it("greeting関数が呼び出されたこと", function() {
      this.greet.greeting();
      expect(this.greet.greeting).toHaveBeenCalled();
    });
    it("greeting関数が1回呼び出されたこと", function() {
      this.greet.greeting();
      expect(this.greet.greeting).toHaveBeenCalledTimes(1);
    });
    it("正しく挨拶ができること", function() {
      expect(this.greet.greeting("everyone")).toBe("Hi");
    });
    it("ダミー関数を設定する", function() {
      spyOn(this.greet, "greeting2").and.callFake(function(name){
        return "Hello " + name;
      });
      expect(this.greet.greeting2("everyone")).toBe("Hello everyone");
    });
    it("ダミー例外を発生させる", function() {
      // 例外発生するのでコールバック関数を渡す
      spyOn(this.greet, "greeting3").and.throwError("Not Implemented");
      expect(function(){this.greet.greeting3("everyone")}.bind(this)).toThrowError("Not Implemented");
    });
     
        
    describe("モックオブジェクト", function() {
      beforeEach(function() {
        this.greet = jasmine.createSpyObj("greetMock", ["greeting", "goodby"]);

        this.greet.greeting("everyone");
        this.greet.goodby("mitz");
      });

      it("モックにgreetingとgoodby関数が定義されていること", function() {
        expect(this.greet.greeting).toBeDefined();
        expect(this.greet.goodby).toBeDefined();
      });
      it("greetingとgoodby関数がそれぞれ1回ずつ呼び出されたこと", function() {
        expect(this.greet.greeting).toHaveBeenCalledTimes(1);
        expect(this.greet.goodby).toHaveBeenCalledTimes(1);
      });
      it("greetingとgoodby関数の引数が正しいこと", function() {
        expect(this.greet.greeting).toHaveBeenCalledWith("everyone");
        expect(this.greet.goodby).toHaveBeenCalledWith("mitz");
      });
    });
  });

  describe("日時のテスト", function() {
    beforeEach(function() {
      jasmine.clock().install();
    });
    afterEach(function() {
      jasmine.clock().uninstall();
    });
    it("このブックの初版は2017/4/5であること", function() {
      var baseTime = new Date(2017, 4, 5);
      // 日時をモックに差し替える
      jasmine.clock().mockDate(baseTime);
      expect(new Date().getTime()).toEqual(baseTime.getTime());
    });
  });
  
  describe("非同期", function() {
    var result = null;
    beforeEach(function(done) {
      setTimeout(function(){
        result = "done";
        done();
      }, 1);
    });
    it("非同期処理の終了を待ってから次の処理を実行する(1ms後の処理を見る)", function() {
      expect(result).toBe("done");
    });
    
    describe("非同期とタイムアウト", function() {
      it("Jasmineのデフォルトのタイムアウト時間(5s)で失敗すること", function(done) {
        setTimeout(function() {
          done();
        }, 5001);
      });

      it("テストケースのタイムアウトを50msにして、doneが呼び出される前にタイムアウトで失敗すること", function(done) {
        setTimeout(function() {
          done();
        }, 100);
      }, 50);
    });
    
  });
  
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console