2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mochaで各テストごとにテストデータを設定してbeforeEachで使う

2
Last updated at Posted at 2014-03-09

微妙に違う処理をbeforeEachでまとめたいんだけど、テストごとにデータ設定してもどうやって参照すればいいの・・・
なときに使えるスニペット。
テストのオブジェクトにプロパティ追加してるのが少し気になるけど、現状これ以上にスマートなやり方を思いつけない。

test.js
var message;

function setMessage(str) {
  message = "Hello " + str + "!";
}

describe('メッセージのセット', function() {
  beforeEach(function() {
    setMessage(this.currentTest.testData);
  });
  
  it('hoge', function() {
    expect(message).to.equal('Hello hoge!');
  }).testData = 'hoge';
  
  it('fuga', function() {
    expect(message).to.equal('Hello fuga!');
  }).testData = 'fuga';
});
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?