3

I am writing my unit tests for NodeJS using Jest. A part of my code exits using process.exit(1), so when trying to test it using Jest, the test terminates when it comes to this line with the error Command failed with exit code 1. which is the default behaviour of process.exit(1).

Can anyone please tell how to work with Jest to handle this scenario and continue with the other tests?

Canta
  • 1,480
  • 1
  • 13
  • 26
amal
  • 321
  • 5
  • 14
  • 1
    Does this answer your question? [stubbing process.exit with jest](https://stackoverflow.com/questions/46148169/stubbing-process-exit-with-jest) – Gus Feb 24 '20 at 10:59

1 Answers1

-1

I think, throwing an error instead gives a more maintainable and testable code for you. Like:

if (err) {
  throw new Error("Some relevant error msg")
}

If you insist on using process.exit(1), you can test for process.exitCode, which should be 1 in your case.

  • 1
    But process.exit(1) just terminates the test. It does not come back to my unit test where I can check for its value. Makes sense? – amal Aug 21 '18 at 18:25
  • 2
    Well, process.exit going to shut down nodejs. You can't prevent that. https://nodejs.org/api/process.html#process_event_exit – Máté Németh Aug 22 '18 at 07:16