Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: In Node 20, Jest exits with non-zero exit code if process.exitCode is set in the tested code #14501

Closed
garrettjstevens opened this issue Sep 7, 2023 · 7 comments

Comments

@garrettjstevens
Copy link

garrettjstevens commented Sep 7, 2023

Version

29.6.4

Steps to reproduce

git clone https://github.com/garrettjstevens/jest-20-exitcode-repro.git
cd jest-20-exitcode-repro/
yarn
yarn jest

Expected behavior

test('should exit cleanly', async () => {
  expect(async () => {
    process.exitCode = 1
    throw new Error('foo')
  }).rejects.toThrow('foo')
})

This is the code in the reproduction. I expect that the test will pass and Jest will exit with status code 0 since all the tests passed.

Actual behavior

In Node.js 18, Jest exits with code 0 and the output is:

yarn run v1.22.19
$ /home/garrett/Playground/jest-20-exitcode-repro/node_modules/.bin/jest
 PASS  ./exitCode.test.js
  ✓ should exit without errors (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.244 s, estimated 1 s
Ran all test suites.
Done in 0.54s.

In Node.js 20, Jest exits with code 1 and the output is:

yarn run v1.22.19
$ /home/garrett/Playground/jest-20-exitcode-repro/node_modules/.bin/jest
 PASS  ./exitCode.test.js
  ✓ should exit without errors (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.139 s, estimated 1 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Additional context

No response

Environment

# Usint Node.js 18
System:
  OS: Linux 5.15 Linux Mint 21.2 (Victoria)
  CPU: (8) x64 Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Binaries:
  Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
  Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.0/bin/yarn
  npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
  pnpm: 8.7.4 - ~/.nvm/versions/node/v18.18.0/bin/pnpm
npmPackages:
  jest: ^29.7.0 => 29.7.0

# Using Node.js 20
System:
  OS: Linux 5.15 Linux Mint 21.2 (Victoria)
  CPU: (8) x64 Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Binaries:
  Node: 20.8.0 - ~/.nvm/versions/node/v20.8.0/bin/node
  Yarn: 1.22.19 - ~/.nvm/versions/node/v20.8.0/bin/yarn
  npm: 10.1.0 - ~/.nvm/versions/node/v20.8.0/bin/npm
  pnpm: 8.7.4 - ~/.nvm/versions/node/v20.8.0/bin/pnpm
npmPackages:
  jest: ^29.7.0 => 29.7.0
@github-actions
Copy link

github-actions bot commented Oct 8, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 8, 2023
@garrettjstevens
Copy link
Author

I've tried with the latest release of Jest (v29.7.0) and Node.js 20 (v20.8.0) and the behavior has not changed.

@github-actions github-actions bot removed the Stale label Oct 9, 2023
@SimenB
Copy link
Member

SimenB commented Oct 10, 2023

Happy to take a PR which makes sure to ignore any process.exitCode set by the user. We already intercept exit() calls.

environment.global.process.exit = function exit(...args: Array<any>) {
const error = new ErrorWithStack(
`process.exit called with "${args.join(', ')}"`,
exit,
);
const formattedError = formatExecError(
error,
projectConfig,
{noStackTrace: false},
undefined,
true,
);
process.stderr.write(formattedError);
return realExit(...args);
};
}

Copy link

github-actions bot commented Nov 9, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 9, 2023
Drarig29 added a commit to DataDog/synthetics-ci-github-action that referenced this issue Nov 13, 2023
Drarig29 added a commit to DataDog/synthetics-ci-github-action that referenced this issue Nov 13, 2023
Drarig29 added a commit to DataDog/synthetics-ci-github-action that referenced this issue Nov 16, 2023
Copy link

github-actions bot commented Dec 9, 2023

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

1 similar comment
Copy link

github-actions bot commented Dec 9, 2023

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 9, 2023
Copy link

github-actions bot commented Jan 9, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants