-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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]: FORCE_COLOR
set internally leaks through to tests on Windows after upgrade to Node.js v18.17.0
#14391
Comments
I was able to reproduce some unexpected test failures on Mac too, when I upgraded Node to v18.17.0. A trigger might be tests running in parallel. The failures I was seeing went away when I tried |
jest/packages/jest-worker/src/workers/ChildProcessWorker.ts Lines 130 to 145 in 0fd5b1c
I had similar issue while testing a CLI tool. If I recall it right, all this is needed because of limitations of child process and The problem I was facing: At first I thought it would be enough to use worker threads ( Other part of issue was related with In the end my solution was to use |
Probably it would be the best to remove EDIT More motivation: color support is about the environment and this piece of data must be known before the app starts. E.g., |
From a quick look at just the |
The trick is that |
This seems to work perfectly: // program.js
console.log([ 123, 'abc', () => {} ]); // child.mjs
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
import { Chalk } from 'chalk';
const { JEST_CHALK_LEVEL } = process.env;
const chalk = new Chalk({ level: +JEST_CHALK_LEVEL });
console.log(chalk.blue('Hello world!'));
const { stdout } = await execFileAsync('node', ['program.js']);
console.log(stdout); // parent.mjs
import { fork } from 'node:child_process';
import { supportsColor } from 'chalk';
const JEST_CHALK_LEVEL = supportsColor ? String(supportsColor.level) : '0';
const env = { ...process.env, JEST_CHALK_LEVEL };
fork('child.mjs', [], { env }); Tested with: node parent.mjs # 'Hello world!' in color
FORCE_COLOR=1 node parent.mjs # everything in color
node parent.mjs --no-color # no color
|
Could even do delete process.env.JEST_WORKER_ID;
delete process.env.JEST_CHALK_LEVEL; after using the values to leave the environment untouched for tests and child processes spawned from them. |
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. |
I'll expand a bit on the failing tests I am seeing in Commander. We do not use Chalk or any ascii text colouring in Commander or in the tests. However, The failing test looks like const execFileAsync = util.promisify(childProcess.execFile);
test('example failing test', async() => {
const { stdout } = await execFileAsync('node', ['--inspect-port=9270', fixture, 'sub']);
expect(stdout).toBe("[ '--inspect-port=127.0.0.1:9271' ]\n");
}); The fixture effectively does this: childProcess.spawn('node', [ '--inspect-port=127.0.0.1:9271', 'sub.js'], { stdio: 'inherit' }) The final file is just:
I have not been able to reproduce the failure in a smaller setup, so I might be missing some critical step or parallel test to trigger the problem condition. |
This is still relevant. |
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. |
I worked around the colour issue in Commander by adding |
Possibly related: #12162 |
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. |
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. |
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. |
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. |
Version
29.6.2
Steps to reproduce
FORCE_COLOR
environment variable is unset.Expected behavior
I expect the tests to pass.
Actual behavior
Instead, both of them fail because the value of
process.env.FORCE_COLOR
is'1'
.Additional context
The issue is not present when using Node.js versions prior to v18.17.0. I believe the fact it appears after the upgrade is due to nodejs/node#48034.
The upgrade broke tests in tj/commander.js checking the output of
node
child processes, and it was quite hard to figure out what the reason was.Environment
The text was updated successfully, but these errors were encountered: