From 8ba03dd27f4c6e10994388554d5f6e6508547a8c Mon Sep 17 00:00:00 2001 From: Olufemi Anjorin Date: Fri, 13 Nov 2020 13:33:36 +0100 Subject: [PATCH 01/11] rename moduleLoader to runtime --- packages/jest-config/src/Deprecated.ts | 6 ++++++ packages/jest-config/src/ValidConfig.ts | 2 +- packages/jest-config/src/index.ts | 2 +- packages/jest-config/src/normalize.ts | 2 +- .../__tests__/__snapshots__/logDebugMessages.test.ts.snap | 2 +- packages/jest-runner/src/runTest.ts | 4 +--- .../__tests__/__snapshots__/script_transformer.test.ts.snap | 6 +++--- packages/jest-types/src/Config.ts | 4 ++-- packages/jest-validate/src/__tests__/fixtures/jestConfig.ts | 2 +- packages/test-utils/src/config.ts | 2 +- 10 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index 841983fa1a3b..e0c523e7b053 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -26,6 +26,12 @@ const deprecatedOptions: DeprecatedOptions = { Please update your configuration.`, + moduleLoader: () => ` Option ${chalk.bold( + '"moduleLoader"', + )} has been renamed, to runtime. + + Please update your configuration.`, + preprocessorIgnorePatterns: (options: { preprocessorIgnorePatterns?: Array; }) => ` Option ${chalk.bold( diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 712f1fb967b5..fafe688b8120 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -66,7 +66,6 @@ const initialOptions: Config.InitialOptions = { maxWorkers: '50%', moduleDirectories: ['node_modules'], moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'], - moduleLoader: '', moduleNameMapper: { '^React$': '/node_modules/react', }, @@ -94,6 +93,7 @@ const initialOptions: Config.InitialOptions = { roots: [''], runTestsByPath: false, runner: 'jest-runner', + runtime: '', setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], silent: true, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index eac57f2fd94a..6c8ffaf8b3d2 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -188,7 +188,6 @@ const groupOptions = ( injectGlobals: options.injectGlobals, moduleDirectories: options.moduleDirectories, moduleFileExtensions: options.moduleFileExtensions, - moduleLoader: options.moduleLoader, moduleNameMapper: options.moduleNameMapper, modulePathIgnorePatterns: options.modulePathIgnorePatterns, modulePaths: options.modulePaths, @@ -201,6 +200,7 @@ const groupOptions = ( rootDir: options.rootDir, roots: options.roots, runner: options.runner, + runtime: options.runtime, setupFiles: options.setupFiles, setupFilesAfterEnv: options.setupFilesAfterEnv, skipFilter: options.skipFilter, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 97ea8dc7bfe2..8fb2f2a043bd 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -643,7 +643,7 @@ export default function normalize( case 'dependencyExtractor': case 'globalSetup': case 'globalTeardown': - case 'moduleLoader': + case 'runtime': case 'snapshotResolver': case 'testResultsProcessor': case 'testRunner': diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap index 3e384ab92cda..327fba70d2ce 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap @@ -21,7 +21,6 @@ exports[`prints the config object 1`] = ` "moduleFileExtensions": [ "js" ], - "moduleLoader": "/test_module_loader_path", "moduleNameMapper": [], "modulePathIgnorePatterns": [], "modulePaths": [], @@ -35,6 +34,7 @@ exports[`prints the config object 1`] = ` "path/to/dir/test" ], "runner": "jest-runner", + "runtime": "/test_module_loader_path", "setupFiles": [], "setupFilesAfterEnv": [], "skipFilter": false, diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 1174753dfb5d..3438363e7f9a 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -112,9 +112,7 @@ async function runTestInternal( : require(config.testRunner), ).default; const Runtime: typeof RuntimeClass = interopRequireDefault( - config.moduleLoader - ? require(config.moduleLoader) - : require('jest-runtime'), + config.runtime ? require(config.runtime) : require('jest-runtime'), ).default; const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout; diff --git a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap index c9021bd66fb9..6cfa56909671 100644 --- a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap +++ b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap @@ -25,7 +25,6 @@ Object { "moduleFileExtensions": Array [ "js", ], - "moduleLoader": "/test_module_loader_path", "moduleNameMapper": Array [], "modulePathIgnorePatterns": Array [], "modulePaths": Array [], @@ -38,6 +37,7 @@ Object { "rootDir": "/", "roots": Array [], "runner": "jest-runner", + "runtime": "/test_module_loader_path", "setupFiles": Array [], "setupFilesAfterEnv": Array [], "skipFilter": false, @@ -234,7 +234,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; @@ -251,7 +251,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index bc8c925777bd..eaf03d5bd98b 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -162,7 +162,6 @@ export type InitialOptions = Partial<{ maxWorkers: number | string; moduleDirectories: Array; moduleFileExtensions: Array; - moduleLoader: Path; moduleNameMapper: { [key: string]: string | Array; }; @@ -188,6 +187,7 @@ export type InitialOptions = Partial<{ rootDir: Path; roots: Array; runner: string; + runtime: Path; runTestsByPath: boolean; scriptPreprocessor: string; setupFiles: Array; @@ -337,7 +337,6 @@ export type ProjectConfig = { injectGlobals: boolean; moduleDirectories: Array; moduleFileExtensions: Array; - moduleLoader?: Path; moduleNameMapper: Array<[string, string]>; modulePathIgnorePatterns: Array; modulePaths?: Array; @@ -350,6 +349,7 @@ export type ProjectConfig = { rootDir: Path; roots: Array; runner: string; + runtime?: Path; setupFiles: Array; setupFilesAfterEnv: Array; skipFilter: boolean; diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts index da7a4c201017..89f49fd96341 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.ts @@ -84,7 +84,6 @@ const validConfig = { logHeapUsage: true, moduleDirectories: ['node_modules'], moduleFileExtensions: ['js', 'json', 'jsx', 'node'], - moduleLoader: '', moduleNameMapper: { '^React$': '/node_modules/react', '^Vue$': ['/node_modules/vue', '/node_modules/vue3'], @@ -102,6 +101,7 @@ const validConfig = { restoreMocks: false, rootDir: '/', roots: [''], + runtime: '', setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], silent: true, diff --git a/packages/test-utils/src/config.ts b/packages/test-utils/src/config.ts index f81132fa2215..add3c458d1da 100644 --- a/packages/test-utils/src/config.ts +++ b/packages/test-utils/src/config.ts @@ -85,7 +85,6 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { injectGlobals: true, moduleDirectories: [], moduleFileExtensions: ['js'], - moduleLoader: '/test_module_loader_path', moduleNameMapper: [], modulePathIgnorePatterns: [], modulePaths: [], @@ -98,6 +97,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { rootDir: '/test_root_dir/', roots: [], runner: 'jest-runner', + runtime: '/test_module_loader_path', setupFiles: [], setupFilesAfterEnv: [], skipFilter: false, From 81a06e220a9da6bd2b42507d4c411cfb04f6e7d2 Mon Sep 17 00:00:00 2001 From: Olufemi Anjorin Date: Fri, 13 Nov 2020 20:52:24 +0100 Subject: [PATCH 02/11] rename extraGlobals to sandboxInjectedGlobals --- .../__snapshots__/showConfig.test.ts.snap | 2 +- ...test.ts => sandboxInjectedGlobals.test.ts} | 2 +- packages/jest-config/src/Deprecated.ts | 6 ++++++ packages/jest-config/src/ValidConfig.ts | 2 +- packages/jest-config/src/index.ts | 2 +- packages/jest-config/src/normalize.ts | 6 +++--- .../logDebugMessages.test.ts.snap | 2 +- packages/jest-environment/src/index.ts | 2 +- .../src/__tests__/runtime_wrap.js | 4 +++- packages/jest-runtime/src/index.ts | 20 ++++++++++--------- .../script_transformer.test.ts.snap | 6 +++--- packages/jest-types/src/Config.ts | 4 ++-- packages/test-utils/src/config.ts | 2 +- 13 files changed, 35 insertions(+), 25 deletions(-) rename e2e/__tests__/{extraGlobals.test.ts => sandboxInjectedGlobals.test.ts} (91%) diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 9e24197f7ab0..14253dd8bc32 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -15,7 +15,6 @@ exports[`--showConfig outputs config info and exits 1`] = ` "detectLeaks": false, "detectOpenHandles": false, "errorOnDeprecated": false, - "extraGlobals": [], "forceCoverageMatch": [], "globals": {}, "haste": { @@ -46,6 +45,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` "<>" ], "runner": "jest-runner", + "sandboxInjectedGlobals": [], "setupFiles": [], "setupFilesAfterEnv": [], "skipFilter": false, diff --git a/e2e/__tests__/extraGlobals.test.ts b/e2e/__tests__/sandboxInjectedGlobals.test.ts similarity index 91% rename from e2e/__tests__/extraGlobals.test.ts rename to e2e/__tests__/sandboxInjectedGlobals.test.ts index 62d063f66895..925e11267cec 100644 --- a/e2e/__tests__/extraGlobals.test.ts +++ b/e2e/__tests__/sandboxInjectedGlobals.test.ts @@ -14,7 +14,7 @@ const DIR = path.resolve(tmpdir(), 'extra-globals'); beforeEach(() => { cleanup(DIR); - createEmptyPackage(DIR, {jest: {extraGlobals: ['Math']}}); + createEmptyPackage(DIR, {jest: {sandboxInjectedGlobals: ['Math']}}); }); afterAll(() => cleanup(DIR)); diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index e0c523e7b053..a6306dd9e4e4 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -20,6 +20,12 @@ const deprecatedOptions: DeprecatedOptions = { } `, + extraGlobals: () => ` Option ${chalk.bold( + '"extraGlobals"', + )} has been renamed, to sandboxInjectedGlobals. + + Please update your configuration.`, + mapCoverage: () => ` Option ${chalk.bold( '"mapCoverage"', )} has been removed, as it's no longer necessary. diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index fafe688b8120..0242f2ea8663 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -44,7 +44,6 @@ const initialOptions: Config.InitialOptions = { } as const), errorOnDeprecated: false, expand: false, - extraGlobals: [], filter: '/filter.js', forceCoverageMatch: ['**/*.t.js'], forceExit: false, @@ -94,6 +93,7 @@ const initialOptions: Config.InitialOptions = { runTestsByPath: false, runner: 'jest-runner', runtime: '', + sandboxInjectedGlobals: [], setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], silent: true, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 6c8ffaf8b3d2..50d21c914c53 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -178,7 +178,6 @@ const groupOptions = ( detectOpenHandles: options.detectOpenHandles, displayName: options.displayName, errorOnDeprecated: options.errorOnDeprecated, - extraGlobals: options.extraGlobals, filter: options.filter, forceCoverageMatch: options.forceCoverageMatch, globalSetup: options.globalSetup, @@ -201,6 +200,7 @@ const groupOptions = ( roots: options.roots, runner: options.runner, runtime: options.runtime, + sandboxInjectedGlobals: options.sandboxInjectedGlobals, setupFiles: options.setupFiles, setupFilesAfterEnv: options.setupFilesAfterEnv, skipFilter: options.skipFilter, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 8fb2f2a043bd..efc58dabec54 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -881,7 +881,6 @@ export default function normalize( case 'detectOpenHandles': case 'errorOnDeprecated': case 'expand': - case 'extraGlobals': case 'globals': case 'findRelatedTests': case 'forceCoverageMatch': @@ -907,6 +906,7 @@ export default function normalize( case 'restoreMocks': case 'rootDir': case 'runTestsByPath': + case 'sandboxInjectedGlobals': case 'silent': case 'skipFilter': case 'skipNodeResolution': @@ -1092,8 +1092,8 @@ export default function normalize( newOptions.projects = []; } - if (!newOptions.extraGlobals) { - newOptions.extraGlobals = []; + if (!newOptions.sandboxInjectedGlobals) { + newOptions.sandboxInjectedGlobals = []; } if (!newOptions.forceExit) { diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap index 327fba70d2ce..0ae0d84c35a4 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap @@ -12,7 +12,6 @@ exports[`prints the config object 1`] = ` "detectLeaks": false, "detectOpenHandles": false, "errorOnDeprecated": false, - "extraGlobals": [], "forceCoverageMatch": [], "globals": {}, "haste": {}, @@ -35,6 +34,7 @@ exports[`prints the config object 1`] = ` ], "runner": "jest-runner", "runtime": "/test_module_loader_path", + "sandboxInjectedGlobals": [], "setupFiles": [], "setupFilesAfterEnv": [], "skipFilter": false, diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index f83d0cb5c595..6a174f1f2e7f 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -30,7 +30,7 @@ export type ModuleWrapper = ( __dirname: string, __filename: Module['filename'], jest?: Jest, - ...extraGlobals: Array + ...sandboxInjectedGlobals: Array ) => unknown; export declare class JestEnvironment { diff --git a/packages/jest-runtime/src/__tests__/runtime_wrap.js b/packages/jest-runtime/src/__tests__/runtime_wrap.js index af60683b5f3d..55ba6ac6dd4e 100644 --- a/packages/jest-runtime/src/__tests__/runtime_wrap.js +++ b/packages/jest-runtime/src/__tests__/runtime_wrap.js @@ -24,7 +24,9 @@ describe('Runtime', () => { }); it('injects "extra globals"', async () => { - const runtime = await createRuntime(__filename, {extraGlobals: ['Math']}); + const runtime = await createRuntime(__filename, { + sandboxInjectedGlobals: ['Math'], + }); expect( wrap(runtime.wrapCodeInModuleWrapper('module.exports = "Hello!"')), diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 65376a7c7403..43e797ddf402 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1091,15 +1091,17 @@ export default class Runtime { const lastArgs: [Jest | undefined, ...Array] = [ this._config.injectGlobals ? jestObject : undefined, // jest object - ...this._config.extraGlobals.map(globalVariable => { - if (this._environment.global[globalVariable]) { - return this._environment.global[globalVariable]; - } + ...this._config.sandboxInjectedGlobals.map( + globalVariable => { + if (this._environment.global[globalVariable]) { + return this._environment.global[globalVariable]; + } - throw new Error( - `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`, - ); - }), + throw new Error( + `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`, + ); + }, + ), ]; if (!this._mainModule && filename === this._testPath) { @@ -1669,7 +1671,7 @@ export default class Runtime { '__dirname', '__filename', this._config.injectGlobals ? 'jest' : undefined, - ...this._config.extraGlobals, + ...this._config.sandboxInjectedGlobals, ].filter(notEmpty); } diff --git a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap index 6cfa56909671..89df760cf3f3 100644 --- a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap +++ b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap @@ -13,7 +13,6 @@ Object { "detectOpenHandles": false, "displayName": undefined, "errorOnDeprecated": false, - "extraGlobals": Array [], "filter": undefined, "forceCoverageMatch": Array [], "globalSetup": undefined, @@ -38,6 +37,7 @@ Object { "roots": Array [], "runner": "jest-runner", "runtime": "/test_module_loader_path", + "sandboxInjectedGlobals": Array [], "setupFiles": Array [], "setupFilesAfterEnv": Array [], "skipFilter": false, @@ -234,7 +234,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","sandboxInjectedGlobals":[],"setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; @@ -251,7 +251,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","sandboxInjectedGlobals":[],"setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index eaf03d5bd98b..ec95de5e7e51 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -142,7 +142,6 @@ export type InitialOptions = Partial<{ detectOpenHandles: boolean; displayName: string | DisplayName; expand: boolean; - extraGlobals: Array; filter: Path; findRelatedTests: boolean; forceCoverageMatch: Array; @@ -189,6 +188,7 @@ export type InitialOptions = Partial<{ runner: string; runtime: Path; runTestsByPath: boolean; + sandboxInjectedGlobals: Array; scriptPreprocessor: string; setupFiles: Array; setupTestFrameworkScriptFile: Path; @@ -327,7 +327,6 @@ export type ProjectConfig = { detectOpenHandles: boolean; displayName?: DisplayName; errorOnDeprecated: boolean; - extraGlobals: Array; filter?: Path; forceCoverageMatch: Array; globalSetup?: string; @@ -350,6 +349,7 @@ export type ProjectConfig = { roots: Array; runner: string; runtime?: Path; + sandboxInjectedGlobals: Array; setupFiles: Array; setupFilesAfterEnv: Array; skipFilter: boolean; diff --git a/packages/test-utils/src/config.ts b/packages/test-utils/src/config.ts index add3c458d1da..dfa63c3b7638 100644 --- a/packages/test-utils/src/config.ts +++ b/packages/test-utils/src/config.ts @@ -75,7 +75,6 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { detectOpenHandles: false, displayName: undefined, errorOnDeprecated: false, - extraGlobals: [], filter: undefined, forceCoverageMatch: [], globalSetup: undefined, @@ -98,6 +97,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { roots: [], runner: 'jest-runner', runtime: '/test_module_loader_path', + sandboxInjectedGlobals: [], setupFiles: [], setupFilesAfterEnv: [], skipFilter: false, From 7029bf28defac9e8fde8385f768457f2b161e1b9 Mon Sep 17 00:00:00 2001 From: Olufemi Anjorin Date: Fri, 13 Nov 2020 21:24:22 +0100 Subject: [PATCH 03/11] rename extraGlobals to sandboxInjectedGlobals --- .../__snapshots__/showConfig.test.ts.snap | 2 +- ...test.ts => sandboxInjectedGlobals.test.ts} | 2 +- packages/jest-config/src/Deprecated.ts | 6 ++++++ packages/jest-config/src/ValidConfig.ts | 2 +- packages/jest-config/src/index.ts | 2 +- packages/jest-config/src/normalize.ts | 6 +++--- .../logDebugMessages.test.ts.snap | 2 +- packages/jest-environment/src/index.ts | 2 +- .../src/__tests__/runtime_wrap.js | 4 +++- packages/jest-runtime/src/index.ts | 20 ++++++++++--------- .../script_transformer.test.ts.snap | 6 +++--- packages/jest-types/src/Config.ts | 6 +++--- packages/test-utils/src/config.ts | 2 +- 13 files changed, 36 insertions(+), 26 deletions(-) rename e2e/__tests__/{extraGlobals.test.ts => sandboxInjectedGlobals.test.ts} (91%) diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 9e24197f7ab0..14253dd8bc32 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -15,7 +15,6 @@ exports[`--showConfig outputs config info and exits 1`] = ` "detectLeaks": false, "detectOpenHandles": false, "errorOnDeprecated": false, - "extraGlobals": [], "forceCoverageMatch": [], "globals": {}, "haste": { @@ -46,6 +45,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` "<>" ], "runner": "jest-runner", + "sandboxInjectedGlobals": [], "setupFiles": [], "setupFilesAfterEnv": [], "skipFilter": false, diff --git a/e2e/__tests__/extraGlobals.test.ts b/e2e/__tests__/sandboxInjectedGlobals.test.ts similarity index 91% rename from e2e/__tests__/extraGlobals.test.ts rename to e2e/__tests__/sandboxInjectedGlobals.test.ts index 62d063f66895..925e11267cec 100644 --- a/e2e/__tests__/extraGlobals.test.ts +++ b/e2e/__tests__/sandboxInjectedGlobals.test.ts @@ -14,7 +14,7 @@ const DIR = path.resolve(tmpdir(), 'extra-globals'); beforeEach(() => { cleanup(DIR); - createEmptyPackage(DIR, {jest: {extraGlobals: ['Math']}}); + createEmptyPackage(DIR, {jest: {sandboxInjectedGlobals: ['Math']}}); }); afterAll(() => cleanup(DIR)); diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index e0c523e7b053..a6306dd9e4e4 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -20,6 +20,12 @@ const deprecatedOptions: DeprecatedOptions = { } `, + extraGlobals: () => ` Option ${chalk.bold( + '"extraGlobals"', + )} has been renamed, to sandboxInjectedGlobals. + + Please update your configuration.`, + mapCoverage: () => ` Option ${chalk.bold( '"mapCoverage"', )} has been removed, as it's no longer necessary. diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index fafe688b8120..0242f2ea8663 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -44,7 +44,6 @@ const initialOptions: Config.InitialOptions = { } as const), errorOnDeprecated: false, expand: false, - extraGlobals: [], filter: '/filter.js', forceCoverageMatch: ['**/*.t.js'], forceExit: false, @@ -94,6 +93,7 @@ const initialOptions: Config.InitialOptions = { runTestsByPath: false, runner: 'jest-runner', runtime: '', + sandboxInjectedGlobals: [], setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], silent: true, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 6c8ffaf8b3d2..50d21c914c53 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -178,7 +178,6 @@ const groupOptions = ( detectOpenHandles: options.detectOpenHandles, displayName: options.displayName, errorOnDeprecated: options.errorOnDeprecated, - extraGlobals: options.extraGlobals, filter: options.filter, forceCoverageMatch: options.forceCoverageMatch, globalSetup: options.globalSetup, @@ -201,6 +200,7 @@ const groupOptions = ( roots: options.roots, runner: options.runner, runtime: options.runtime, + sandboxInjectedGlobals: options.sandboxInjectedGlobals, setupFiles: options.setupFiles, setupFilesAfterEnv: options.setupFilesAfterEnv, skipFilter: options.skipFilter, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 8fb2f2a043bd..efc58dabec54 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -881,7 +881,6 @@ export default function normalize( case 'detectOpenHandles': case 'errorOnDeprecated': case 'expand': - case 'extraGlobals': case 'globals': case 'findRelatedTests': case 'forceCoverageMatch': @@ -907,6 +906,7 @@ export default function normalize( case 'restoreMocks': case 'rootDir': case 'runTestsByPath': + case 'sandboxInjectedGlobals': case 'silent': case 'skipFilter': case 'skipNodeResolution': @@ -1092,8 +1092,8 @@ export default function normalize( newOptions.projects = []; } - if (!newOptions.extraGlobals) { - newOptions.extraGlobals = []; + if (!newOptions.sandboxInjectedGlobals) { + newOptions.sandboxInjectedGlobals = []; } if (!newOptions.forceExit) { diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap index 327fba70d2ce..0ae0d84c35a4 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap @@ -12,7 +12,6 @@ exports[`prints the config object 1`] = ` "detectLeaks": false, "detectOpenHandles": false, "errorOnDeprecated": false, - "extraGlobals": [], "forceCoverageMatch": [], "globals": {}, "haste": {}, @@ -35,6 +34,7 @@ exports[`prints the config object 1`] = ` ], "runner": "jest-runner", "runtime": "/test_module_loader_path", + "sandboxInjectedGlobals": [], "setupFiles": [], "setupFilesAfterEnv": [], "skipFilter": false, diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index f83d0cb5c595..6a174f1f2e7f 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -30,7 +30,7 @@ export type ModuleWrapper = ( __dirname: string, __filename: Module['filename'], jest?: Jest, - ...extraGlobals: Array + ...sandboxInjectedGlobals: Array ) => unknown; export declare class JestEnvironment { diff --git a/packages/jest-runtime/src/__tests__/runtime_wrap.js b/packages/jest-runtime/src/__tests__/runtime_wrap.js index af60683b5f3d..55ba6ac6dd4e 100644 --- a/packages/jest-runtime/src/__tests__/runtime_wrap.js +++ b/packages/jest-runtime/src/__tests__/runtime_wrap.js @@ -24,7 +24,9 @@ describe('Runtime', () => { }); it('injects "extra globals"', async () => { - const runtime = await createRuntime(__filename, {extraGlobals: ['Math']}); + const runtime = await createRuntime(__filename, { + sandboxInjectedGlobals: ['Math'], + }); expect( wrap(runtime.wrapCodeInModuleWrapper('module.exports = "Hello!"')), diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 65376a7c7403..43e797ddf402 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1091,15 +1091,17 @@ export default class Runtime { const lastArgs: [Jest | undefined, ...Array] = [ this._config.injectGlobals ? jestObject : undefined, // jest object - ...this._config.extraGlobals.map(globalVariable => { - if (this._environment.global[globalVariable]) { - return this._environment.global[globalVariable]; - } + ...this._config.sandboxInjectedGlobals.map( + globalVariable => { + if (this._environment.global[globalVariable]) { + return this._environment.global[globalVariable]; + } - throw new Error( - `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`, - ); - }), + throw new Error( + `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`, + ); + }, + ), ]; if (!this._mainModule && filename === this._testPath) { @@ -1669,7 +1671,7 @@ export default class Runtime { '__dirname', '__filename', this._config.injectGlobals ? 'jest' : undefined, - ...this._config.extraGlobals, + ...this._config.sandboxInjectedGlobals, ].filter(notEmpty); } diff --git a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap index 6cfa56909671..89df760cf3f3 100644 --- a/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap +++ b/packages/jest-transform/src/__tests__/__snapshots__/script_transformer.test.ts.snap @@ -13,7 +13,6 @@ Object { "detectOpenHandles": false, "displayName": undefined, "errorOnDeprecated": false, - "extraGlobals": Array [], "filter": undefined, "forceCoverageMatch": Array [], "globalSetup": undefined, @@ -38,6 +37,7 @@ Object { "roots": Array [], "runner": "jest-runner", "runtime": "/test_module_loader_path", + "sandboxInjectedGlobals": Array [], "setupFiles": Array [], "setupFilesAfterEnv": Array [], "skipFilter": false, @@ -234,7 +234,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","sandboxInjectedGlobals":[],"setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}],["\\\\.css$","css-preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; @@ -251,7 +251,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = ` const TRANSFORMED = { filename: '/fruits/banana.js', script: 'module.exports = "banana";', - config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', + config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","runtime":"/test_module_loader_path","sandboxInjectedGlobals":[],"setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}', }; `; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index eaf03d5bd98b..e9ed764947ca 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -142,7 +142,6 @@ export type InitialOptions = Partial<{ detectOpenHandles: boolean; displayName: string | DisplayName; expand: boolean; - extraGlobals: Array; filter: Path; findRelatedTests: boolean; forceCoverageMatch: Array; @@ -187,8 +186,9 @@ export type InitialOptions = Partial<{ rootDir: Path; roots: Array; runner: string; - runtime: Path; runTestsByPath: boolean; + runtime: Path; + sandboxInjectedGlobals: Array; scriptPreprocessor: string; setupFiles: Array; setupTestFrameworkScriptFile: Path; @@ -327,7 +327,6 @@ export type ProjectConfig = { detectOpenHandles: boolean; displayName?: DisplayName; errorOnDeprecated: boolean; - extraGlobals: Array; filter?: Path; forceCoverageMatch: Array; globalSetup?: string; @@ -350,6 +349,7 @@ export type ProjectConfig = { roots: Array; runner: string; runtime?: Path; + sandboxInjectedGlobals: Array; setupFiles: Array; setupFilesAfterEnv: Array; skipFilter: boolean; diff --git a/packages/test-utils/src/config.ts b/packages/test-utils/src/config.ts index add3c458d1da..dfa63c3b7638 100644 --- a/packages/test-utils/src/config.ts +++ b/packages/test-utils/src/config.ts @@ -75,7 +75,6 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { detectOpenHandles: false, displayName: undefined, errorOnDeprecated: false, - extraGlobals: [], filter: undefined, forceCoverageMatch: [], globalSetup: undefined, @@ -98,6 +97,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = { roots: [], runner: 'jest-runner', runtime: '/test_module_loader_path', + sandboxInjectedGlobals: [], setupFiles: [], setupFilesAfterEnv: [], skipFilter: false, From c2d09afd3d288741811a0e3452da9824aab05d2d Mon Sep 17 00:00:00 2001 From: Olufemi Anjorin Date: Fri, 13 Nov 2020 21:34:57 +0100 Subject: [PATCH 04/11] update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8bfbfa97d82..f4977f11049a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## master ### Features - +- `[jest-config]` [**BREAKING**] Rename `moduleLoader` to `runtime` and `extraGlobals` to `sandboxInjectedGlobals` configuration options ([#10817](https://github.com/facebook/jest/pull/10817)) - `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874)) - `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728)) From af4f16828cfd6c3458349a2c424d9e500ba9e426 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:29:11 +0100 Subject: [PATCH 05/11] cleanup deprecations --- packages/jest-config/src/Deprecated.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index 61db61505c19..77e65c64949a 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -17,21 +17,17 @@ const deprecatedOptions: DeprecatedOptions = { '"browser"', )} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as shown in the documentation: https://jestjs.io/docs/configuration#resolver-string`, - extraGlobals: () => ` Option ${chalk.bold( + extraGlobals: (_options: {extraGlobals?: string}) => ` Option ${chalk.bold( '"extraGlobals"', - )} has been renamed, to sandboxInjectedGlobals. - - Please update your configuration.`, - - mapCoverage: () => ` Option ${chalk.bold( - '"mapCoverage"', - )} has been removed, as it's no longer necessary. + )} was replaced by passing the URL via ${chalk.bold( + '"sandboxInjectedGlobals"', + )}. Please update your configuration.`, - moduleLoader: () => ` Option ${chalk.bold( + moduleLoader: (_options: {moduleLoader?: string}) => ` Option ${chalk.bold( '"moduleLoader"', - )} has been renamed, to runtime. + )} was replaced by passing the URL via ${chalk.bold('"runtime"')}. Please update your configuration.`, From 348987aff10c5e1b5d4b3e8e5a1cb558ea212d94 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:32:16 +0100 Subject: [PATCH 06/11] oops --- packages/jest-types/src/Config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 7bc2e325a76f..cf0f6ae07187 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -379,7 +379,7 @@ export type ProjectConfig = { roots: Array; runner: string; runtime?: string; - sandboxInjectedGlobals: Array; + sandboxInjectedGlobals: Array; setupFiles: Array; setupFilesAfterEnv: Array; skipFilter: boolean; From ff402f6f480a8ba3d6d5ad0b74986c3812d23972 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:33:17 +0100 Subject: [PATCH 07/11] split changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f3f1fd9a33a..40949ac2e453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ - `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354)) - `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355)) - `[jest-config, @jest/types]` Add `ci` to `GlobalConfig` ([#12378](https://github.com/facebook/jest/pull/12378)) -- `[jest-config]` [**BREAKING**] Rename `moduleLoader` to `runtime` and `extraGlobals` to `sandboxInjectedGlobals` configuration options ([#10817](https://github.com/facebook/jest/pull/10817)) +- `[jest-config]` [**BREAKING**] Rename `moduleLoader` to `runtime` ([#10817](https://github.com/facebook/jest/pull/10817)) +- `[jest-config]` [**BREAKING**] Rename `extraGlobals` to `sandboxInjectedGlobals` ([#10817](https://github.com/facebook/jest/pull/10817)) - `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440)) - `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290)) - `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924)) From f80597b9be4b376bfa6519e3f9c6125375d48db6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:35:31 +0100 Subject: [PATCH 08/11] test deprecation --- packages/jest-config/src/Deprecated.ts | 6 +-- .../__snapshots__/normalize.test.ts.snap | 48 +++++++++++++++++++ .../src/__tests__/normalize.test.ts | 36 ++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index 77e65c64949a..4568d7fed3c0 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -19,15 +19,13 @@ const deprecatedOptions: DeprecatedOptions = { extraGlobals: (_options: {extraGlobals?: string}) => ` Option ${chalk.bold( '"extraGlobals"', - )} was replaced by passing the URL via ${chalk.bold( - '"sandboxInjectedGlobals"', - )}. + )} was replaced by ${chalk.bold('"sandboxInjectedGlobals"')}. Please update your configuration.`, moduleLoader: (_options: {moduleLoader?: string}) => ` Option ${chalk.bold( '"moduleLoader"', - )} was replaced by passing the URL via ${chalk.bold('"runtime"')}. + )} was replaced by ${chalk.bold('"runtime"')}. Please update your configuration.`, diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.ts.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.ts.snap index 53723751af42..46538f039b17 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.ts.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.ts.snap @@ -132,6 +132,54 @@ exports[`extensionsToTreatAsEsm throws on .mjs 1`] = ` " `; +exports[`extraGlobals logs a deprecation warning when \`extraGlobals\` is used 1`] = ` +[MockFunction] { + "calls": Array [ + Array [ + " Deprecation Warning: + + Option "extraGlobals" was replaced by "sandboxInjectedGlobals". + + Please update your configuration. + + Configuration Documentation: + https://jestjs.io/docs/configuration +", + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], +} +`; + +exports[`moduleLoader logs a deprecation warning when \`moduleLoader\` is used 1`] = ` +[MockFunction] { + "calls": Array [ + Array [ + " Deprecation Warning: + + Option "moduleLoader" was replaced by "runtime". + + Please update your configuration. + + Configuration Documentation: + https://jestjs.io/docs/configuration +", + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], +} +`; + exports[`preset throws when module was found but no "jest-preset.js" or "jest-preset.json" files 1`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index 2787645a3169..953daba30575 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1936,3 +1936,39 @@ describe('testURL', () => { expect(console.warn).toMatchSnapshot(); }); }); + +describe('extraGlobals', () => { + beforeEach(() => { + jest.mocked(console.warn).mockImplementation(() => {}); + }); + + it('logs a deprecation warning when `extraGlobals` is used', async () => { + await normalize( + { + extraGlobals: ['Math'], + rootDir: '/root/', + }, + {} as Config.Argv, + ); + + expect(console.warn).toMatchSnapshot(); + }); +}); + +describe('moduleLoader', () => { + beforeEach(() => { + jest.mocked(console.warn).mockImplementation(() => {}); + }); + + it('logs a deprecation warning when `moduleLoader` is used', async () => { + await normalize( + { + rootDir: '/root/', + moduleLoader: '/runtime.js', + }, + {} as Config.Argv, + ); + + expect(console.warn).toMatchSnapshot(); + }); +}); From ab44248d0f9d87ff1f46613943aa5d1afa3fe4cb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:38:47 +0100 Subject: [PATCH 09/11] docs --- docs/Configuration.md | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 9c7ccbd8118a..d9486f1627a2 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -375,23 +375,6 @@ Jest will run `.mjs` and `.js` files with nearest `package.json`'s `type` field } ``` -### `extraGlobals` \[array<string>] - -Default: `undefined` - -Test files run inside a [vm](https://nodejs.org/api/vm.html), which slows calls to global context properties (e.g. `Math`). With this option you can specify extra properties to be defined inside the vm for faster lookups. - -For example, if your tests call `Math` often, you can pass it by setting `extraGlobals`. - -```json -{ - ... - "jest": { - "extraGlobals": ["Math"] - } -} -``` - ### `forceCoverageMatch` \[array<string>] Default: `['']` @@ -907,6 +890,25 @@ async function runTests( If you need to restrict your test-runner to only run in serial rather than being executed in parallel your class should have the property `isSerial` to be set as `true`. +### `sandboxInjectedGlobals` \[array<string>] + +_Note: renamed from `extraGlobals` in Jest 28._ + +Default: `undefined` + +Test files run inside a [vm](https://nodejs.org/api/vm.html), which slows calls to global context properties (e.g. `Math`). With this option you can specify extra properties to be defined inside the vm for faster lookups. + +For example, if your tests call `Math` often, you can pass it by setting `sandboxInjectedGlobals`. + +```json +{ + ... + "jest": { + "sandboxInjectedGlobals": ["Math"] + } +} +``` + ### `setupFiles` \[array] Default: `[]` From 2979f5706e141bd3324cebf4d18e1c567d7c9fe3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 09:43:39 +0100 Subject: [PATCH 10/11] note esm compat --- docs/Configuration.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index d9486f1627a2..9552ca526793 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -892,7 +892,11 @@ If you need to restrict your test-runner to only run in serial rather than being ### `sandboxInjectedGlobals` \[array<string>] -_Note: renamed from `extraGlobals` in Jest 28._ +:::tip + +Renamed from `extraGlobals` in Jest 28. + +::: Default: `undefined` @@ -909,6 +913,12 @@ For example, if your tests call `Math` often, you can pass it by setting `sandbo } ``` +:::note + +This option has no effect if you use [native ESM](ECMAScriptModules.md). + +::: + ### `setupFiles` \[array] Default: `[]` From 81995722295b64e6dcc0af03a29af03db83bd3eb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Feb 2022 10:27:22 +0100 Subject: [PATCH 11/11] lint --- packages/jest-config/src/__tests__/normalize.test.ts | 2 +- packages/jest-runner/src/runTest.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index 953daba30575..ee77087457dc 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1963,8 +1963,8 @@ describe('moduleLoader', () => { it('logs a deprecation warning when `moduleLoader` is used', async () => { await normalize( { - rootDir: '/root/', moduleLoader: '/runtime.js', + rootDir: '/root/', }, {} as Config.Argv, ); diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 6ee794a4bdc0..c0ad57bbc93c 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -115,7 +115,9 @@ async function runTestInternal( : projectConfig.testRunner, ); const Runtime: typeof RuntimeClass = interopRequireDefault( - projectConfig.runtime ? require(projectConfig.runtime) : require('jest-runtime'), + projectConfig.runtime + ? require(projectConfig.runtime) + : require('jest-runtime'), ).default; const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;