1
0

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.

Jasmineで可変エラーメッセージを掴むには

Posted at

jasmineは可変エラーメッセージに対応してない

公式ドキュメントより


it("The 'toThrowError' matcher is for testing a specific thrown exception", function() {
    var foo = function() {
      throw new TypeError("foo bar baz");
    };

    expect(foo).toThrowError("foo bar baz");
    expect(foo).toThrowError(/bar/);
    expect(foo).toThrowError(TypeError);
    expect(foo).toThrowError(TypeError, "foo bar baz");
  });

上記のように関数ポインタを渡す必要があるので以下のような関数をテストする際に困る。

testFunction(hoge) {
  if (condition) throw new Error(`Error Message ${hoge} !`);
}

解決方法

こちら参照

無名関数で囲んでやれば関数ポインタ渡すことになるよね?ということらしい。
なので、以下のようにテストすればいい。

it('適切なエラーが投げられていること', () => {
  expect(() => {testFunction('たまげた')}).toThrowError('Error Message たまげた !');
});

以上!!

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?