vitest
vitest copied to clipboard
Only with `--threads=false` option is `process.exitCode` affected
Describe the bug
For example:
import { test } from "vitest";
test("set exit code", () => {
// vitest/0.29.3
// default : pass
// --no-threads : fail
// --single-thread : pass
process.exitCode = 1;
});
Fails when --threads=false option is used, otherwise succeeds.
$ npx vitest run --threads=false
RUN v0.29.3 /home/kou029w/_/vitest-0.29.3
✓ test/set-exit-code.test.ts (1)
Test Files 1 passed (1)
Tests 1 passed (1)
Start at 00:52:53
Duration 277ms (transform 38ms, setup 0ms, collect 26ms, tests 2ms)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Child process exited unexpectedly with code 1
❯ ChildProcess.<anonymous> node_modules/.pnpm/[email protected]/node_modules/vitest/dist/chunk-node-pkg.7627b6fc.js:7637:18
❯ ChildProcess.emit node:events:513:28
❯ maybeClose node:internal/child_process:1091:16
❯ ChildProcess._handle.onexit node:internal/child_process:302:5
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
https://github.com/vitest-dev/vitest/blob/3573032875eac78eec55f37c7d42505bf2f0d4cb/packages/vitest/src/runtime/child.ts#L84
It appears to me that perhaps rewriting this line as follows should solve the problem:
process.exitCode = undefined;
procesExit();
But I did not know if this method was good or not.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-jhlx7e?file=vite.config.ts
System Info
System:
OS: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Memory: 15.96 GB / 23.14 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 18.15.0 - /usr/bin/node
Yarn: 1.22.19 - ~/.local/bin/yarn
npm: 9.5.0 - /usr/bin/npm
Browsers:
Chrome: 111.0.5563.64
Firefox: 111.0
npmPackages:
vitest: ^0.29.3 => 0.29.3
Used Package Manager
pnpm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
### Tasks
It seems that I have the same problem, have you found a workaround to avoid this?
EDIT
I managed to fix my test by mocking the process exit like so
it('should have an exit code', async () => {
vi.spyOn(process, 'exit').mockImplementationOnce((() => {}) as any);
// ... command changing exit code here
expect(process.exitCode).toBe(1);
// reset exit code
process.exitCode = undefined;
});