Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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 たまげた !');
});

以上!!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away