Skip to content

Commit 6c6768d

Browse files
committed
Use FORCE_COLOR=0 for all tests
This change ensures that the tests work regardless of the value of FORCE_COLOR environment variable. There are environments where this variable is set to 1 and the tests fail. For example the tests fail when they are run from Jetbrains IDEs (Webstorm, Rubymine and etc).
1 parent 2278230 commit 6c6768d

9 files changed

+75
-51
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Chore & Maintenance
88

99
- `[website]` Switch domain to https://jestjs.io ([#6549](https://github.com/facebook/jest/pull/6549))
10+
- `[tests]` Free tests from the dependency on value of FORCE_COLOR ([#6585](https://github.com/facebook/jest/pull/6585/files))
1011

1112
## 23.2.0
1213

e2e/__tests__/coverage_report.test.js

+38-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const runJest = require('../runJest');
1616
const DIR = path.resolve(__dirname, '../coverage-report');
1717

1818
test('outputs coverage report', () => {
19-
const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage']);
19+
const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage'], {
20+
stripAnsi: true,
21+
});
2022
const coverageDir = path.resolve(__dirname, '../coverage-report/coverage');
2123

2224
// - the `setup.js` file is ignored and should not be in the coverage report.
@@ -31,39 +33,51 @@ test('outputs coverage report', () => {
3133
});
3234

3335
test('collects coverage only from specified file', () => {
34-
const {stdout} = runJest(DIR, [
35-
'--no-cache',
36-
'--coverage',
37-
'--collectCoverageFrom', // overwrites the one in package.json
38-
'setup.js',
39-
]);
36+
const {stdout} = runJest(
37+
DIR,
38+
[
39+
'--no-cache',
40+
'--coverage',
41+
'--collectCoverageFrom', // overwrites the one in package.json
42+
'setup.js',
43+
],
44+
{stripAnsi: true},
45+
);
4046

4147
// Coverage report should only have `setup.js` coverage info
4248
expect(stdout).toMatchSnapshot();
4349
});
4450

4551
test('collects coverage only from multiple specified files', () => {
46-
const {stdout} = runJest(DIR, [
47-
'--no-cache',
48-
'--coverage',
49-
'--collectCoverageFrom',
50-
'setup.js',
51-
'--collectCoverageFrom',
52-
'OtherFile.js',
53-
]);
52+
const {stdout} = runJest(
53+
DIR,
54+
[
55+
'--no-cache',
56+
'--coverage',
57+
'--collectCoverageFrom',
58+
'setup.js',
59+
'--collectCoverageFrom',
60+
'OtherFile.js',
61+
],
62+
{stripAnsi: true},
63+
);
5464

5565
expect(stdout).toMatchSnapshot();
5666
});
5767

5868
test('collects coverage only from specified files avoiding dependencies', () => {
59-
const {stdout} = runJest(DIR, [
60-
'--no-cache',
61-
'--coverage',
62-
'--collectCoverageOnlyFrom',
63-
'Sum.js',
64-
'--',
65-
'Sum.test.js',
66-
]);
69+
const {stdout} = runJest(
70+
DIR,
71+
[
72+
'--no-cache',
73+
'--coverage',
74+
'--collectCoverageOnlyFrom',
75+
'Sum.js',
76+
'--',
77+
'Sum.test.js',
78+
],
79+
{stripAnsi: true},
80+
);
6781

6882
// Coverage report should only have `sum.js` coverage info
6983
expect(stdout).toMatchSnapshot();
@@ -106,7 +120,7 @@ test('collects coverage from duplicate files avoiding shared cache', () => {
106120
runJest(DIR, args);
107121

108122
// Run for the second time
109-
const {stdout, status} = runJest(DIR, args);
123+
const {stdout, status} = runJest(DIR, args, {stripAnsi: true});
110124
expect(stdout).toMatchSnapshot();
111125
expect(status).toBe(0);
112126
});

e2e/__tests__/coverage_threshold.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ test('exits with 1 if coverage threshold is not met', () => {
4444
'package.json': JSON.stringify(pkgJson, null, 2),
4545
});
4646

47-
const {stdout, status} = runJest(DIR, ['--coverage', '--ci=false']);
47+
const {stdout, status} = runJest(DIR, ['--coverage', '--ci=false'], {
48+
stripAnsi: true,
49+
});
4850
expect(status).toBe(1);
4951
expect(stdout).toMatchSnapshot();
5052
});

e2e/__tests__/find_related_files.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('--findRelatedTests flag', () => {
6464
let stdout;
6565
let stderr;
6666

67-
({stdout, stderr} = runJest(DIR));
67+
({stdout, stderr} = runJest(DIR, [], {stripAnsi: true}));
6868
let summary;
6969
let rest;
7070
({summary, rest} = extractSummary(stderr));
@@ -80,7 +80,9 @@ describe('--findRelatedTests flag', () => {
8080
// both a.js and b.js should be in the coverage
8181
expect(stdout).toMatchSnapshot();
8282

83-
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']));
83+
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js'], {
84+
stripAnsi: true,
85+
}));
8486

8587
({summary, rest} = extractSummary(stderr));
8688

e2e/__tests__/module_name_mapper.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ test('moduleNameMapper wrong configuration', () => {
2020
});
2121

2222
test('moduleNameMapper correct configuration', () => {
23-
const {stderr, status} = runJest('module-name-mapper-correct-config');
23+
const {stderr, status} = runJest('module-name-mapper-correct-config', [], {
24+
stripAnsi: true,
25+
});
2426
const {rest} = extractSummary(stderr);
2527

2628
expect(status).toBe(0);

e2e/__tests__/transform.test.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ describe('babel-jest', () => {
3333
});
3434

3535
it('instruments only specific files and collects coverage', () => {
36-
const {stdout} = runJest(dir, ['--coverage', '--no-cache']);
36+
const {stdout} = runJest(dir, ['--coverage', '--no-cache'], {
37+
stripAnsi: true,
38+
});
3739
expect(stdout).toMatch('Covered.js');
3840
expect(stdout).not.toMatch('NotCovered.js');
3941
expect(stdout).not.toMatch('ExcludedFromCoverage.js');
@@ -63,11 +65,11 @@ describe('no babel-jest', () => {
6365
});
6466

6567
test('instrumentation with no babel-jest', () => {
66-
const {stdout} = runJest(tempDir, [
67-
'--no-cache',
68-
'--coverage',
69-
'--no-watchman',
70-
]);
68+
const {stdout} = runJest(
69+
tempDir,
70+
['--no-cache', '--coverage', '--no-watchman'],
71+
{stripAnsi: true},
72+
);
7173
expect(stdout).toMatch('Covered.js');
7274
expect(stdout).not.toMatch('ExcludedFromCoverage.js');
7375
// coverage result should not change
@@ -92,7 +94,9 @@ describe('custom transformer', () => {
9294
});
9395

9496
it('instruments files', () => {
95-
const {stdout, status} = runJest(dir, ['--no-cache', '--coverage']);
97+
const {stdout, status} = runJest(dir, ['--no-cache', '--coverage'], {
98+
stripAnsi: true,
99+
});
96100
// coverage should be empty because there's no real instrumentation
97101
expect(stdout).toMatchSnapshot();
98102
expect(status).toBe(0);

e2e/__tests__/typescript_coverage.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const runJest = require('../runJest');
1414
it('instruments and collects coverage for typescript files', () => {
1515
const dir = path.resolve(__dirname, '../typescript-coverage');
1616
run('yarn', dir);
17-
const {stdout} = runJest(dir, ['--coverage', '--no-cache']);
17+
const {stdout} = runJest(dir, ['--coverage', '--no-cache'], {
18+
stripAnsi: true,
19+
});
1820
expect(stdout).toMatchSnapshot();
1921
});

e2e/runJest.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const path = require('path');
1212
const fs = require('fs');
1313
const execa = require('execa');
1414
const {Writable} = require('readable-stream');
15+
const stripAnsi = require('strip-ansi');
1516
const {normalizeIcons} = require('./Utils');
1617

1718
const {sync: spawnSync} = execa;
@@ -21,6 +22,7 @@ const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
2122
type RunJestOptions = {
2223
nodePath?: string,
2324
skipPkgJsonCheck?: boolean, // don't complain if can't find package.json
25+
stripAnsi?: boolean, // remove colors from stdout and stderr
2426
};
2527

2628
// return the result of the spawned process:
@@ -49,13 +51,8 @@ function runJest(
4951
);
5052
}
5153

52-
const env = options.nodePath
53-
? Object.assign({}, process.env, {
54-
FORCE_COLOR: 0,
55-
NODE_PATH: options.nodePath,
56-
})
57-
: process.env;
58-
54+
const env = Object.assign({}, process.env, {FORCE_COLOR: 0});
55+
if (options.nodePath) env['NODE_PATH'] = options.nodePath;
5956
const result = spawnSync(JEST_PATH, args || [], {
6057
cwd: dir,
6158
env,
@@ -66,7 +63,9 @@ function runJest(
6663
result.status = result.code;
6764

6865
result.stdout = normalizeIcons(result.stdout);
66+
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
6967
result.stderr = normalizeIcons(result.stderr);
68+
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);
7069

7170
return result;
7271
}
@@ -118,12 +117,8 @@ runJest.until = async function(
118117
);
119118
}
120119

121-
const env = options.nodePath
122-
? Object.assign({}, process.env, {
123-
FORCE_COLOR: 0,
124-
NODE_PATH: options.nodePath,
125-
})
126-
: process.env;
120+
const env = Object.assign({}, process.env, {FORCE_COLOR: 0});
121+
if (options.nodePath) env['NODE_PATH'] = options.nodePath;
127122

128123
const jestPromise = execa(JEST_PATH, args || [], {
129124
cwd: dir,
@@ -151,7 +146,9 @@ runJest.until = async function(
151146
result.status = result.code;
152147

153148
result.stdout = normalizeIcons(result.stdout);
149+
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
154150
result.stderr = normalizeIcons(result.stderr);
151+
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);
155152

156153
return result;
157154
};

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0:
13241324
babylon "^6.18.0"
13251325
lodash "^4.17.4"
13261326

1327-
babel-traverse@^6.14.1, babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-traverse@^6.25.0, babel-traverse@^6.26.0:
1327+
babel-traverse@^6.0.0, babel-traverse@^6.14.1, babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-traverse@^6.25.0, babel-traverse@^6.26.0:
13281328
version "6.26.0"
13291329
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee"
13301330
dependencies:
@@ -1338,7 +1338,7 @@ babel-traverse@^6.14.1, babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-tr
13381338
invariant "^2.2.2"
13391339
lodash "^4.17.4"
13401340

1341-
babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26.0:
1341+
babel-types@^6.0.0, babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26.0:
13421342
version "6.26.0"
13431343
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497"
13441344
dependencies:

0 commit comments

Comments
 (0)