2017-09-11 3 views
2

J'ai du code qui fait quelque chose commestubbing process.exit avec plaisanterie

function myFunc(condition){ 
    if(condition){ 
    process.exit(ERROR_CODE) 
    } 
} 

Comment puis-je tester cela en plaisantant? Écrasement exit à process avec jest.fn() et revenir en arrière après le test ne fonctionne pas, puisque le processus sort

Répondre

2

Vous pouvez utiliser jest.spyOn comme cela appellera la méthode originale ainsi:

const exit = jest.spyOn(process, 'exit'); 
//run your test 
expect(exit).toHaveBeenCalledWith('ERROR_CODE');