Skip to content

Commit

Permalink
Change runScript return type to unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikael Lirbank committed Feb 27, 2019
1 parent f45cc88 commit 1f9081a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ export type EnvironmentContext = {
// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper
type ModuleWrapper = (...args: Array<unknown>) => unknown;

export function hasModuleWrapper(
obj: unknown,
): obj is {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} {
return (
typeof obj === 'object' &&
!!obj &&
obj.hasOwnProperty(ScriptTransformer.EVAL_RESULT_VARIABLE)
);
}

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 | unknown;
runScript(

This comment has been minimized.

Copy link
@SimenB

SimenB Feb 27, 2019

Member

I'd keep this as it was, and move the check into jest-environment-jsdom. It's an error from the environments if they don't return {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null, and it would be great if the typings told developers that instead of failing at runtime

script: Script,
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
runScript(script: Script): unknown;
setup(): Promise<void>;
teardown(): Promise<void>;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
JestEnvironment,
LocalModuleRequire,
Module,
hasModuleWrapper
} from '@jest/environment';
import jestMock, {MockFunctionMetadata} from 'jest-mock';
import HasteMap, {ModuleMap} from 'jest-haste-map';
Expand Down Expand Up @@ -679,7 +680,7 @@ class Runtime {

const runScript = this._environment.runScript(transformedFile.script);

if (runScript === null) {
if (!hasModuleWrapper(runScript)) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
Expand Down

0 comments on commit 1f9081a

Please sign in to comment.