{"id":19889546,"url":"https://github.com/tsmx/jest-process-exit","last_synced_at":"2026-03-17T12:03:11.262Z","repository":{"id":65550081,"uuid":"285809283","full_name":"tsmx/jest-process-exit","owner":"tsmx","description":"Testing process.exit with Jest in NodeJS","archived":false,"fork":false,"pushed_at":"2024-06-18T19:10:10.000Z","size":589,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T04:13:41.465Z","etag":null,"topics":["exit","jest","nodejs","process","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tsmx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-07T11:07:38.000Z","updated_at":"2024-07-03T16:06:48.000Z","dependencies_parsed_at":"2024-06-19T00:37:13.904Z","dependency_job_id":"d0f65dea-f161-4d2e-a393-39e98006a070","html_url":"https://github.com/tsmx/jest-process-exit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fjest-process-exit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fjest-process-exit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fjest-process-exit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fjest-process-exit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsmx","download_url":"https://codeload.github.com/tsmx/jest-process-exit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252081324,"owners_count":21691684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exit","jest","nodejs","process","testing"],"created_at":"2024-11-12T18:10:40.969Z","updated_at":"2026-03-17T12:03:06.217Z","avatar_url":"https://github.com/tsmx.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Testing process.exit with Jest in NodeJS\n\n## The scenario\n\nConsider you have the following function to be tested with [Jest](https://jestjs.io/):\n\n```javascript\nfunction myFunc() {\n    //\n    // ...do \"stuff\"\n    //\n    if (condition) {\n        process.exit(ERROR_CODE);\n    }\n    //\n    // ...do \"other stuff\"\n    //\n }\n```\n\n## Setting up the test case\n\nTo test the branch where `condition` is `true` you have to mock `process.exit` because otherwise the Jest test process would truly exit and therefore fail. Of course this mocking should have been done in a way that \"other stuff\" is never executed in this test.\n\nTo achieve that we use Jest's `spyOn` to implement a mock for `process.exit` that instead of exiting the process will throw en error.\n\n```javascript\nconst mockExit = jest.spyOn(process, 'exit')\n    .mockImplementation((number) =\u003e { throw new Error('process.exit: ' + number); });\n```\n\nThis ensures that the execution of our function ends immediately without doing \"other stuff\" and without ending the Jest test process. Also this mock serves to check if `process.exit` really was called and what the exit code was. We do this with Jest's `toHaveBeenCalledWith` test function.\n\nTo get the test case up and running we have to wrap our functions execution in an `expect( ... ).toThrow()` statement because it is now throwing an error by using the mock. Also it is a good practice to restore the original mocked function by calling `mockRestore` to avoid unintended side-effects.\n\nSo we have our final test case looking like that:\n\n```javascript\nit('tests myFunc with process.exit', async () =\u003e {\n    const mockExit = jest.spyOn(process, 'exit')\n        .mockImplementation((number) =\u003e { throw new Error('process.exit: ' + number); });\n    expect(() =\u003e {\n        myFunc(true);\n    }).toThrow();\n    expect(mockExit).toHaveBeenCalledWith(-1);\n    mockExit.mockRestore();\n});\n```\n\n## Try it out\n\n```\nnpm install\nnpm run test\n```\n\nHappy testing ;)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsmx%2Fjest-process-exit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsmx%2Fjest-process-exit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsmx%2Fjest-process-exit/lists"}