From 0bf9fe4a10763b17ee76cbb7875494c8f3c60028 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 19:12:19 -0700
Subject: [PATCH 1/7] CustomConsole: print console.error and console.assert to
 stderr

---
 CHANGELOG.md                                  |  1 +
 packages/jest-console/src/CustomConsole.ts    | 25 +++++----
 ...{console.test.ts => CustomConsole.test.ts} | 52 +++++++++++++++----
 3 files changed, 58 insertions(+), 20 deletions(-)
 rename packages/jest-console/src/__tests__/{console.test.ts => CustomConsole.test.ts} (77%)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45f3986cd5fe..09a1ede5bbef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@
 
 ### Fixes
 
+- `[@jest/console]` Print to stderr when calling `console.error` or `console.assert` in verbose mode or using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
 - `[expect]` Add negative equality tests for iterables ([#8260](https://github.com/facebook/jest/pull/8260))
 - `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
 
diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts
index 13bcd0e7ec23..cd109a14e0ab 100644
--- a/packages/jest-console/src/CustomConsole.ts
+++ b/packages/jest-console/src/CustomConsole.ts
@@ -12,8 +12,8 @@ import chalk from 'chalk';
 import {LogCounters, LogMessage, LogTimers, LogType} from './types';
 
 // TODO: Copied from `jest-util`. Import from it in Jest 25
-function clearLine(stream: NodeJS.WritableStream) {
-  if (process.stdout.isTTY) {
+function clearLine(stream: NodeJS.WritableStream & {isTTY?: boolean}) {
+  if (stream.isTTY) {
     stream.write('\x1b[999D\x1b[K');
   }
 }
@@ -22,6 +22,7 @@ type Formatter = (type: LogType, message: LogMessage) => string;
 
 export default class CustomConsole extends Console {
   private _stdout: NodeJS.WritableStream;
+  private _stderr: NodeJS.WritableStream;
   private _formatBuffer: Formatter;
   private _counters: LogCounters;
   private _timers: LogTimers;
@@ -34,21 +35,27 @@ export default class CustomConsole extends Console {
   ) {
     super(stdout, stderr);
     this._stdout = stdout;
+    this._stderr = stderr;
     this._formatBuffer = formatBuffer;
     this._counters = {};
     this._timers = {};
     this._groupDepth = 0;
   }
 
-  private _logToParentConsole(message: string) {
-    super.log(message);
-  }
-
   private _log(type: LogType, message: string) {
-    clearLine(this._stdout);
-    this._logToParentConsole(
-      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
+    const isErrorType = type === 'error' || type === 'assert';
+    const formattedMessage = this._formatBuffer(
+      type,
+      '  '.repeat(this._groupDepth) + message,
     );
+
+    if (isErrorType) {
+      clearLine(this._stderr);
+      super.error(formattedMessage);
+    } else {
+      clearLine(this._stdout);
+      super.log(formattedMessage);
+    }
   }
 
   assert(value: any, message?: string | Error) {
diff --git a/packages/jest-console/src/__tests__/console.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts
similarity index 77%
rename from packages/jest-console/src/__tests__/console.test.ts
rename to packages/jest-console/src/__tests__/CustomConsole.test.ts
index 4b994fc6e888..feaab8083a4a 100644
--- a/packages/jest-console/src/__tests__/console.test.ts
+++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts
@@ -5,47 +5,77 @@
  * LICENSE file in the root directory of this source tree.
  */
 
+import {Writable} from 'stream';
 import chalk from 'chalk';
 import CustomConsole from '../CustomConsole';
 
 describe('CustomConsole', () => {
   let _console;
-  let _stdout = '';
+  let _stdout;
+  let _stderr;
 
   beforeEach(() => {
-    _console = new CustomConsole(process.stdout, process.stderr);
-    jest.spyOn(_console, '_logToParentConsole').mockImplementation(message => {
-      _stdout += message + '\n';
+    _stdout = '';
+    _stderr = '';
+
+    const stdout = new Writable({
+      write(chunk, encoding, callback) {
+        _stdout += chunk.toString();
+        callback();
+      },
     });
 
-    _stdout = '';
+    const stderr = new Writable({
+      write(chunk, encoding, callback) {
+        _stderr += chunk.toString();
+        callback();
+      },
+    });
+
+    _console = new CustomConsole(stdout, stderr);
+  });
+
+  describe('log', () => {
+    test('should print to stdout', () => {
+      _console.log('Hello world!');
+
+      expect(_stdout).toBe('Hello world!\n');
+    });
+  });
+
+  describe('error', () => {
+    test('should print to stderr', () => {
+      _console.error('Found some error!');
+
+      expect(_stderr).toBe('Found some error!\n');
+    });
   });
 
   describe('assert', () => {
     test('do not log when the assertion is truthy', () => {
       _console.assert(true);
 
-      expect(_stdout).toMatch('');
+      expect(_stderr).toMatch('');
     });
 
     test('do not log when the assertion is truthy and there is a message', () => {
       _console.assert(true, 'ok');
 
-      expect(_stdout).toMatch('');
+      expect(_stderr).toMatch('');
     });
 
     test('log the assertion error when the assertion is falsy', () => {
       _console.assert(false);
 
-      expect(_stdout).toMatch('AssertionError');
-      expect(_stdout).toMatch('false == true');
+      expect(_stderr).toMatch('AssertionError');
+      expect(_stderr).toMatch('false == true');
     });
 
     test('log the assertion error when the assertion is falsy with another message argument', () => {
       _console.assert(false, 'ok');
 
-      expect(_stdout).toMatch('AssertionError');
-      expect(_stdout).toMatch('ok');
+      expect(_stderr).toMatch('AssertionError');
+      expect(_stderr).toMatch('ok');
     });
   });
 

From 2ba1f69a6c3075c77f89b36de60b7d753de4f7ae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 19:23:11 -0700
Subject: [PATCH 2/7] Refactor

---
 packages/jest-console/src/CustomConsole.ts | 24 ++++++++++------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts
index cd109a14e0ab..2a7a55601298 100644
--- a/packages/jest-console/src/CustomConsole.ts
+++ b/packages/jest-console/src/CustomConsole.ts
@@ -43,26 +43,24 @@ export default class CustomConsole extends Console {
   }
 
   private _log(type: LogType, message: string) {
-    const isErrorType = type === 'error' || type === 'assert';
-    const formattedMessage = this._formatBuffer(
-      type,
-      '  '.repeat(this._groupDepth) + message,
+    clearLine(this._stdout);
+    super.log(
+      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
     );
+  }
 
-    if (isErrorType) {
-      clearLine(this._stderr);
-      super.error(formattedMessage);
-    } else {
-      clearLine(this._stdout);
-      super.log(formattedMessage);
-    }
+  private _logError(type: LogType, message: string) {
+    clearLine(this._stderr);
+    super.error(
+      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
+    );
   }
 
   assert(value: any, message?: string | Error) {
     try {
       assert(value, message);
     } catch (error) {
-      this._log('assert', error.toString());
+      this._logError('assert', error.toString());
     }
   }
 
@@ -91,7 +89,7 @@ export default class CustomConsole extends Console {
   }
 
   error(firstArg: any, ...args: Array<any>) {
-    this._log('error', format(firstArg, ...args));
+    this._logError('error', format(firstArg, ...args));
   }
 
   group(title?: string, ...args: Array<any>) {

From dfc6683a01c2c2752342f9a94f37afb5541a5388 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 20:03:35 -0700
Subject: [PATCH 3/7] Revert change of behaviour in verbose mode

---
 CHANGELOG.md                        | 2 +-
 packages/jest-runner/src/runTest.ts | 8 ++------
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 09a1ede5bbef..d86611d4b464 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,7 +17,7 @@
 
 ### Fixes
 
-- `[@jest/console]` Print to stderr when calling `console.error` or `console.assert` in verbose mode or using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
+- `[@jest/console]` Print to stderr when calling `console.error` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
 - `[expect]` Add negative equality tests for iterables ([#8260](https://github.com/facebook/jest/pull/8260))
 - `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
 
diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts
index 49bcb0646b5c..dadef559f159 100644
--- a/packages/jest-runner/src/runTest.ts
+++ b/packages/jest-runner/src/runTest.ts
@@ -135,13 +135,9 @@ async function runTestInternal(
   let testConsole;
 
   if (globalConfig.silent) {
-    testConsole = new NullConsole(consoleOut, process.stderr, consoleFormatter);
+    testConsole = new NullConsole(consoleOut, consoleOut, consoleFormatter);
   } else if (globalConfig.verbose) {
-    testConsole = new CustomConsole(
-      consoleOut,
-      process.stderr,
-      consoleFormatter,
-    );
+    testConsole = new CustomConsole(consoleOut, consoleOut, consoleFormatter);
   } else {
     testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
   }

From 52962d19052cd252134974b562c39e309e277f2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 20:08:27 -0700
Subject: [PATCH 4/7] Print to stderr when using console.warn

---
 CHANGELOG.md                                              | 2 +-
 packages/jest-console/src/CustomConsole.ts                | 2 +-
 packages/jest-console/src/__tests__/CustomConsole.test.ts | 8 ++++++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d86611d4b464..9fc5e235afed 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,7 +17,7 @@
 
 ### Fixes
 
-- `[@jest/console]` Print to stderr when calling `console.error` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
+- `[@jest/console]` Print to stderr when calling `console.error`, `console.warn` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
 - `[expect]` Add negative equality tests for iterables ([#8260](https://github.com/facebook/jest/pull/8260))
 - `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
 
diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts
index 2a7a55601298..6118e48cb715 100644
--- a/packages/jest-console/src/CustomConsole.ts
+++ b/packages/jest-console/src/CustomConsole.ts
@@ -142,7 +142,7 @@ export default class CustomConsole extends Console {
   }
 
   warn(firstArg: any, ...args: Array<any>) {
-    this._log('warn', format(firstArg, ...args));
+    this._logError('warn', format(firstArg, ...args));
   }
 
   getBuffer() {
diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts
index feaab8083a4a..7bf5b566cb2b 100644
--- a/packages/jest-console/src/__tests__/CustomConsole.test.ts
+++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts
@@ -51,6 +51,14 @@ describe('CustomConsole', () => {
     });
   });
 
+  describe('warn', () => {
+    test('should print to stderr', () => {
+      _console.warn('Found some warning!');
+
+      expect(_stderr).toBe('Found some warning!\n');
+    });
+  });
+
   describe('assert', () => {
     test('do not log when the assertion is truthy', () => {
       _console.assert(true);

From fd2db75b76f57e94b55e67d23db639e30abc291a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 21:29:40 -0700
Subject: [PATCH 5/7] Change Node implementation detail in test

---
 packages/jest-console/src/__tests__/CustomConsole.test.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts
index 7bf5b566cb2b..fed8d092cdc3 100644
--- a/packages/jest-console/src/__tests__/CustomConsole.test.ts
+++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts
@@ -76,7 +76,7 @@ describe('CustomConsole', () => {
       _console.assert(false);
 
       expect(_stderr).toMatch('AssertionError');
-      expect(_stderr).toMatch('false == true');
+      expect(_stderr).toMatch('The expression evaluated to a falsy value:');
     });
 
     test('log the assertion error when the assertion is falsy with another message argument', () => {

From 5d0f9f2f0a0151c174bd36c3bba8f51fc3409ee1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 21:32:20 -0700
Subject: [PATCH 6/7] Fix changelog entry position

---
 CHANGELOG.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9fc5e235afed..e4a1e92d436a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,8 @@
 
 ### Fixes
 
+- `[@jest/console]` Print to stderr when calling `console.error`, `console.warn` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
+
 ### Chore & Maintenance
 
 ### Performance
@@ -17,7 +19,6 @@
 
 ### Fixes
 
-- `[@jest/console]` Print to stderr when calling `console.error`, `console.warn` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
 - `[expect]` Add negative equality tests for iterables ([#8260](https://github.com/facebook/jest/pull/8260))
 - `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
 

From 794f9d55b455a8d9e14fc170d33bd5594fd11a28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Norte?= <rubennorte@fb.com>
Date: Tue, 2 Apr 2019 21:42:26 -0700
Subject: [PATCH 7/7] Fix difference between node versions

---
 .../jest-console/src/__tests__/CustomConsole.test.ts     | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts
index fed8d092cdc3..56c6964eab7b 100644
--- a/packages/jest-console/src/__tests__/CustomConsole.test.ts
+++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts
@@ -76,14 +76,17 @@ describe('CustomConsole', () => {
       _console.assert(false);
 
       expect(_stderr).toMatch('AssertionError');
-      expect(_stderr).toMatch('The expression evaluated to a falsy value:');
+      expect(_stderr).toMatch(
+        // The message may differ across Node versions
+        /(false == true)|(The expression evaluated to a falsy value:)/,
+      );
     });
 
     test('log the assertion error when the assertion is falsy with another message argument', () => {
-      _console.assert(false, 'ok');
+      _console.assert(false, 'this should not happen');
 
       expect(_stderr).toMatch('AssertionError');
-      expect(_stderr).toMatch('ok');
+      expect(_stderr).toMatch('this should not happen');
     });
   });