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

chore: migrate jest-environment-node to TypeScript #7985

Merged
merged 8 commits into from Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/jest-environment-node/package.json
Expand Up @@ -8,8 +8,10 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/fake-timers": "^24.1.0",
"@jest/types": "^24.1.0",
"jest-mock": "^24.0.0",
"jest-util": "^24.0.0"
},
Expand Down
Expand Up @@ -3,33 +3,27 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Script} from 'vm';
import type {ProjectConfig} from 'types/Config';
import type {Global} from 'types/Global';
import type {ModuleMocker} from 'jest-mock';

import vm from 'vm';
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
import vm, {Script, Context} from 'vm';
import {Global, Config} from '@jest/types';
import {ModuleMocker} from 'jest-mock';
import {installCommonGlobals} from 'jest-util';
import mock from 'jest-mock';
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';

type Timer = {|
id: number,
ref: () => Timer,
unref: () => Timer,
|};
type Timer = {
id: number;
ref: () => Timer;
unref: () => Timer;
};

class NodeEnvironment {
context: ?vm$Context;
fakeTimers: ?FakeTimers<Timer>;
global: ?Global;
moduleMocker: ?ModuleMocker;
context?: Context | null;
fakeTimers?: FakeTimers<Timer> | null;
global?: Global.Global | null;
moduleMocker?: ModuleMocker | null;

constructor(config: ProjectConfig) {
constructor(config: Config.ProjectConfig) {
this.context = vm.createContext();
const global = (this.global = vm.runInContext(
'this',
Expand All @@ -48,7 +42,7 @@ class NodeEnvironment {
global.URLSearchParams = URLSearchParams;
}
installCommonGlobals(global, config.globals);
this.moduleMocker = new mock.ModuleMocker(global);
this.moduleMocker = new ModuleMocker(global);

const timerIdToRef = (id: number) => ({
id,
Expand All @@ -60,7 +54,8 @@ class NodeEnvironment {
},
});

const timerRefToId = (timer: Timer): ?number => (timer && timer.id) || null;
const timerRefToId = (timer: Timer): number | undefined =>
(timer && timer.id) || undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please note that I've changed the possible return type from null to undefined here. FakeTimers does not like the timerConfig if timerRefToId returns null (but undefined is OK).

Copy link
Member

Choose a reason for hiding this comment

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

That's fine. Happy to get rid of the null!


const timerConfig = {
idToRef: timerIdToRef,
Expand Down Expand Up @@ -89,7 +84,7 @@ class NodeEnvironment {
}

// Disabling rule as return type depends on script's return type.
runScript(script: Script): ?any {
runScript(script: Script): any | null | undefined {
Copy link

Choose a reason for hiding this comment

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

null | undefined is redundant since this is already any.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

null | undefined is redundant since this is already any.

I agree, I was just trying to refactor without changing to much. I'll try to change it into a proper type instead of 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.

@SimenB WRT the any type here. I've just logged what is returned from runScript() and it looks like this (in a separate app/jest setup):

{ 'Object.<anonymous>': [Function: Object.<anonymous>] }

JSON stringified it is an empty object:

'{}'

I'm not sure what to put as the return type here. I just don't have enough understanding of what is being done in the method. Just keep it as any? Or an unsealed object {} | undefined | null? Open to suggestions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Never mind, I'll follow your diff below.

if (this.context) {
return script.runInContext(this.context);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-environment-node/tsconfig.json
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"references": [
{"path": "../jest-fake-timers"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"}
]
}