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: cleanup JestEnvironment types #7988

Merged
merged 2 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -59,7 +59,7 @@
- `[jest-circus]`: Migrate to TypeScript ([#7916](https://github.com/facebook/jest/pull/7916))
- `[jest-phabricator]`: Migrate to TypeScript ([#7965](https://github.com/facebook/jest/pull/7965))
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964))
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))

### Performance
Expand Down
Expand Up @@ -47,7 +47,8 @@ const jestAdapter = async (
});

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

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

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

Expand Down
16 changes: 6 additions & 10 deletions packages/jest-environment/src/index.ts
Expand Up @@ -22,19 +22,15 @@ export type EnvironmentContext = {
// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper
type ModuleWrapper = (...args: Array<unknown>) => unknown;

export interface JestEnvironment {
new (
config: Config.ProjectConfig,
context?: EnvironmentContext,
): JestEnvironment;
export declare class JestEnvironment {
constructor(config: Config.ProjectConfig);
constructor(config: Config.ProjectConfig, context: EnvironmentContext);
global: Global.Global;
fakeTimers: FakeTimers<unknown> | null;
moduleMocker: ModuleMocker | null;
runScript(
script: Script,
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
global: Global.Global;
// TODO: This is nullable, and TS doesn't understand we deal with it in `jest-runtime`. Should be fixed
fakeTimers: FakeTimers<unknown>;
testFilePath: Config.Path;
moduleMocker: ModuleMocker;
setup(): Promise<void>;
teardown(): Promise<void>;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/runTest.ts
Expand Up @@ -96,7 +96,7 @@ async function runTestInternal(
});
}

const TestEnvironment: JestEnvironment = require(testEnvironment);
const TestEnvironment: typeof JestEnvironment = require(testEnvironment);
const testFramework: TestFramework =
process.env.JEST_CIRCUS === '1'
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/cli/index.ts
Expand Up @@ -80,7 +80,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
watchman: globalConfig.watchman,
}) as Promise<Context>)
.then(hasteMap => {
const Environment: JestEnvironment = require(config.testEnvironment);
const Environment: typeof JestEnvironment = require(config.testEnvironment);
const environment = new Environment(config);
setGlobal(
environment.global,
Expand Down
16 changes: 9 additions & 7 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -125,7 +125,8 @@ class Runtime {
this._isCurrentlyExecutingManualMock = null;
this._mockFactories = Object.create(null);
this._mockRegistry = Object.create(null);
this._moduleMocker = this._environment.moduleMocker;
// during setup, this cannot be null (and it's fine to explode if it is)
this._moduleMocker = this._environment.moduleMocker!;
this._isolatedModuleRegistry = null;
this._isolatedMockRegistry = null;
this._moduleRegistry = Object.create(null);
Expand Down Expand Up @@ -170,7 +171,7 @@ class Runtime {
}
}

// TODO: Can this be `static shouldInstrument = shouldInstrument;`?
// TODO: Make this `static shouldInstrument = shouldInstrument;` after https://github.com/facebook/jest/issues/7846
static shouldInstrument(
filename: Config.Path,
options: ShouldInstrumentOptions,
Expand Down Expand Up @@ -934,11 +935,11 @@ class Runtime {
return jestObject;
};
const useFakeTimers = () => {
this._environment.fakeTimers.useFakeTimers();
_getFakeTimers().useFakeTimers();
return jestObject;
};
const useRealTimers = () => {
this._environment.fakeTimers.useRealTimers();
_getFakeTimers().useRealTimers();
return jestObject;
};
const resetModules = () => {
Expand Down Expand Up @@ -968,15 +969,16 @@ class Runtime {
return jestObject;
};

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

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

const jestObject: Jest = {
Expand Down Expand Up @@ -1024,7 +1026,7 @@ class Runtime {
return jestObject;
}

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