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-runner to typescript #7968

Merged
merged 27 commits into from
Feb 25, 2019
Merged

Conversation

natealcedo
Copy link
Contributor

@natealcedo natealcedo commented Feb 23, 2019

This PR migrates jest-runner. I need help on some things hence opening an early PR

Heres the diff

diff --git c/packages/jest-runner/build/index.js w/packages/jest-runner/build/index.js
index ab3272fb1..d9b5092bd 100644
--- c/packages/jest-runner/build/index.js
+++ w/packages/jest-runner/build/index.js
@@ -10,8 +10,6 @@ function _exit() {
   return data;
 }
 
-var _runTest = _interopRequireDefault(require('./runTest'));
-
 function _throat() {
   const data = _interopRequireDefault(require('throat'));
 
@@ -32,6 +30,10 @@ function _jestWorker() {
   return data;
 }
 
+var _runTest = _interopRequireDefault(require('./runTest'));
+
+var _testWorker = require('./testWorker');
+
 function _interopRequireDefault(obj) {
   return obj && obj.__esModule ? obj : {default: obj};
 }
diff --git c/packages/jest-runner/build/runTest.js w/packages/jest-runner/build/runTest.js
index 4d2628682..5439bd4fe 100644
--- c/packages/jest-runner/build/runTest.js
+++ w/packages/jest-runner/build/runTest.js
@@ -178,7 +178,7 @@ function _asyncToGenerator(fn) {
   };
 }
 
-function freezeConsole(testConsole, config) {
+function freezeConsole(testConsole, config) { // @ts-ignore: Correct types when `jest-util` is ESM
   testConsole._log = function fakeConsolePush(_type, message) {
     const error = new (_jestUtil()).ErrorWithStack(
       `${_chalk().default.red(
@@ -235,18 +235,15 @@ function _runTestInternal() {
         })
       );
     }
-    /* $FlowFixMe */
 
     const TestEnvironment = require(testEnvironment);
 
     const testFramework =
       process.env.JEST_CIRCUS === '1'
         ? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
-        : /* $FlowFixMe */
-          require(config.testRunner);
+        : require(config.testRunner);
     const Runtime = config.moduleLoader
-      ? /* $FlowFixMe */
-        require(config.moduleLoader)
+      ? require(config.moduleLoader)
       : require('jest-runtime');
     let runtime = undefined;
     const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
@@ -313,7 +310,7 @@ function _runTestInternal() {
           try {
             return {
               map: JSON.parse(
-                _gracefulFs().default.readFileSync(sourceMapSource)
+                _gracefulFs().default.readFileSync(sourceMapSource, 'utf8')
               ),
               url: source
             };
@@ -413,7 +410,7 @@ function _runTestInternal() {
         );
       });
     } finally {
-      yield environment.teardown();
+      yield environment.teardown(); // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33351
 
       _sourceMapSupport().default.resetRetrieveHandlers();
     }
diff --git c/packages/jest-runner/build/testWorker.js w/packages/jest-runner/build/testWorker.js
index 3e87b5fc2..ba8bc6c58 100644
--- c/packages/jest-runner/build/testWorker.js
+++ w/packages/jest-runner/build/testWorker.js
@@ -5,20 +5,20 @@ Object.defineProperty(exports, '__esModule', {
 });
 exports.worker = worker;
 
-function _exit() {
-  const data = _interopRequireDefault(require('exit'));
+function _jestHasteMap() {
+  const data = _interopRequireDefault(require('jest-haste-map'));
 
-  _exit = function _exit() {
+  _jestHasteMap = function _jestHasteMap() {
     return data;
   };
 
   return data;
 }
 
-function _jestHasteMap() {
-  const data = _interopRequireDefault(require('jest-haste-map'));
+function _exit() {
+  const data = _interopRequireDefault(require('exit'));
 
-  _jestHasteMap = function _jestHasteMap() {
+  _exit = function _exit() {
     return data;
   };
 
diff --git c/packages/jest-runner/build/types.js w/packages/jest-runner/build/types.js
new file mode 100644
index 000000000..ad9a93a7c
--- /dev/null
+++ w/packages/jest-runner/build/types.js
@@ -0,0 +1 @@
+'use strict';

Copy link
Contributor Author

@natealcedo natealcedo left a comment

Choose a reason for hiding this comment

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

Leaving some comments on the parts I need help. The test suite is failing for me locally. Can't verify if its just my machine or not. Well on the flipside, nearly there!

packages/jest-runner/src/index.ts Outdated Show resolved Hide resolved
packages/jest-runner/src/runTest.ts Outdated Show resolved Hide resolved
packages/jest-runner/src/runTest.ts Outdated Show resolved Hide resolved

environment.global.process.exit = function exit(...args) {
(environment.global as NodeJS.Global).process.exit = function exit(
...args: Array<any>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unknown doesnt work here

packages/jest-runner/src/runTest.ts Show resolved Hide resolved
packages/jest-runner/src/runTest.ts Outdated Show resolved Hide resolved
@SimenB
Copy link
Member

SimenB commented Feb 23, 2019

Regarding your Environment woes, I've made some changes to it in #7964, not sure if that'll help or not, though 🙂

@natealcedo
Copy link
Contributor Author

Just pushed some more changes. Let me know what else needs resolving

@SimenB
Copy link
Member

SimenB commented Feb 24, 2019

You need to add a couple of @ts-ignores for the modules not yet migrated

@SimenB
Copy link
Member

SimenB commented Feb 25, 2019

I've tweaked this a bit, I think it's pretty close now. Will review properly tomorrow 🙂

@natealcedo
Copy link
Contributor Author

Hey thank you so much for the small fixes that I missed! They’re much appreciated. I’ll add in the diff and update the change log in a bit.

@codecov-io
Copy link

codecov-io commented Feb 25, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@5d48312). Click here to learn what that means.
The diff coverage is 28.57%.

Impacted file tree graph

@@           Coverage Diff            @@
##             master   #7968   +/-   ##
========================================
  Coverage          ?   64.7%           
========================================
  Files             ?     256           
  Lines             ?   10051           
  Branches          ?    1497           
========================================
  Hits              ?    6504           
  Misses            ?    3223           
  Partials          ?     324
Impacted Files Coverage Δ
packages/jest-worker/src/types.ts 100% <ø> (ø)
packages/jest-runner/src/runTest.ts 2.59% <0%> (ø)
packages/jest-runner/src/testWorker.ts 0% <0%> (ø)
packages/jest-runner/src/index.ts 65.11% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d48312...949baad. Read the comment docs.

uid?: number;
gid?: number;
};
export {ForkOptions};
Copy link
Member

Choose a reason for hiding this comment

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

I love these changes 😀

@@ -48,7 +48,7 @@ export interface Describe extends DescribeBase {
skip: ItBase;
}

export interface Global {
export interface Global extends NodeJS.Global {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know this was the fix. I tried extending the global inside the declare. Cool beans.

@SimenB SimenB requested a review from thymikee February 25, 2019 00:45
@natealcedo natealcedo changed the title [WIP] Migrate jest-runner to typescript Migrate jest-runner to typescript Feb 25, 2019
@@ -5,6 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import EventEmitter from 'events';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be import * as EventEmitter from 'events' ?

Copy link
Member

Choose a reason for hiding this comment

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

It's the same object

$ node -p "require('events') === require('events').EventEmitter"
true

And the node docs does not destructure: https://nodejs.org/api/events.html#events_events

Copy link
Contributor

@gengjiawen gengjiawen Feb 25, 2019

Choose a reason for hiding this comment

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

My concern is that events may not has a default export just like path.
import path from 'path' won't work on ts.

Copy link
Member

Choose a reason for hiding this comment

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

Fair point! Changed. Should we add "allowSyntheticDefaultImports": false to out tsconfig?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would conflict with esModuleInterop: true. I think we need it for now, but in an ideal world in the future where everything is written in ESM we will be able to drop esModuleInterop

Copy link
Member

Choose a reason for hiding this comment

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

Anyways, this PR is not the right place to discuss it. Might be worth a new issue, though, so we do it correctly

Copy link
Contributor

Choose a reason for hiding this comment

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

esModuleInterop doc: "Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility"

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but that doesn't mean I can't disable it again, does it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be possible to, but it seems like that would mean to forbid default-importing module.exports for type checking, but including the interop helper in output code. Because we use Babel, so our tsc does not emit anything, that would equal not enabling either of them at all, I think. And I believe we need it at least for default-importing some util libraries from npm that use module.exports =

Copy link
Member

Choose a reason for hiding this comment

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

Anyways, this PR is not the right place to discuss it. Might be worth a new issue, though, so we do it correctly

😀

@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 12, 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.

6 participants