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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@

### Chore & Maintenance

- `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985))
- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855), [#7951](https://github.com/facebook/jest/pull/7951))
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972))
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-config/tsconfig.json
Expand Up @@ -4,9 +4,11 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: This is missing `jest-validate`, in addition to `jest-environment-jsdom`, `jest-environment-node` and
// `jest-jasmine2`, but those are just `require.resolve`d, so no real use for their types
// TODO: This is missing `jest-validate`, in addition to
// `jest-environment-jsdom` and `jest-jasmine2`, but those are just
// `require.resolve`d, so no real use for their types
"references": [
{"path": "../jest-environment-node"},
{"path": "../jest-get-type"},
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-environment-node/package.json
Expand Up @@ -8,8 +8,11 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/environment": "^24.1.0",
"@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,28 @@
*
* 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';
import {JestEnvironment} from '@jest/environment';

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;
class NodeEnvironment implements JestEnvironment {
context: Context | null;
fakeTimers: FakeTimers<Timer> | null;
global: Global.Global;
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 +43,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 +55,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 All @@ -75,11 +71,11 @@ class NodeEnvironment {
});
}

setup(): Promise<void> {
setup() {
return Promise.resolve();
}

teardown(): Promise<void> {
teardown() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
Expand All @@ -88,13 +84,14 @@ class NodeEnvironment {
return Promise.resolve();
}

// Disabling rule as return type depends on script's return type.
runScript(script: Script): ?any {
// TS infers the return type to be `any`, since that's what `runInContext`
// returns.
runScript(script: Script) {
if (this.context) {
return script.runInContext(this.context);
}
return null;
}
}

module.exports = NodeEnvironment;
export = NodeEnvironment;
14 changes: 14 additions & 0 deletions packages/jest-environment-node/tsconfig.json
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"references": [
{"path": "../jest-environment"},
{"path": "../jest-fake-timers"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"}
]
}
3 changes: 2 additions & 1 deletion packages/jest-runtime/tsconfig.json
Expand Up @@ -4,10 +4,11 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: Missing `jest-validate` and `jest-environment-node`
// TODO: Missing `jest-validate`
"references": [
{"path": "../jest-config"},
{"path": "../jest-environment"},
{"path": "../jest-environment-node"},
{"path": "../jest-haste-map"},
{"path": "../jest-message-util"},
{"path": "../jest-mock"},
Expand Down