LoginSignup
5
4

More than 5 years have passed since last update.

mochaのテストケースで条件によってスキップする方法

Posted at

JavaScriptテストフレームワークのmochaでBDDやTDDでテストケースを書いていて、実行環境/条件によって、特定のテストをスキップしたいときがありますが、その書き方のメモです。

karma/testemなどを利用して、複数のブラウザ環境でテストを実行するようにしていると、あるブラウザではまだサポートされていないAPIが絡むテストケースで失敗してしまいます。

そういった場合に、conditionを指定してdescribe/describe.skip, it/it.skipsuite/suite.skip, test/test.skipを振り分けるようにしています。

Inclusive tests - skipについて
http://visionmedia.github.io/mocha/#inclusive-tests

Asynchronous code のコードでdescribeを例にすると下記のようになります。
よりスマートに書けるかもしれないので一例として。

(window.indexedDB ? describe : describe.skip)("Connection", function() {
  var db = new Connection
    , tobi = new User("tobi")
    , loki = new User("loki")
    , jane = new User("jane");

  beforeEach(function(done){
    db.clear(function(err){
      if (err) return done(err);
      db.save([tobi, loki, jane], done);
    });
  })

  describe("#find()", function(){
    it("respond with matching records", function(done){
      db.find({ type: "User" }, function(err, res){
        if (err) return done(err);
        res.should.have.length(3);
        done();
      })
    })
  })
})
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4