diff --git a/CHANGELOG.md b/CHANGELOG.md index 5024d178279c..ee703f2ae35a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -203,6 +203,7 @@ ### Chore & Maintenance - `[website]` Switch domain to https://jestjs.io ([#6549](https://github.com/facebook/jest/pull/6549)) +- `[tests]` Free tests from the dependency on value of FORCE_COLOR ([#6585](https://github.com/facebook/jest/pull/6585/files)) - `[tests]` Improve stability of `yarn test` on Windows ([#6534](https://github.com/facebook/jest/pull/6534)) - `[*]` Transpile object shorthand into Node 4 compatible syntax ([#6582](https://github.com/facebook/jest/pull/6582)) - `[*]` Update all legacy links to jestjs.io ([#6622](https://github.com/facebook/jest/pull/6622)) diff --git a/e2e/__tests__/coverage_report.test.js b/e2e/__tests__/coverage_report.test.js index 98cf792dac3e..66bbbd19d064 100644 --- a/e2e/__tests__/coverage_report.test.js +++ b/e2e/__tests__/coverage_report.test.js @@ -16,7 +16,9 @@ import runJest from '../runJest'; const DIR = path.resolve(__dirname, '../coverage-report'); test('outputs coverage report', () => { - const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage']); + const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage'], { + stripAnsi: true, + }); const coverageDir = path.resolve(__dirname, '../coverage-report/coverage'); // - the `setup.js` file is ignored and should not be in the coverage report. @@ -31,53 +33,71 @@ test('outputs coverage report', () => { }); test('collects coverage only from specified file', () => { - const {stdout} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--collectCoverageFrom', // overwrites the one in package.json - 'setup.js', - ]); + const {stdout} = runJest( + DIR, + [ + '--no-cache', + '--coverage', + '--collectCoverageFrom', // overwrites the one in package.json + 'setup.js', + ], + {stripAnsi: true}, + ); // Coverage report should only have `setup.js` coverage info expect(stdout).toMatchSnapshot(); }); test('collects coverage only from multiple specified files', () => { - const {stdout} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--collectCoverageFrom', - 'setup.js', - '--collectCoverageFrom', - 'OtherFile.js', - ]); + const {stdout} = runJest( + DIR, + [ + '--no-cache', + '--coverage', + '--collectCoverageFrom', + 'setup.js', + '--collectCoverageFrom', + 'OtherFile.js', + ], + {stripAnsi: true}, + ); expect(stdout).toMatchSnapshot(); }); test('collects coverage only from specified files avoiding dependencies', () => { - const {stdout} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--collectCoverageOnlyFrom', - 'Sum.js', - '--', - 'Sum.test.js', - ]); + const {stdout} = runJest( + DIR, + [ + '--no-cache', + '--coverage', + '--collectCoverageOnlyFrom', + 'Sum.js', + '--', + 'Sum.test.js', + ], + {stripAnsi: true}, + ); // Coverage report should only have `sum.js` coverage info expect(stdout).toMatchSnapshot(); }); test('json reporter printing with --coverage', () => { - const {stderr, status} = runJest('json-reporter', ['--coverage']); + const {stderr, status} = runJest('json-reporter', ['--coverage'], { + stripAnsi: true, + }); const {summary} = extractSummary(stderr); expect(status).toBe(1); expect(summary).toMatchSnapshot(); }); test('outputs coverage report as json', () => { - const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage', '--json']); + const {stdout, status} = runJest( + DIR, + ['--no-cache', '--coverage', '--json'], + {stripAnsi: true}, + ); expect(status).toBe(0); try { @@ -90,35 +110,43 @@ test('outputs coverage report as json', () => { }); test('outputs coverage report when text is requested', () => { - const {stdout, status} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--coverageReporters=text', - '--coverageReporters=html', - ]); + const {stdout, status} = runJest( + DIR, + [ + '--no-cache', + '--coverage', + '--coverageReporters=text', + '--coverageReporters=html', + ], + {stripAnsi: true}, + ); expect(status).toBe(0); expect(stdout).toMatch(/Stmts | . Branch/); expect(stdout).toMatchSnapshot(); }); test('outputs coverage report when text-summary is requested', () => { - const {stdout, status} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--coverageReporters=text-summary', - ]); + const {stdout, status} = runJest( + DIR, + ['--no-cache', '--coverage', '--coverageReporters=text-summary'], + {stripAnsi: true}, + ); expect(status).toBe(0); expect(stdout).toMatch(/Coverage summary/); expect(stdout).toMatchSnapshot(); }); test('outputs coverage report when text and text-summary is requested', () => { - const {stdout, status} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--coverageReporters=text-summary', - '--coverageReporters=text', - ]); + const {stdout, status} = runJest( + DIR, + [ + '--no-cache', + '--coverage', + '--coverageReporters=text-summary', + '--coverageReporters=text', + ], + {stripAnsi: true}, + ); expect(status).toBe(0); expect(stdout).toMatch(/Stmts | . Branch/); expect(stdout).toMatch(/Coverage summary/); @@ -126,11 +154,11 @@ test('outputs coverage report when text and text-summary is requested', () => { }); test('does not output coverage report when html is requested', () => { - const {stdout, status} = runJest(DIR, [ - '--no-cache', - '--coverage', - '--coverageReporters=html', - ]); + const {stdout, status} = runJest( + DIR, + ['--no-cache', '--coverage', '--coverageReporters=html'], + {stripAnsi: true}, + ); expect(status).toBe(0); expect(stdout).toMatch(/^$/); expect(stdout).toMatchSnapshot(); @@ -150,10 +178,10 @@ test('collects coverage from duplicate files avoiding shared cache', () => { 'Identical.test.js', ]; // Run once to prime the cache - runJest(DIR, args); + runJest(DIR, args, {stripAnsi: true}); // Run for the second time - const {stdout, status} = runJest(DIR, args); + const {stdout, status} = runJest(DIR, args, {stripAnsi: true}); expect(stdout).toMatchSnapshot(); expect(status).toBe(0); }); diff --git a/e2e/__tests__/coverage_threshold.test.js b/e2e/__tests__/coverage_threshold.test.js index bc1256308b7b..be071087ca5b 100644 --- a/e2e/__tests__/coverage_threshold.test.js +++ b/e2e/__tests__/coverage_threshold.test.js @@ -44,7 +44,9 @@ test('exits with 1 if coverage threshold is not met', () => { 'package.json': JSON.stringify(pkgJson, null, 2), }); - const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false'], { + stripAnsi: true, + }); const {rest, summary} = extractSummary(stderr); expect(status).toBe(1); @@ -79,7 +81,9 @@ test('exits with 1 if path threshold group is not found in coverage data', () => 'package.json': JSON.stringify(pkgJson, null, 2), }); - const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false'], { + stripAnsi: true, + }); const {rest, summary} = extractSummary(stderr); expect(status).toBe(1); @@ -117,7 +121,9 @@ test('exits with 0 if global threshold group is not found in coverage data', () 'package.json': JSON.stringify(pkgJson, null, 2), }); - const {stdout, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {stdout, status} = runJest(DIR, ['--coverage', '--ci=false'], { + stripAnsi: true, + }); expect(status).toBe(0); expect(stdout).toMatchSnapshot('stdout'); @@ -157,7 +163,9 @@ test('excludes tests matched by path threshold groups from global group', () => 'package.json': JSON.stringify(pkgJson, null, 2), }); - const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false'], { + stripAnsi: true, + }); const {rest, summary} = extractSummary(stderr); expect(status).toBe(1); @@ -198,7 +206,9 @@ test('file is matched by all path and glob threshold groups', () => { 'package.json': JSON.stringify(pkgJson, null, 2), }); - const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false'], { + stripAnsi: true, + }); const {rest, summary} = extractSummary( /* This test also runs on windows and when the glob fails it outputs the system specific absolute path to the test file. */ diff --git a/e2e/__tests__/find_related_files.test.js b/e2e/__tests__/find_related_files.test.js index 10b82c3cc832..2024e1dd97e5 100644 --- a/e2e/__tests__/find_related_files.test.js +++ b/e2e/__tests__/find_related_files.test.js @@ -63,7 +63,7 @@ describe('--findRelatedTests flag', () => { let stdout; let stderr; - ({stdout, stderr} = runJest(DIR)); + ({stdout, stderr} = runJest(DIR, [], {stripAnsi: true})); let summary; let rest; ({summary, rest} = extractSummary(stderr)); @@ -79,7 +79,9 @@ describe('--findRelatedTests flag', () => { // both a.js and b.js should be in the coverage expect(stdout).toMatchSnapshot(); - ({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js'])); + ({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js'], { + stripAnsi: true, + })); ({summary, rest} = extractSummary(stderr)); @@ -110,7 +112,9 @@ describe('--findRelatedTests flag', () => { let stdout; let stderr; - ({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js', 'b.js'])); + ({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js', 'b.js'], { + stripAnsi: true, + })); const {summary, rest} = extractSummary(stderr); expect(summary).toMatchSnapshot(); diff --git a/e2e/__tests__/module_name_mapper.test.js b/e2e/__tests__/module_name_mapper.test.js index 283b19d1bce4..84119593751a 100644 --- a/e2e/__tests__/module_name_mapper.test.js +++ b/e2e/__tests__/module_name_mapper.test.js @@ -20,7 +20,9 @@ test('moduleNameMapper wrong configuration', () => { }); test('moduleNameMapper correct configuration', () => { - const {stderr, status} = runJest('module-name-mapper-correct-config'); + const {stderr, status} = runJest('module-name-mapper-correct-config', [], { + stripAnsi: true, + }); const {rest} = extractSummary(stderr); expect(status).toBe(0); diff --git a/e2e/__tests__/transform.test.js b/e2e/__tests__/transform.test.js index 78c4b52e0839..619af7c52cf6 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -33,7 +33,9 @@ describe('babel-jest', () => { }); it('instruments only specific files and collects coverage', () => { - const {stdout} = runJest(dir, ['--coverage', '--no-cache']); + const {stdout} = runJest(dir, ['--coverage', '--no-cache'], { + stripAnsi: true, + }); expect(stdout).toMatch('Covered.js'); expect(stdout).not.toMatch('NotCovered.js'); expect(stdout).not.toMatch('ExcludedFromCoverage.js'); @@ -63,11 +65,11 @@ describe('no babel-jest', () => { }); test('instrumentation with no babel-jest', () => { - const {stdout} = runJest(tempDir, [ - '--no-cache', - '--coverage', - '--no-watchman', - ]); + const {stdout} = runJest( + tempDir, + ['--no-cache', '--coverage', '--no-watchman'], + {stripAnsi: true}, + ); expect(stdout).toMatch('Covered.js'); expect(stdout).not.toMatch('ExcludedFromCoverage.js'); // coverage result should not change @@ -92,7 +94,9 @@ describe('custom transformer', () => { }); it('instruments files', () => { - const {stdout, status} = runJest(dir, ['--no-cache', '--coverage']); + const {stdout, status} = runJest(dir, ['--no-cache', '--coverage'], { + stripAnsi: true, + }); // coverage should be empty because there's no real instrumentation expect(stdout).toMatchSnapshot(); expect(status).toBe(0); diff --git a/e2e/__tests__/typescript_coverage.test.js b/e2e/__tests__/typescript_coverage.test.js index dcc0ed13f300..cae19b1fc555 100644 --- a/e2e/__tests__/typescript_coverage.test.js +++ b/e2e/__tests__/typescript_coverage.test.js @@ -14,6 +14,8 @@ import runJest from '../runJest'; it('instruments and collects coverage for typescript files', () => { const dir = path.resolve(__dirname, '../typescript-coverage'); run('yarn', dir); - const {stdout} = runJest(dir, ['--coverage', '--no-cache']); + const {stdout} = runJest(dir, ['--coverage', '--no-cache'], { + stripAnsi: true, + }); expect(stdout).toMatchSnapshot(); }); diff --git a/e2e/runJest.js b/e2e/runJest.js index 8c3c2277d02c..dca157aebbd1 100644 --- a/e2e/runJest.js +++ b/e2e/runJest.js @@ -12,6 +12,7 @@ import path from 'path'; import fs from 'fs'; import execa, {sync as spawnSync} from 'execa'; import {Writable} from 'readable-stream'; +const stripAnsi = require('strip-ansi'); import {normalizeIcons} from './Utils'; const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js'); @@ -19,6 +20,7 @@ const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js'); type RunJestOptions = { nodePath?: string, skipPkgJsonCheck?: boolean, // don't complain if can't find package.json + stripAnsi?: boolean, // remove colors from stdout and stderr }; // return the result of the spawned process: @@ -47,13 +49,8 @@ export default function runJest( ); } - const env = options.nodePath - ? Object.assign({}, process.env, { - FORCE_COLOR: 0, - NODE_PATH: options.nodePath, - }) - : process.env; - + const env = Object.assign({}, process.env, {FORCE_COLOR: 0}); + if (options.nodePath) env['NODE_PATH'] = options.nodePath; const result = spawnSync(JEST_PATH, args || [], { cwd: dir, env, @@ -64,7 +61,9 @@ export default function runJest( result.status = result.code; result.stdout = normalizeIcons(result.stdout); + if (options.stripAnsi) result.stdout = stripAnsi(result.stdout); result.stderr = normalizeIcons(result.stderr); + if (options.stripAnsi) result.stderr = stripAnsi(result.stderr); return result; } @@ -120,12 +119,8 @@ export const until = async function( ); } - const env = options.nodePath - ? Object.assign({}, process.env, { - FORCE_COLOR: 0, - NODE_PATH: options.nodePath, - }) - : process.env; + const env = Object.assign({}, process.env, {FORCE_COLOR: 0}); + if (options.nodePath) env['NODE_PATH'] = options.nodePath; const jestPromise = execa(JEST_PATH, args || [], { cwd: dir, @@ -153,7 +148,9 @@ export const until = async function( result.status = result.code; result.stdout = normalizeIcons(result.stdout); + if (options.stripAnsi) result.stdout = stripAnsi(result.stdout); result.stderr = normalizeIcons(result.stderr); + if (options.stripAnsi) result.stderr = stripAnsi(result.stderr); return result; };