JavaScriptテストフレームワークのmochaでBDDやTDDでテストケースを書いていて、実行環境/条件によって、特定のテストをスキップしたいときがありますが、その書き方のメモです。
karma/testemなどを利用して、複数のブラウザ環境でテストを実行するようにしていると、あるブラウザではまだサポートされていないAPIが絡むテストケースで失敗してしまいます。
そういった場合に、conditionを指定してdescribe
/describe.skip
, it
/it.skip
やsuite
/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();
})
})
})
})