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?