diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 693d400a4a0c..00aa0c374451 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -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; +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 | null; moduleMocker: ModuleMocker | null; - // runScript( - // script: Script, - // ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null | unknown; - runScript( - script: Script, - ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null; + runScript(script: Script): unknown; setup(): Promise; teardown(): Promise; } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 694738227f3a..137d9f09e4a3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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'; @@ -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.', );