From 3124c91c99bb0ada6c242133d0da35ee7a2740fd Mon Sep 17 00:00:00 2001 From: Aleksei Gusev Date: Mon, 18 Sep 2017 18:55:27 +0300 Subject: [PATCH] Chore: Free tests from FORCE_COLOR environment variable dependency (#4381) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Summary** FORCE_COLOR environment variable allows to force yarn to use colors. This makes some of tests fail because of comparing colorized output with plain strings. For example, if you run a test in environment where FORCE_COLOR is set you get this failure: ``` FAIL __tests__\index.js ● should add package expect(received).toEqual(expected) Expected value to equal: "success Saved lockfile." Received: success Saved lockfile." ``` **Test plan** Run tests in an environment where `FORCE_COLOR` is enabled. They should pass. --- __tests__/index.js | 9 +++++---- __tests__/integration.js | 5 +++++ __tests__/lifecycle-scripts.js | 9 +++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/__tests__/index.js b/__tests__/index.js index 8b415b1763..58458c120b 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -32,14 +32,15 @@ async function execCommand( const cacheDir = path.join(workingDir, '.yarn-cache'); return new Promise((resolve, reject) => { + const cleanedEnv = {...process.env}; + cleanedEnv['YARN_SILENT'] = 0; + delete cleanedEnv['FORCE_COLOR']; + exec( `node "${yarnBin}" --cache-folder="${cacheDir}" ${cmd} ${args.join(' ')}`, { cwd: workingDir, - env: { - ...process.env, - YARN_SILENT: 0, - }, + env: cleanedEnv, }, (error, stdout) => { if (error) { diff --git a/__tests__/integration.js b/__tests__/integration.js index 43646ffb06..ee2f93eb2b 100644 --- a/__tests__/integration.js +++ b/__tests__/integration.js @@ -71,6 +71,11 @@ const PORT_RANGE = MAX_PORT_NUM - MIN_PORT_NUM; const getRandomPort = () => Math.floor(Math.random() * PORT_RANGE) + MIN_PORT_NUM; async function runYarn(args: Array = [], options: Object = {}): Promise> { + if (!options['env']) { + options['env'] = {...process.env}; + options['extendEnv'] = false; + } + delete options['env']['FORCE_COLOR']; const {stdout, stderr} = await execa(path.resolve(__dirname, '../bin/yarn'), args, options); return [stdout, stderr]; diff --git a/__tests__/lifecycle-scripts.js b/__tests__/lifecycle-scripts.js index 20d1bfbb80..d059622162 100644 --- a/__tests__/lifecycle-scripts.js +++ b/__tests__/lifecycle-scripts.js @@ -19,14 +19,15 @@ async function execCommand(cmd: string, packageName: string, env = process.env): await fs.copy(srcPackageDir, packageDir, new NoopReporter()); return new Promise((resolve, reject) => { + const cleanedEnv = {...env}; + cleanedEnv['YARN_SILENT'] = 0; + delete cleanedEnv['FORCE_COLOR']; + exec( `node "${yarnBin}" ${cmd}`, { cwd: packageDir, - env: { - ...env, - YARN_SILENT: 0, - }, + env: cleanedEnv, }, (err, stdout) => { if (err) {