2017-08-02 10 views
0

私はMochaとChaiの単体テストを試みています。テストは、端末で正常に動作しますが、私は、ブラウザでtestrunner.htmlファイルをチェックするとき、それは空白で、ちょうど示し、「:0failures:0duration:渡し0」ブラウザのMochaテスト結果が空白になっています

をしかし、端末には示しています

$ mocha 

    Array 
    ✓ should start empty 

    1 passing (18ms) 
+1

下記の解決策を掲載しました。それは動作しましたか? –

+0

あなたが提供したコードのために働いた。しかし、私のテストのために働かなかった –

答えて

1

あなたのHTML内でこのためには、HTML

  1. mocha cssスタイルシートにリンクします。

    <link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" /> 
    

  2. id='mocha'divタグを書きます。テストはdivに挿入され、ブラウザで確認できます。


  3. <div id="mocha"></div> 
    

    はモカをロードするためのスクリプトタグを記述します。

    <script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script> 
    

  4. chaiのような他の依存関係をロードするためのスクリプトタグを記述します。

    <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.1.0/chai.js"></script> 
    

  5. セットアップ(あなたのテストを書いているかに応じて、またはTDD)モカBDDのAPI。

    <script>mocha.setup("bdd");</script> 
    

  6. (外部JavaScriptファイルにインラインまたはリンク)テストを書きます。

    BDD例:

    describe("addition", function() { 
        it("adds 1 and 1", function() { 
        expect(1 + 1).to.equal(2); 
        }); 
    }); 
    

  7. 実行モカ。

    mocha.run(); 
    

スニペット例

このスニペット

<!-- link to mocha css stylesheet --> 
 
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" /> 
 

 
<!-- write a div with id "mocha" for the output to be inserted into --> 
 
<div id="mocha"></div> 
 

 
<!-- load mocha framework --> 
 
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script> 
 

 
<!-- load any other libraries like the chai assertion library --> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.1.0/chai.js"></script> 
 

 

 
<!-- setup mocha to use the BDD interface --> 
 
<!-- Note: you can use TDD here instead --> 
 
<!-- depending on how you are writing your tests --> 
 
<!-- more information: http://mochajs.org/#tdd --> 
 
<script> 
 
    mocha.setup("bdd"); 
 
</script> 
 

 
<!-- write tests --> 
 
<script> 
 
    // access 'expect' from chai 
 
    var expect = chai.expect; 
 

 
    // write tests (BDD example) 
 
    describe("addition", function() { 
 
    it("adds 1 and 1", function() { 
 
     expect(1 + 1).to.equal(2); 
 
    }); 
 
    it("adds 1000 and 10", function() { 
 
     expect(1000 + 10).to.equal(1010); 
 
    });  
 
    }); 
 
    describe("subtraction", function() { 
 
    it("subtracts 1 from 1", function() { 
 
     expect(1 - 1).to.equal(0); 
 
    }); 
 
    it("subtracts 10 from 1000", function() { 
 
     expect(1000 - 10).to.equal(990); 
 
    });  
 
    }); 
 
    describe("multiplication", function() { 
 
    it("multiplies 1 by 1", function() { 
 
     expect(1 * 1).to.equal(1); 
 
    }); 
 
    it("multiplies 1000 by 10", function() { 
 
     expect(1000 * 10).to.equal(10000); 
 
    });  
 
    }); 
 
    describe("division", function() { 
 
    it("divides 1 by 1", function() { 
 
     expect(1/1).to.equal(1); 
 
    }); 
 
    it("divides 1000 by 10", function() { 
 
     expect(1000/10).to.equal(100); 
 
    });  
 
    });  
 
</script> 
 

 
<!-- run mocha --> 
 
<script> 
 
    mocha.run(); 
 
</script>

を実行してみてください

ここでデモ

  • はそんなにインラインJavaScriptを使用していないCodePen Demoです。

  • 役立ち情報公式ドキュメントでhere見つけることができますドキュメント。
関連する問題