2017-11-20 1 views
1

Hier ist mein Fehlercodes:Fehler werden ausgelöst, aber Jest des `toThrow()` nicht erfasst den Fehler

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> 

Wie Sie der Fehler tatsächlich aufgetreten ist sehen: Function FunctionThatDoesNotExistsInString does not exists in string.. Es wird jedoch nicht als ein Pass in Jest erfasst.

Hier ist mein Codes:

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

Antwort

1

expect(fn).toThrow() erwartet eine Funktion fn, die wenn genannt, eine Ausnahme auslöst.

Sie rufen jedoch sofort CheckFunctionExistenceByStr, was bewirkt, dass die Funktion vor dem Ausführen der Assert zu werfen. Ersetzen

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

mit

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