Mocha/Chai/Sinon のメモ

リファレンス

Mocha

Chai

Sinon

インストール (npm)

npm install --save-dev mocha
npm install --save-dev chai
npm install --save-dev sinon
  • 以下のファイルを使う
    • node_modules/mocha/mocha.js
    • node_modules/mocha/mocha.css
    • node_modules/chai/chai.js
    • node_modules/sinon/pkg/sinon.js

テスト例 (BDD)

var expect = require('chai').expect;
var user = { name: 'flatbird'};
describe('User', function() {
  it('should have a name', function() {
    // この関数内で例外が発生したらテスト失敗
    expect(user.name).to.exist;
    expect(user.name).to.be.a('string');
  });
});
  • 非同期
    • it の引数で done を取るだけで非同期になる。
    • 処理が終わったところで done() をコール。
  it('should be done 1 second later', function(done) {
     setTimeout(function() {
          done()
     }, 1000);
  });

テスト実行 (コマンド)

  • mocha コマンドを叩くと test/ フォルダ下のテストが自動的に実行される。
  • npm install -g でインストールしなかった場合は以下の場所にある。
node_modules/mocha/bin/mocha
  • node run test で実行する場合。
    • package.json に以下の記述を追加する。
"scripts": {
  "test": "node_modules/mocha/bin/mocha"
},

テスト実行 (ブラウザ)

<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="test/mocha.css" />
</head>
<body>
  <div id="mocha"></div>
  <script src="test/mocha.js"></script>
  <script src="test/chai.js"></script>
  <!-- Set up mocha -->
  <script>mocha.setup('bdd')</script>

  <!-- Test code should come here -->
  <script>
    describe('test example', function () {
      it('calculates well', function () {
        chai.expect(2 + 2).to.equal(4);
      });
    });
  </script>

  <!-- Call mocha.run() -->
  <script>
    window.onload = function() {
      mocha.run();
    };
  </script>
</body>
</html>

事前処理と事後処理

  • describe ブロックの中に書く。
  • beforeEach() / afterEach() … it で記述した各テストの実行前後に呼ばれる。
  • before() / after() … 各 describe の前と後に一回だけ呼ばれる。

Published: May 13 2014

  • category: