toThrow(error?)
https://jestjs.io/docs/en/expect#tothrowerror
๋ณ์นญ์ผ๋ก .toThrowError(error?)
Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.
๊ผญ๊ผญ ๊ฐ์ธ์ค์ผํ๋ค.
it('@param precision์ด 1์์ 100์ฌ์ด๊ฐ ์๋๋ฉด RangeError๊ฐ ๋ฐ์ํ๋ค.', () => {
expect(() => { number.toPrecision(101) }).toThrow(RangeError);
});
๊ฐ์ธ์ง ์๊ณ , expect(number.toPrecision(101)).toThrow(RangeError);
์ด๋ ๊ฒ ์ผ๋๋ test๊ฐ ๋์ง ์์
Parameters
์ ๊ท์: error message๊ฐ ํจํด๊ณผ ์ผ์น
๋ฌธ์์ด: error message์ ํ์ ๋ฌธ์์ด์ด ํฌํจ๋จ
error object: error message๋ ๋์ผํ object์ message property
error class: error object๋ class์ ์ธ์คํด์ค
parameter๊ฐ ์๊ฐ๋ณด๋ค ๋ค์ํ๋ค
// Test that the error message says "yuck" somewhere: these are equivalent
expect(drinkOctopus).toThrowError(/yuck/);
expect(drinkOctopus).toThrowError('yuck');
// Test the exact error message
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));
// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
Last updated
Was this helpful?