Skip to content
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

Migrate @jest/reporters to TypeScript #7994

Merged
merged 11 commits into from
Mar 3, 2019

Conversation

lorenzorapetti
Copy link
Contributor

@lorenzorapetti lorenzorapetti commented Feb 26, 2019

Summary

Built diff:

diff --git c/packages/jest-reporters/build/Status.js w/packages/jest-reporters/build/Status.js
index 2be9fd56e..a22d7b86d 100644
--- c/packages/jest-reporters/build/Status.js
+++ w/packages/jest-reporters/build/Status.js
@@ -36,8 +36,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const RUNNING_TEXT = ' RUNS ';
 const RUNNING = _chalk().default.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
@@ -69,7 +67,7 @@ class CurrentTestList {
 
   delete(testPath) {
     const record = this._array.find(
-      record => record && record.testPath === testPath
+      record => record !== null && record.testPath === testPath
     );
 
     this._array[this._array.indexOf(record || null)] = null;
@@ -92,7 +90,6 @@ class Status {
     this._done = false;
     this._emitScheduled = false;
     this._estimatedTime = 0;
-    this._height = 0;
     this._showStatus = false;
   }
 
@@ -111,7 +108,7 @@ class Status {
 
   runFinished() {
     this._done = true;
-    clearInterval(this._interval);
+    if (this._interval) clearInterval(this._interval);
 
     this._emit();
   }
@@ -126,7 +123,7 @@ class Status {
     }
   }
 
-  testFinished(config, testResult, aggregatedResults) {
+  testFinished(_config, testResult, aggregatedResults) {
     const testFilePath = testResult.testFilePath;
     this._aggregatedResults = aggregatedResults;
 
@@ -145,7 +142,7 @@ class Status {
         clear: '',
         content: ''
       };
-    } // $FlowFixMe
+    }
 
     const width = process.stdout.columns;
     let content = '\n';
@@ -199,9 +196,7 @@ class Status {
 
   _emit() {
     this._cache = null;
-    this._lastUpdated = Date.now();
-
-    this._callback();
+    if (this._callback) this._callback();
   }
 
   _debouncedEmit() {
diff --git c/packages/jest-reporters/build/base_reporter.js w/packages/jest-reporters/build/base_reporter.js
index d04e4223d..d3718c2fe 100644
--- c/packages/jest-reporters/build/base_reporter.js
+++ w/packages/jest-reporters/build/base_reporter.js
@@ -20,8 +20,6 @@ function _jestUtil() {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const preRunMessageRemove = _jestUtil().preRunMessage.remove;
 
@@ -30,15 +28,15 @@ class BaseReporter {
     process.stderr.write(message + '\n');
   }
 
-  onRunStart(results, options) {
+  onRunStart(_results, _options) {
     preRunMessageRemove(process.stderr);
   }
 
-  onTestResult(test, testResult, results) {}
+  onTestResult(_test, _testResult, _results) {}
 
-  onTestStart(test) {}
+  onTestStart(_test) {}
 
-  onRunComplete(contexts, aggregatedResults) {}
+  onRunComplete(_contexts, _aggregatedResults) {}
 
   _setError(error) {
     this._error = error;
diff --git c/packages/jest-reporters/build/coverage_reporter.js w/packages/jest-reporters/build/coverage_reporter.js
index 47514835a..9b660ab6c 100644
--- c/packages/jest-reporters/build/coverage_reporter.js
+++ w/packages/jest-reporters/build/coverage_reporter.js
@@ -5,6 +5,16 @@ Object.defineProperty(exports, '__esModule', {
 });
 exports.default = void 0;
 
+function _path() {
+  const data = _interopRequireDefault(require('path'));
+
+  _path = function _path() {
+    return data;
+  };
+
+  return data;
+}
+
 function _jestUtil() {
   const data = require('jest-util');
 
@@ -65,18 +75,6 @@ function _jestWorker() {
   return data;
 }
 
-var _base_reporter = _interopRequireDefault(require('./base_reporter'));
-
-function _path() {
-  const data = _interopRequireDefault(require('path'));
-
-  _path = function _path() {
-    return data;
-  };
-
-  return data;
-}
-
 function _glob() {
   const data = _interopRequireDefault(require('glob'));
 
@@ -87,6 +85,8 @@ function _glob() {
   return data;
 }
 
+var _base_reporter = _interopRequireDefault(require('./base_reporter'));
+
 function _interopRequireDefault(obj) {
   return obj && obj.__esModule ? obj : {default: obj};
 }
@@ -136,7 +136,7 @@ class CoverageReporter extends _base_reporter.default {
     this._options = options || {};
   }
 
-  onTestResult(test, testResult, aggregatedResults) {
+  onTestResult(_test, testResult, _aggregatedResults) {
     if (testResult.coverage) {
       this._coverageMap.merge(testResult.coverage); // Remove coverage data to free up some memory.
 
@@ -147,9 +147,9 @@ class CoverageReporter extends _base_reporter.default {
         try {
           const coverage = this._coverageMap.fileCoverageFor(sourcePath);
 
-          var _coverage$toJSON = coverage.toJSON();
+          var _ref = coverage.toJSON();
 
-          inputSourceMap = _coverage$toJSON.inputSourceMap;
+          inputSourceMap = _ref.inputSourceMap;
         } finally {
           if (inputSourceMap) {
             this._sourceMapStore.registerMap(sourcePath, inputSourceMap);
@@ -265,11 +265,11 @@ class CoverageReporter extends _base_reporter.default {
       const instrumentation = files.map(
         /*#__PURE__*/
         (function() {
-          var _ref = _asyncToGenerator(function*(fileObj) {
+          var _ref2 = _asyncToGenerator(function*(fileObj) {
             const filename = fileObj.path;
             const config = fileObj.config;
 
-            if (!_this2._coverageMap.data[filename]) {
+            if (!_this2._coverageMap.data[filename] && 'worker' in worker) {
               try {
                 const result = yield worker.worker({
                   config,
@@ -303,7 +303,7 @@ class CoverageReporter extends _base_reporter.default {
           });
 
           return function(_x) {
-            return _ref.apply(this, arguments);
+            return _ref2.apply(this, arguments);
           };
         })()
       );
@@ -318,7 +318,7 @@ class CoverageReporter extends _base_reporter.default {
         (0, _jestUtil().clearLine)(process.stderr);
       }
 
-      if (worker && typeof worker.end === 'function') {
+      if (worker && 'end' in worker && typeof worker.end === 'function') {
         worker.end();
       }
     })();
diff --git c/packages/jest-reporters/build/coverage_worker.js w/packages/jest-reporters/build/coverage_worker.js
index 3dd49e94d..67241afd2 100644
--- c/packages/jest-reporters/build/coverage_worker.js
+++ w/packages/jest-reporters/build/coverage_worker.js
@@ -4,31 +4,62 @@ Object.defineProperty(exports, '__esModule', {
   value: true
 });
 exports.worker = worker;
+Object.defineProperty(exports, 'CoverageWorkerResult', {
+  enumerable: true,
+  get: function get() {
+    return _generateEmptyCoverage.CoverageWorkerResult;
+  }
+});
 
-function _exit() {
-  const data = _interopRequireDefault(require('exit'));
+function _fs() {
+  const data = _interopRequireDefault(require('fs'));
 
-  _exit = function _exit() {
+  _fs = function _fs() {
     return data;
   };
 
   return data;
 }
 
-function _fs() {
-  const data = _interopRequireDefault(require('fs'));
+function _exit() {
+  const data = _interopRequireDefault(require('exit'));
 
-  _fs = function _fs() {
+  _exit = function _exit() {
     return data;
   };
 
   return data;
 }
 
-var _generateEmptyCoverage = _interopRequireDefault(
+var _generateEmptyCoverage = _interopRequireWildcard(
   require('./generateEmptyCoverage')
 );
 
+function _interopRequireWildcard(obj) {
+  if (obj && obj.__esModule) {
+    return obj;
+  } else {
+    var newObj = {};
+    if (obj != null) {
+      for (var key in obj) {
+        if (Object.prototype.hasOwnProperty.call(obj, key)) {
+          var desc =
+            Object.defineProperty && Object.getOwnPropertyDescriptor
+              ? Object.getOwnPropertyDescriptor(obj, key)
+              : {};
+          if (desc.get || desc.set) {
+            Object.defineProperty(newObj, key, desc);
+          } else {
+            newObj[key] = obj[key];
+          }
+        }
+      }
+    }
+    newObj.default = obj;
+    return newObj;
+  }
+}
+
 function _interopRequireDefault(obj) {
   return obj && obj.__esModule ? obj : {default: obj};
 }
@@ -38,8 +69,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 // Make sure uncaught errors are logged before we exit.
 process.on('uncaughtException', err => {
diff --git c/packages/jest-reporters/build/default_reporter.js w/packages/jest-reporters/build/default_reporter.js
index ead2efde2..713ca2b45 100644
--- c/packages/jest-reporters/build/default_reporter.js
+++ w/packages/jest-reporters/build/default_reporter.js
@@ -44,11 +44,7 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
-
-/* global stream$Writable, tty$WriteStream */
 const TITLE_BULLET = _chalk().default.bold('\u25cf ');
 
 class DefaultReporter extends _base_reporter.default {
@@ -109,7 +105,7 @@ class DefaultReporter extends _base_reporter.default {
           }, 100);
         }
       }
-    }; // $FlowFixMe
+    };
 
     stream.write = chunk => {
       buffer.push(chunk);
@@ -185,10 +181,9 @@ class DefaultReporter extends _base_reporter.default {
   onRunComplete() {
     this.forceFlushBufferedOutput();
 
-    this._status.runFinished(); // $FlowFixMe
-
-    process.stdout.write = this._out; // $FlowFixMe
+    this._status.runFinished();
 
+    process.stdout.write = this._out;
     process.stderr.write = this._err;
     (0, _jestUtil().clearLine)(process.stderr);
   }
@@ -216,7 +211,7 @@ class DefaultReporter extends _base_reporter.default {
     this._status.testFinished(config, testResult, aggregatedResults);
   }
 
-  printTestFileHeader(testPath, config, result) {
+  printTestFileHeader(_testPath, config, result) {
     this.log(
       (0, _get_result_header.default)(result, this._globalConfig, config)
     );
@@ -236,7 +231,7 @@ class DefaultReporter extends _base_reporter.default {
     }
   }
 
-  printTestFileFailureMessage(testPath, config, result) {
+  printTestFileFailureMessage(_testPath, _config, result) {
     if (result.failureMessage) {
       this.log(result.failureMessage);
     }
diff --git c/packages/jest-reporters/build/generateEmptyCoverage.js w/packages/jest-reporters/build/generateEmptyCoverage.js
index 59e564dc7..db084dfab 100644
--- c/packages/jest-reporters/build/generateEmptyCoverage.js
+++ w/packages/jest-reporters/build/generateEmptyCoverage.js
@@ -40,9 +40,9 @@ function _transform() {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
+// TODO: Remove this
+/// <reference path="./istanbul-lib-coverage.d.ts" />
 const FileCoverage = _istanbulLibCoverage().classes.FileCoverage;
 
 function _default(source, filename, globalConfig, config, changedFiles) {
diff --git c/packages/jest-reporters/build/get_result_header.js w/packages/jest-reporters/build/get_result_header.js
index 2e5ec69b2..12f2214d1 100644
--- c/packages/jest-reporters/build/get_result_header.js
+++ w/packages/jest-reporters/build/get_result_header.js
@@ -26,8 +26,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const LONG_TEST_COLOR = _chalk().default.reset.bold.bgRed; // Explicitly reset for these messages since they can get written out in the
 // middle of error logging
diff --git c/packages/jest-reporters/build/get_snapshot_status.js w/packages/jest-reporters/build/get_snapshot_status.js
index 5f7ce81a6..7248fa8a0 100644
--- c/packages/jest-reporters/build/get_snapshot_status.js
+++ w/packages/jest-reporters/build/get_snapshot_status.js
@@ -34,8 +34,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const ARROW = ' \u203A ';
 const DOT = ' \u2022 ';
diff --git c/packages/jest-reporters/build/get_snapshot_summary.js w/packages/jest-reporters/build/get_snapshot_summary.js
index c4661a026..5c8ddbac3 100644
--- c/packages/jest-reporters/build/get_snapshot_summary.js
+++ w/packages/jest-reporters/build/get_snapshot_summary.js
@@ -36,8 +36,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const ARROW = ' \u203A ';
 const DOWN_ARROW = ' \u21B3 ';
diff --git c/packages/jest-reporters/build/istanbul-api.d.js w/packages/jest-reporters/build/istanbul-api.d.js
new file mode 100644
index 000000000..ad9a93a7c
--- /dev/null
+++ w/packages/jest-reporters/build/istanbul-api.d.js
@@ -0,0 +1 @@
+'use strict';
diff --git c/packages/jest-reporters/build/istanbul-lib-coverage.d.js w/packages/jest-reporters/build/istanbul-lib-coverage.d.js
new file mode 100644
index 000000000..ad9a93a7c
--- /dev/null
+++ w/packages/jest-reporters/build/istanbul-lib-coverage.d.js
@@ -0,0 +1 @@
+'use strict';
diff --git c/packages/jest-reporters/build/node-notifier.d.js w/packages/jest-reporters/build/node-notifier.d.js
new file mode 100644
index 000000000..ad9a93a7c
--- /dev/null
+++ w/packages/jest-reporters/build/node-notifier.d.js
@@ -0,0 +1 @@
+'use strict';
diff --git c/packages/jest-reporters/build/notify_reporter.js w/packages/jest-reporters/build/notify_reporter.js
index 132c0102b..815606f74 100644
--- c/packages/jest-reporters/build/notify_reporter.js
+++ w/packages/jest-reporters/build/notify_reporter.js
@@ -5,30 +5,30 @@ Object.defineProperty(exports, '__esModule', {
 });
 exports.default = void 0;
 
-function _exit() {
-  const data = _interopRequireDefault(require('exit'));
+function _path() {
+  const data = _interopRequireDefault(require('path'));
 
-  _exit = function _exit() {
+  _path = function _path() {
     return data;
   };
 
   return data;
 }
 
-function _path() {
-  const data = _interopRequireDefault(require('path'));
+function _util() {
+  const data = _interopRequireDefault(require('util'));
 
-  _path = function _path() {
+  _util = function _util() {
     return data;
   };
 
   return data;
 }
 
-function _util() {
-  const data = _interopRequireDefault(require('util'));
+function _exit() {
+  const data = _interopRequireDefault(require('exit'));
 
-  _util = function _util() {
+  _exit = function _exit() {
     return data;
   };
 
diff --git c/packages/jest-reporters/build/summary_reporter.js w/packages/jest-reporters/build/summary_reporter.js
index f0163e6c3..6fa4388df 100644
--- c/packages/jest-reporters/build/summary_reporter.js
+++ w/packages/jest-reporters/build/summary_reporter.js
@@ -44,8 +44,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const TEST_SUMMARY_THRESHOLD = 20;
 const NPM_EVENTS = new Set([
@@ -149,7 +147,7 @@ class SummaryReporter extends _base_reporter.default {
       snapshots.updated
     ) {
       let updateCommand;
-      const event = process.env.npm_lifecycle_event;
+      const event = process.env.npm_lifecycle_event || '';
       const prefix = NPM_EVENTS.has(event) ? '' : 'run ';
       const isYarn =
         typeof process.env.npm_config_user_agent === 'string' &&
diff --git c/packages/jest-reporters/build/types.js w/packages/jest-reporters/build/types.js
new file mode 100644
index 000000000..d222427bd
--- /dev/null
+++ w/packages/jest-reporters/build/types.js
@@ -0,0 +1,3 @@
+'use strict';
+
+var _coverage_worker = require('./coverage_worker');
diff --git c/packages/jest-reporters/build/utils.js w/packages/jest-reporters/build/utils.js
index 04bd7b987..5dbf0c679 100644
--- c/packages/jest-reporters/build/utils.js
+++ w/packages/jest-reporters/build/utils.js
@@ -54,8 +54,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const PROGRESS_BAR_WIDTH = 40;
 
diff --git c/packages/jest-reporters/build/verbose_reporter.js w/packages/jest-reporters/build/verbose_reporter.js
index 7d1d3960d..55ebafe31 100644
--- c/packages/jest-reporters/build/verbose_reporter.js
+++ w/packages/jest-reporters/build/verbose_reporter.js
@@ -36,8 +36,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const ICONS = _jestUtil().specialChars.ICONS;
 

Related PRs

Test plan

Green CI

@natealcedo
Copy link

I dont think the tests are part of this migration.

Regarding this, you could probably raise a pr for this if it isn't too difficult. I did the same thing with DefinitelyTyped/DefinitelyTyped#33351

@types/node-notifier doesn't have the typings for actions used here

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome start!

Some PRs to definitelytyped fixing their types would be awesome 🙂

packages/jest-reporters/src/Status.ts Outdated Show resolved Hide resolved
packages/jest-reporters/src/base_reporter.ts Outdated Show resolved Hide resolved
packages/jest-reporters/src/coverage_reporter.ts Outdated Show resolved Hide resolved
packages/jest-reporters/src/default_reporter.ts Outdated Show resolved Hide resolved
@lorenzorapetti
Copy link
Contributor Author

Regarding this, you could probably raise a pr for this if it isn't too difficult.

Some PRs to definitelytyped fixing their types would be awesome 🙂

Sure thing! I will make the PRs soon. I'm also updating all the underscore properties.

@SimenB
Copy link
Member

SimenB commented Feb 27, 2019

Feel free to add some definition files to your PR as you work - both to avoid waiting for definitelytyped to merge and so you can verify the changes does the job 🙂

@lorenzorapetti
Copy link
Contributor Author

Ok, that was quite challenging 😅 I have only one test failing though, so i'm almost there. @SimenB I'll continue as soon as i'm done working

For now i've done the typings for node-notifier, istanbul-lib-coverage and istanbul-api (for this one i wrote only the strictly necessary types for everything to work). This weekend i'll try to create the PRs to DefinitelyTyped.

@SimenB
Copy link
Member

SimenB commented Mar 1, 2019

Awesome! Happy to land this in Jest including typings at first, then go back and remove them once definitelytyped is updated

@lorenzorapetti lorenzorapetti marked this pull request as ready for review March 3, 2019 14:49
@lorenzorapetti
Copy link
Contributor Author

I've finally resolved all the failing tests and wrong typings ✨ Now it should be ready to merge

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome! left some nits and questions

e2e/snapshot-escape/__tests__/snapshot.test.js Outdated Show resolved Hide resolved
packages/jest-reporters/src/Status.ts Outdated Show resolved Hide resolved
packages/jest-reporters/src/notify_reporter.ts Outdated Show resolved Hide resolved
packages/jest-reporters/src/utils.ts Outdated Show resolved Hide resolved
packages/jest-reporters/tsconfig.json Outdated Show resolved Hide resolved
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic, thank you so much! Looking forward to seeing those PRs to DefinitelyTyped be merged so we can get rid of the inline typings here

@SimenB SimenB requested a review from thymikee March 3, 2019 19:22
@SimenB
Copy link
Member

SimenB commented Mar 3, 2019

/cc @thymikee @jeysal PTAL

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

packages/jest-reporters/src/summary_reporter.ts Outdated Show resolved Hide resolved
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants