-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from 5 commits
c857e4c
0cfe10b
57a3567
9b279a6
2c3afe4
ae3c45c
71048f6
cc4eacb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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, | ||
|
@@ -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; | ||
|
||
const timerConfig = { | ||
idToRef: timerIdToRef, | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essentially, the return value this object: https://github.com/facebook/jest/blob/419661f57ea8e3af26a788bb3db92127c854d13b/packages/jest-transform/src/ScriptTransformer.ts#L559-L565 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree, I was just trying to refactor without changing to much. I'll try to change it into a proper type instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimenB WRT the
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
] | ||
} |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
!