URL() 생성자는 던지고 canParse() 는 false, parse() 는 null — 같은 파싱인데 실패를 알리는 방식만 다르다1
URL.parse(url)메서드는 주어진 URL에 따라 새 URL 객체를 생성- 두 번째 파라미터 base는 상대 URL을 해석하기 위한 기준 URL로 사용되며, 이를 통해 URL의 경로가 올바르게 조정됨
- URL 객체나 다른 문자열을 파라미터로 사용할 수 있으며, 내부에서 문자열로 변환됨
describe('null 입력에 대한 URL API 동작 테스트', () => {
test('new URL(null)은 TypeError를 발생시킨다', () => {
expect(() => new URL(null)).toThrow(TypeError)
})
test('URL.canParse(null)은 false를 반환한다', () => {
expect(URL.canParse(null)).toBe(false)
})
test('URL.parse(null)은 null을 반환한다', () => {
expect(URL.parse(null)).toBe(null)
})
})
Footnotes
-
URL: parse() static method - MDN: “This is an alternative to using the URL() constructor to construct a
URLwithin a try…catch block, or using canParse() to check the parameters and returningnullif the method returnsfalse.” ↩