Skip to content

Commit 16fe18d

Browse files
authored
chore: cleanup JestEnvironment types (#7988)
1 parent 419661f commit 16fe18d

6 files changed

Lines changed: 22 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
- `[jest-circus]`: Migrate to TypeScript ([#7916](https://github.com/facebook/jest/pull/7916))
6060
- `[jest-phabricator]`: Migrate to TypeScript ([#7965](https://github.com/facebook/jest/pull/7965))
6161
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
62-
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964))
62+
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
6363
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
6464

6565
### Performance

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const jestAdapter = async (
4747
});
4848

4949
if (config.timers === 'fake') {
50-
environment.fakeTimers.useFakeTimers();
50+
// during setup, this cannot be null (and it's fine to explode if it is)
51+
environment.fakeTimers!.useFakeTimers();
5152
}
5253

5354
globals.beforeEach(() => {
@@ -63,7 +64,8 @@ const jestAdapter = async (
6364
runtime.resetAllMocks();
6465

6566
if (config.timers === 'fake') {
66-
environment.fakeTimers.useFakeTimers();
67+
// during setup, this cannot be null (and it's fine to explode if it is)
68+
environment.fakeTimers!.useFakeTimers();
6769
}
6870
}
6971

packages/jest-environment/src/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ export type EnvironmentContext = {
2222
// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper
2323
type ModuleWrapper = (...args: Array<unknown>) => unknown;
2424

25-
export interface JestEnvironment {
26-
new (
27-
config: Config.ProjectConfig,
28-
context?: EnvironmentContext,
29-
): JestEnvironment;
25+
export declare class JestEnvironment {
26+
constructor(config: Config.ProjectConfig);
27+
constructor(config: Config.ProjectConfig, context: EnvironmentContext);
28+
global: Global.Global;
29+
fakeTimers: FakeTimers<unknown> | null;
30+
moduleMocker: ModuleMocker | null;
3031
runScript(
3132
script: Script,
3233
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
33-
global: Global.Global;
34-
// TODO: This is nullable, and TS doesn't understand we deal with it in `jest-runtime`. Should be fixed
35-
fakeTimers: FakeTimers<unknown>;
36-
testFilePath: Config.Path;
37-
moduleMocker: ModuleMocker;
3834
setup(): Promise<void>;
3935
teardown(): Promise<void>;
4036
}

packages/jest-runner/src/runTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async function runTestInternal(
9696
});
9797
}
9898

99-
const TestEnvironment: JestEnvironment = require(testEnvironment);
99+
const TestEnvironment: typeof JestEnvironment = require(testEnvironment);
100100
const testFramework: TestFramework =
101101
process.env.JEST_CIRCUS === '1'
102102
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies

packages/jest-runtime/src/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
8080
watchman: globalConfig.watchman,
8181
}) as Promise<Context>)
8282
.then(hasteMap => {
83-
const Environment: JestEnvironment = require(config.testEnvironment);
83+
const Environment: typeof JestEnvironment = require(config.testEnvironment);
8484
const environment = new Environment(config);
8585
setGlobal(
8686
environment.global,

packages/jest-runtime/src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class Runtime {
125125
this._isCurrentlyExecutingManualMock = null;
126126
this._mockFactories = Object.create(null);
127127
this._mockRegistry = Object.create(null);
128-
this._moduleMocker = this._environment.moduleMocker;
128+
// during setup, this cannot be null (and it's fine to explode if it is)
129+
this._moduleMocker = this._environment.moduleMocker!;
129130
this._isolatedModuleRegistry = null;
130131
this._isolatedMockRegistry = null;
131132
this._moduleRegistry = Object.create(null);
@@ -170,7 +171,7 @@ class Runtime {
170171
}
171172
}
172173

173-
// TODO: Can this be `static shouldInstrument = shouldInstrument;`?
174+
// TODO: Make this `static shouldInstrument = shouldInstrument;` after https://github.com/facebook/jest/issues/7846
174175
static shouldInstrument(
175176
filename: Config.Path,
176177
options: ShouldInstrumentOptions,
@@ -934,11 +935,11 @@ class Runtime {
934935
return jestObject;
935936
};
936937
const useFakeTimers = () => {
937-
this._environment.fakeTimers.useFakeTimers();
938+
_getFakeTimers().useFakeTimers();
938939
return jestObject;
939940
};
940941
const useRealTimers = () => {
941-
this._environment.fakeTimers.useRealTimers();
942+
_getFakeTimers().useRealTimers();
942943
return jestObject;
943944
};
944945
const resetModules = () => {
@@ -968,15 +969,16 @@ class Runtime {
968969
return jestObject;
969970
};
970971

971-
const _getFakeTimers = () => {
972+
const _getFakeTimers = (): NonNullable<JestEnvironment['fakeTimers']> => {
972973
if (!this._environment.fakeTimers) {
973974
this._logFormattedReferenceError(
974975
'You are trying to access a property or method of the Jest environment after it has been torn down.',
975976
);
976977
process.exitCode = 1;
977978
}
978979

979-
return this._environment.fakeTimers;
980+
// We've logged a user message above, so it doesn't matter if we return `null` here
981+
return this._environment.fakeTimers!;
980982
};
981983

982984
const jestObject: Jest = {
@@ -1024,7 +1026,7 @@ class Runtime {
10241026
return jestObject;
10251027
}
10261028

1027-
_logFormattedReferenceError(errorMessage: string) {
1029+
private _logFormattedReferenceError(errorMessage: string) {
10281030
const originalStack = new ReferenceError(errorMessage)
10291031
.stack!.split('\n')
10301032
// Remove this file from the stack (jest-message-utils will keep one line)

0 commit comments

Comments
 (0)