2017-11-20 7 views
1

내 오류 코드 캡처하지 않습니다 Function FunctionThatDoesNotExistsInString does not exists in string.를 : 오류가 실제로 발생 않았다 볼 수 있듯이오류가 발생하지만, 농담의`toThrow는()`오류 여기

FAIL build/__test__/FuncOps.CheckFunctionExistenceByString.test.js 
    ● 
    expect(CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    )).toThrow(); 


    Function FunctionThatDoesNotExistsInString does not exists in string. 

     at CheckFunctionExistenceByStr (build/FuncOps.js:35:15) 
     at Object.<anonymous> (build/__test__/FuncOps.CheckFunctionExistenceByString.test.js:12:51) 
      at new Promise (<anonymous>) 
      at <anonymous> 

합니다. 그러나 그것은 Jest에서 패스로 캡처되지 않습니다.

test(` 
    expect(CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    )).toThrow(); 
    `,() => { 
    expect(CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    )).toThrow(); 
    } 
); 

답변

1

expect(fn).toThrow()가 기대하는 기능를 호출 할 때 는, 예외가 발생, fn :

여기 내 코드입니다.

그러나 CheckFunctionExistenceByStr immediatelly를 호출하면 어설 션을 실행하기 전에 기능을 중지시킵니다.

교체

test(` 
    expect(CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    )).toThrow(); 
    `,() => { 
    expect(CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    )).toThrow(); 
    } 
); 

test(` 
    expect(() => { 
     CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    ) 
    }).toThrow(); 
    `,() => { 
    expect(() => { 
     CheckFunctionExistenceByStr(
     'any string', 'FunctionThatDoesNotExistsInString' 
    ) 
    }).toThrow(); 
    } 
);