From 1b644fd5da4319ab80fa8f2d2d608bf15af9c1f7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 11:25:17 +0100 Subject: [PATCH 1/5] chore: use `globalThis` over `global` everywhere --- .eslintrc.js | 24 ++- docs/Configuration.md | 6 +- docs/MongoDB.md | 4 +- docs/Puppeteer.md | 6 +- docs/Troubleshooting.md | 2 +- e2e/__tests__/global.test.ts | 2 +- e2e/__tests__/json.test.ts | 2 +- e2e/__tests__/multiProjectRunner.test.ts | 4 +- e2e/__tests__/symbol.test.ts | 3 +- e2e/__tests__/testEnvironment.test.ts | 2 +- .../__tests__/integration.test.js | 14 +- e2e/console-debugging/stdout-spy.js | 12 +- e2e/coverage-report/__tests__/sum.test.js | 2 +- e2e/coverage-report/setup.js | 2 +- e2e/env-test/__tests__/env.test.js | 2 +- .../__tests__/expect-in-vm.test.js | 2 +- e2e/fake-promises/asap/fake-promises.js | 2 +- e2e/fake-promises/immediate/fake-promises.js | 2 +- e2e/override-globals/index.js | 2 +- .../__tests__/resetModules.test.js | 3 +- .../__tests__/runnerPatch.test.js | 2 +- .../__tests__/test1.test.js | 2 +- .../__tests__/test2.test.js | 2 +- e2e/setup-files-after-env-config/setup1.js | 2 +- e2e/setup-files-after-env-config/setup2.js | 2 +- .../setupHooksIntoRunner.js | 2 +- .../__tests__/custom.test.js | 2 +- .../__tests__/testUsingESMTestEnv.test.js | 2 +- .../EnvUsingRunScript.js | 2 +- .../__tests__/customPreprocessor.test.js | 8 +- examples/mongodb/__tests__/db.test.js | 4 +- examples/mongodb/setup.js | 2 +- examples/mongodb/teardown.js | 2 +- .../__tests__/infinite_timer_game.test.js | 2 +- examples/timer/__tests__/timer_game.test.js | 2 +- packages/babel-plugin-jest-hoist/src/index.ts | 2 +- packages/diff-sequences/perf/index.js | 8 +- packages/expect/src/jestMatchersObject.ts | 12 +- packages/jest-circus/src/eventHandler.ts | 2 + packages/jest-circus/src/run.ts | 1 + packages/jest-circus/src/state.ts | 2 + packages/jest-circus/src/utils.ts | 2 +- .../jest-core/src/__tests__/globals.test.ts | 6 +- packages/jest-each/src/index.ts | 2 +- .../src/__mocks__/index.ts | 1 + .../src/__tests__/legacyFakeTimers.test.ts | 20 +- .../src/__tests__/modernFakeTimers.test.ts | 187 +++++++++++++----- .../jest-fake-timers/src/legacyFakeTimers.ts | 12 +- packages/jest-haste-map/src/index.ts | 2 +- .../src/__tests__/hooksError.test.ts | 2 +- .../src/__tests__/pTimeout.test.ts | 4 +- packages/jest-jasmine2/src/jasmine/Env.ts | 6 +- .../jest-jasmine2/src/jasmine/jasmineLight.ts | 2 + .../jest-jasmine2/src/jasmineAsyncInstall.ts | 2 +- packages/jest-jasmine2/src/jestExpect.ts | 2 + .../jest-jasmine2/src/setup_jest_globals.ts | 6 +- .../src/__tests__/index.test.ts | 12 +- packages/jest-leak-detector/src/index.ts | 3 +- packages/jest-mock/src/index.ts | 2 +- packages/jest-runner/src/runTest.ts | 6 +- .../jest-types/__typetests__/jest.test.ts | 2 +- .../__tests__/installCommonGlobals.test.ts | 2 +- .../jest-util/src/installCommonGlobals.ts | 4 +- packages/jest-util/src/isPromise.ts | 4 +- .../src/__performance_tests__/test.js | 8 +- .../src/workers/ChildProcessWorker.ts | 2 +- .../src/workers/NodeThreadsWorker.ts | 2 +- scripts/babel-plugin-jest-native-globals.js | 90 +-------- .../babel-plugin-jest-require-outside-vm.js | 2 +- 69 files changed, 296 insertions(+), 262 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index a6e843aef4e5..ff2c68ee9eb9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,8 +13,8 @@ const internalPackages = getPackages() module.exports = { env: { + es2020: true, 'jest/globals': true, - node: true, }, extends: [ 'plugin:markdown/recommended', @@ -23,7 +23,7 @@ module.exports = { 'plugin:prettier/recommended', ], globals: { - BigInt: 'readonly', + console: 'readonly', }, overrides: [ { @@ -41,7 +41,7 @@ module.exports = { {argsIgnorePattern: '^_'}, ], '@typescript-eslint/prefer-ts-expect-error': 'error', - // TS verifies this + // TS verifies these 'consistent-return': 'off', 'no-dupe-class-members': 'off', 'no-unused-vars': 'off', @@ -166,6 +166,7 @@ module.exports = { rules: { 'import/order': 'off', 'import/sort-keys': 'off', + 'no-restricted-globals': ['off'], 'sort-keys': 'off', }, }, @@ -228,9 +229,9 @@ module.exports = { 'e2e/jasmine-async/__tests__/*', ], globals: { - fail: true, - jasmine: true, - pending: true, + fail: 'readonly', + jasmine: 'readonly', + pending: 'readonly', }, }, { @@ -252,6 +253,10 @@ module.exports = { 'jest/valid-expect': 'off', }, }, + { + env: {node: true}, + files: ['*.js', '*.jsx'], + }, { files: [ 'scripts/*', @@ -431,6 +436,13 @@ module.exports = { 'no-prototype-builtins': 'error', 'no-redeclare': 'warn', 'no-regex-spaces': 'warn', + 'no-restricted-globals': [ + 'error', + { + message: 'Use `globalThis` instead.', + name: 'global', + }, + ], 'no-restricted-imports': [ 'error', {message: 'Please use graceful-fs instead.', name: 'fs'}, diff --git a/docs/Configuration.md b/docs/Configuration.md index ec927f3dbf9d..c7ffb8c9e6e3 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -463,13 +463,13 @@ Example: module.exports = async () => { // ... // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; + globalThis.__MONGOD__ = mongod; }; ``` ```js title="teardown.js" module.exports = async function () { - await global.__MONGOD__.stop(); + await globalThis.__MONGOD__.stop(); }; ``` @@ -1151,7 +1151,7 @@ module.exports = CustomEnvironment; let someGlobalObject; beforeAll(() => { - someGlobalObject = global.someGlobalObject; + someGlobalObject = globalThis.someGlobalObject; }); ``` diff --git a/docs/MongoDB.md b/docs/MongoDB.md index 3610da622673..e0bfe1c04e77 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -33,10 +33,10 @@ describe('insert', () => { let db; beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__, { + connection = await MongoClient.connect(globalThis.__MONGO_URI__, { useNewUrlParser: true, }); - db = await connection.db(global.__MONGO_DB_NAME__); + db = await connection.db(globalThis.__MONGO_DB_NAME__); }); afterAll(async () => { diff --git a/docs/Puppeteer.md b/docs/Puppeteer.md index b3cb2eebceb4..2f9beb581bc3 100644 --- a/docs/Puppeteer.md +++ b/docs/Puppeteer.md @@ -65,7 +65,7 @@ module.exports = async function () { const browser = await puppeteer.launch(); // store the browser instance so we can teardown it later // this global is only available in the teardown but not in TestEnvironments - global.__BROWSER_GLOBAL__ = browser; + globalThis.__BROWSER_GLOBAL__ = browser; // use the file system to expose the wsEndpoint for TestEnvironments await mkdir(DIR, {recursive: true}); @@ -125,7 +125,7 @@ const path = require('path'); const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); module.exports = async function () { // close the browser instance - await global.__BROWSER_GLOBAL__.close(); + await globalThis.__BROWSER_GLOBAL__.close(); // clean-up the wsEndpoint file await fs.rm(DIR, {recursive: true, force: true}); @@ -142,7 +142,7 @@ describe( () => { let page; beforeAll(async () => { - page = await global.__BROWSER_GLOBAL__.newPage(); + page = await globalThis.__BROWSER_GLOBAL__.newPage(); await page.goto('https://google.com'); }, timeout); diff --git a/docs/Troubleshooting.md b/docs/Troubleshooting.md index 28cea902472d..a663ff16fdfd 100644 --- a/docs/Troubleshooting.md +++ b/docs/Troubleshooting.md @@ -140,7 +140,7 @@ If a promise doesn't resolve at all, this error might be thrown: - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.` ``` -Most commonly this is being caused by conflicting Promise implementations. Consider replacing the global promise implementation with your own, for example `global.Promise = jest.requireActual('promise');` and/or consolidate the used Promise libraries to a single one. +Most commonly this is being caused by conflicting Promise implementations. Consider replacing the global promise implementation with your own, for example `globalThis.Promise = jest.requireActual('promise');` and/or consolidate the used Promise libraries to a single one. If your test is long running, you may want to consider to increase the timeout by calling `jest.setTimeout` diff --git a/e2e/__tests__/global.test.ts b/e2e/__tests__/global.test.ts index 8e7c61475ca0..04e93e76d414 100644 --- a/e2e/__tests__/global.test.ts +++ b/e2e/__tests__/global.test.ts @@ -6,5 +6,5 @@ */ test('globals are properly defined', () => { - expect(global.Object).toBe(Object); + expect(globalThis.Object).toBe(Object); }); diff --git a/e2e/__tests__/json.test.ts b/e2e/__tests__/json.test.ts index df56cb15d3c3..eaca0c4a9ae2 100644 --- a/e2e/__tests__/json.test.ts +++ b/e2e/__tests__/json.test.ts @@ -6,7 +6,7 @@ */ test('JSON is available in the global scope', () => { - expect(JSON).toBe(global.JSON); + expect(JSON).toBe(globalThis.JSON); }); test('JSON.parse creates objects from within this context', () => { diff --git a/e2e/__tests__/multiProjectRunner.test.ts b/e2e/__tests__/multiProjectRunner.test.ts index 85233e452468..b4b42588ccf8 100644 --- a/e2e/__tests__/multiProjectRunner.test.ts +++ b/e2e/__tests__/multiProjectRunner.test.ts @@ -353,10 +353,10 @@ test('resolves projects and their properly', () => { testEnvironment: 'node', }), 'project1/__tests__/test.test.js': - "test('project1', () => expect(global.project1).toBe(true))", + "test('project1', () => expect(globalThis.project1).toBe(true))", 'project1/project1_setup.js': 'global.project1 = true;', 'project2/__tests__/test.test.js': - "test('project2', () => expect(global.project2).toBe(true))", + "test('project2', () => expect(globalThis.project2).toBe(true))", 'project2/project2.conf.json': JSON.stringify({ name: 'project2', rootDir: '../', // root dir is set to the top level diff --git a/e2e/__tests__/symbol.test.ts b/e2e/__tests__/symbol.test.ts index 22406c346f61..4db34d48d0d0 100644 --- a/e2e/__tests__/symbol.test.ts +++ b/e2e/__tests__/symbol.test.ts @@ -6,7 +6,8 @@ */ test('Symbol deletion', () => { - global.Symbol = undefined; + // @ts-expect-error + globalThis.Symbol = undefined; expect({}).toEqual({}); }); diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 32f29cc968a2..ef57efd7ad76 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -50,7 +50,7 @@ it('handles missing `mocked` property', () => { jest.mocked(); test('halla', () => { - expect(global.thing).toBe('nope'); + expect(globalThis.thing).toBe('nope'); }); `, }); diff --git a/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js b/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js index da7fa4af07d5..845748de8575 100644 --- a/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js +++ b/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js @@ -34,10 +34,10 @@ let e; })(); jest.mock('../__test_modules__/f', () => { - if (!global.CALLS) { - global.CALLS = 0; + if (!globalThis.CALLS) { + globalThis.CALLS = 0; } - global.CALLS++; + globalThis.CALLS++; return { _isMock: true, @@ -116,15 +116,15 @@ describe('babel-plugin-jest-hoist', () => { it('only executes the module factories once', () => { jest.resetModules(); - global.CALLS = 0; + globalThis.CALLS = 0; require('../__test_modules__/f'); - expect(global.CALLS).toEqual(1); + expect(globalThis.CALLS).toEqual(1); require('../__test_modules__/f'); - expect(global.CALLS).toEqual(1); + expect(globalThis.CALLS).toEqual(1); - delete global.CALLS; + delete globalThis.CALLS; }); it('does not hoist dontMock calls before imports', () => { diff --git a/e2e/console-debugging/stdout-spy.js b/e2e/console-debugging/stdout-spy.js index 737acf1f4678..d813152260cb 100644 --- a/e2e/console-debugging/stdout-spy.js +++ b/e2e/console-debugging/stdout-spy.js @@ -8,19 +8,19 @@ const originalStdoutWrite = process.stdout.write.bind(process.stdout); -global.process.__stdoutWriteMock = global.process.__stdoutWriteMock || null; +process.__stdoutWriteMock = process.__stdoutWriteMock || null; /* This is a terrible hack to ensure that we monkeyPath stdoutWrite before the jest reporter does... */ -if (!global.process.__stdoutWriteMock) { - global.process.__stdoutWriteMock = (...args) => { - global.process.__stdoutWriteMock.text = args[0]; +if (!process.__stdoutWriteMock) { + process.__stdoutWriteMock = (...args) => { + process.__stdoutWriteMock.text = args[0]; originalStdoutWrite(...args); }; - process.stdout.write = global.process.__stdoutWriteMock; + process.stdout.write = process.__stdoutWriteMock; } -module.exports = global.process.__stdoutWriteMock; +module.exports = process.__stdoutWriteMock; diff --git a/e2e/coverage-report/__tests__/sum.test.js b/e2e/coverage-report/__tests__/sum.test.js index 21e90c7698b8..f91782ab67db 100644 --- a/e2e/coverage-report/__tests__/sum.test.js +++ b/e2e/coverage-report/__tests__/sum.test.js @@ -10,7 +10,7 @@ jest.mock('../sumDependency.js'); // call mock explicitly const {sum} = require('../sum'); -if (!global.setup) { +if (!globalThis.setup) { throw new Error('setup.js was not called.'); } diff --git a/e2e/coverage-report/setup.js b/e2e/coverage-report/setup.js index dc7cb70c808e..9c1a36bc7afa 100644 --- a/e2e/coverage-report/setup.js +++ b/e2e/coverage-report/setup.js @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -global.setup = true; +globalThis.setup = true; diff --git a/e2e/env-test/__tests__/env.test.js b/e2e/env-test/__tests__/env.test.js index dfef7d825e99..1ff23404a145 100644 --- a/e2e/env-test/__tests__/env.test.js +++ b/e2e/env-test/__tests__/env.test.js @@ -6,6 +6,6 @@ */ 'use strict'; -console.log(global.window ? 'WINDOW' : 'NO WINDOW'); +console.log(globalThis.window ? 'WINDOW' : 'NO WINDOW'); test('stub', () => expect(1).toBe(1)); diff --git a/e2e/expect-in-vm/__tests__/expect-in-vm.test.js b/e2e/expect-in-vm/__tests__/expect-in-vm.test.js index 183364245d92..6aebd8e7f86f 100644 --- a/e2e/expect-in-vm/__tests__/expect-in-vm.test.js +++ b/e2e/expect-in-vm/__tests__/expect-in-vm.test.js @@ -14,7 +14,7 @@ it('correctly expects RegExp inside a new VM context', () => { `(function(require, module, exports, __dirname, __filename, expect) { expect('ab12cd').toMatch(/ab12cd/); })`, - global, + globalThis, ); const module = { diff --git a/e2e/fake-promises/asap/fake-promises.js b/e2e/fake-promises/asap/fake-promises.js index 8ca9ed7d94dc..b2ae7b5549ae 100644 --- a/e2e/fake-promises/asap/fake-promises.js +++ b/e2e/fake-promises/asap/fake-promises.js @@ -7,4 +7,4 @@ 'use strict'; -global.Promise = require('promise'); +globalThis.Promise = require('promise'); diff --git a/e2e/fake-promises/immediate/fake-promises.js b/e2e/fake-promises/immediate/fake-promises.js index 080e9ccd3c8b..e84e08d86e10 100644 --- a/e2e/fake-promises/immediate/fake-promises.js +++ b/e2e/fake-promises/immediate/fake-promises.js @@ -7,4 +7,4 @@ 'use strict'; -global.Promise = require('promise/setimmediate'); +globalThis.Promise = require('promise/setimmediate'); diff --git a/e2e/override-globals/index.js b/e2e/override-globals/index.js index 561d42923a8c..b213a71292a6 100644 --- a/e2e/override-globals/index.js +++ b/e2e/override-globals/index.js @@ -5,6 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -global.Promise = function () { +globalThis.Promise = function () { throw new Error('Booo'); }; diff --git a/e2e/reset-modules/__tests__/resetModules.test.js b/e2e/reset-modules/__tests__/resetModules.test.js index 9be9e1217117..f09bb2a08f90 100644 --- a/e2e/reset-modules/__tests__/resetModules.test.js +++ b/e2e/reset-modules/__tests__/resetModules.test.js @@ -6,8 +6,7 @@ */ 'use strict'; -// eslint-disable-next-line no-undef -global.testObject = new Proxy( +globalThis.testObject = new Proxy( {}, { get: function getter(target, key) { diff --git a/e2e/setup-files-after-env-config/__tests__/runnerPatch.test.js b/e2e/setup-files-after-env-config/__tests__/runnerPatch.test.js index 624dc69885c0..cec808e5bee2 100644 --- a/e2e/setup-files-after-env-config/__tests__/runnerPatch.test.js +++ b/e2e/setup-files-after-env-config/__tests__/runnerPatch.test.js @@ -8,6 +8,6 @@ describe('setupFile', () => { it('patches jasmine in setup file', () => { - expect(global.describeDefined).toBe(true); + expect(globalThis.describeDefined).toBe(true); }); }); diff --git a/e2e/setup-files-after-env-config/__tests__/test1.test.js b/e2e/setup-files-after-env-config/__tests__/test1.test.js index 57b4eabc4e04..3407d32c5c2e 100644 --- a/e2e/setup-files-after-env-config/__tests__/test1.test.js +++ b/e2e/setup-files-after-env-config/__tests__/test1.test.js @@ -8,6 +8,6 @@ describe('test', () => { it('has predefined global variable', () => { - expect(global.definedInSetupFile).toEqual(true); + expect(globalThis.definedInSetupFile).toEqual(true); }); }); diff --git a/e2e/setup-files-after-env-config/__tests__/test2.test.js b/e2e/setup-files-after-env-config/__tests__/test2.test.js index 57b4eabc4e04..3407d32c5c2e 100644 --- a/e2e/setup-files-after-env-config/__tests__/test2.test.js +++ b/e2e/setup-files-after-env-config/__tests__/test2.test.js @@ -8,6 +8,6 @@ describe('test', () => { it('has predefined global variable', () => { - expect(global.definedInSetupFile).toEqual(true); + expect(globalThis.definedInSetupFile).toEqual(true); }); }); diff --git a/e2e/setup-files-after-env-config/setup1.js b/e2e/setup-files-after-env-config/setup1.js index 343515c503b9..e908da4700aa 100644 --- a/e2e/setup-files-after-env-config/setup1.js +++ b/e2e/setup-files-after-env-config/setup1.js @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -global.definedInSetupFile = true; +globalThis.definedInSetupFile = true; diff --git a/e2e/setup-files-after-env-config/setup2.js b/e2e/setup-files-after-env-config/setup2.js index 343515c503b9..e908da4700aa 100644 --- a/e2e/setup-files-after-env-config/setup2.js +++ b/e2e/setup-files-after-env-config/setup2.js @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -global.definedInSetupFile = true; +globalThis.definedInSetupFile = true; diff --git a/e2e/setup-files-after-env-config/setupHooksIntoRunner.js b/e2e/setup-files-after-env-config/setupHooksIntoRunner.js index abae35bde5ad..bd860ba9e1eb 100644 --- a/e2e/setup-files-after-env-config/setupHooksIntoRunner.js +++ b/e2e/setup-files-after-env-config/setupHooksIntoRunner.js @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -global.describeDefined = !!global.describe; +globalThis.describeDefined = !!globalThis.describe; diff --git a/e2e/test-environment-async/__tests__/custom.test.js b/e2e/test-environment-async/__tests__/custom.test.js index 91eadf76a535..d43b0d64d4d7 100644 --- a/e2e/test-environment-async/__tests__/custom.test.js +++ b/e2e/test-environment-async/__tests__/custom.test.js @@ -8,5 +8,5 @@ 'use strict'; test('setup', () => { - expect(global.setup).toBe('setup'); + expect(globalThis.setup).toBe('setup'); }); diff --git a/e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js b/e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js index b60ff5d6d94f..ed6ebd4161e2 100644 --- a/e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js +++ b/e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js @@ -7,5 +7,5 @@ 'use strict'; test('dummy', () => { - expect(global.someVar).toBe(42); + expect(globalThis.someVar).toBe(42); }); diff --git a/e2e/test-environment-run-script/EnvUsingRunScript.js b/e2e/test-environment-run-script/EnvUsingRunScript.js index 0c106dd9970c..a4ae5f1900f9 100644 --- a/e2e/test-environment-run-script/EnvUsingRunScript.js +++ b/e2e/test-environment-run-script/EnvUsingRunScript.js @@ -8,7 +8,7 @@ module.exports = class Env { constructor() { - this.global = global; + this.global = globalThis; this.moduleMocker = {}; } }; diff --git a/e2e/transform/custom-instrumenting-preprocessor/__tests__/customPreprocessor.test.js b/e2e/transform/custom-instrumenting-preprocessor/__tests__/customPreprocessor.test.js index af4ff9bfcbde..eaf8a43f5659 100644 --- a/e2e/transform/custom-instrumenting-preprocessor/__tests__/customPreprocessor.test.js +++ b/e2e/transform/custom-instrumenting-preprocessor/__tests__/customPreprocessor.test.js @@ -9,10 +9,10 @@ require('../src'); -it('instruments by setting global.__INSTRUMENTED__', () => { - expect(global.__INSTRUMENTED__).toBe(true); +it('instruments by setting globalThis.__INSTRUMENTED__', () => { + expect(globalThis.__INSTRUMENTED__).toBe(true); }); -it('preprocesses by setting global.__PREPROCESSED__', () => { - expect(global.__PREPROCESSED__).toBe(true); +it('preprocesses by setting globalThis.__PREPROCESSED__', () => { + expect(globalThis.__PREPROCESSED__).toBe(true); }); diff --git a/examples/mongodb/__tests__/db.test.js b/examples/mongodb/__tests__/db.test.js index 9b6e4cc6201e..cf1c75b631ff 100644 --- a/examples/mongodb/__tests__/db.test.js +++ b/examples/mongodb/__tests__/db.test.js @@ -4,11 +4,11 @@ let connection; let db; beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__, { + connection = await MongoClient.connect(globalThis.__MONGO_URI__, { useNewUrlParser: true, useUnifiedTopology: true, }); - db = await connection.db(global.__MONGO_DB_NAME__); + db = await connection.db(globalThis.__MONGO_DB_NAME__); }); afterAll(async () => { diff --git a/examples/mongodb/setup.js b/examples/mongodb/setup.js index e7ca973fa043..095d57b150b6 100644 --- a/examples/mongodb/setup.js +++ b/examples/mongodb/setup.js @@ -25,5 +25,5 @@ module.exports = async () => { fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig)); // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; + globalThis.__MONGOD__ = mongod; }; diff --git a/examples/mongodb/teardown.js b/examples/mongodb/teardown.js index ab260a66005a..2104c8ebf451 100644 --- a/examples/mongodb/teardown.js +++ b/examples/mongodb/teardown.js @@ -6,7 +6,7 @@ const fs = require('fs'); const globalConfigPath = path.join(__dirname, 'globalConfig.json'); module.exports = async function () { - await global.__MONGOD__.stop(); + await globalThis.__MONGOD__.stop(); fs.unlinkSync(globalConfigPath); }; diff --git a/examples/timer/__tests__/infinite_timer_game.test.js b/examples/timer/__tests__/infinite_timer_game.test.js index 8205ccc68a9c..9266ded414e2 100644 --- a/examples/timer/__tests__/infinite_timer_game.test.js +++ b/examples/timer/__tests__/infinite_timer_game.test.js @@ -5,7 +5,7 @@ jest.useFakeTimers(); it('schedules a 10-second timer after 1 second', () => { - jest.spyOn(global, 'setTimeout'); + jest.spyOn(globalThis, 'setTimeout'); const infiniteTimerGame = require('../infiniteTimerGame'); const callback = jest.fn(); diff --git a/examples/timer/__tests__/timer_game.test.js b/examples/timer/__tests__/timer_game.test.js index c2f55ea24ecf..f7f94304f2e3 100644 --- a/examples/timer/__tests__/timer_game.test.js +++ b/examples/timer/__tests__/timer_game.test.js @@ -6,7 +6,7 @@ jest.useFakeTimers(); describe('timerGame', () => { beforeEach(() => { - jest.spyOn(global, 'setTimeout'); + jest.spyOn(globalThis, 'setTimeout'); }); it('waits 1 second before ending the game', () => { const timerGame = require('../timerGame'); diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index 0d47bb6ac05b..93ec81e017fa 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -91,7 +91,7 @@ const ALLOWED_IDENTIFIERS = new Set( '__filename', '__dirname', 'undefined', - ...Object.getOwnPropertyNames(global), + ...Object.getOwnPropertyNames(globalThis), ].sort(), ); diff --git a/packages/diff-sequences/perf/index.js b/packages/diff-sequences/perf/index.js index 5673f9db1ee3..f537b61f14e2 100644 --- a/packages/diff-sequences/perf/index.js +++ b/packages/diff-sequences/perf/index.js @@ -26,10 +26,10 @@ const testBaseline = (a, b) => { }, name: 'baseline', onCycle() { - global.gc(); // after run cycle + globalThis.gc(); // after run cycle }, onStart() { - global.gc(); // when benchmark starts + globalThis.gc(); // when benchmark starts }, }); @@ -56,10 +56,10 @@ const testImproved = function (a, b) { }, name: 'improved', onCycle() { - global.gc(); // after run cycle + globalThis.gc(); // after run cycle }, onStart() { - global.gc(); // when benchmark starts + globalThis.gc(); // when benchmark starts }, }); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index 16175ca70c72..90e26e6b7779 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -22,14 +22,14 @@ const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object'); // Jest may override the stack trace of Errors thrown by internal matchers. export const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher'); -if (!Object.prototype.hasOwnProperty.call(global, JEST_MATCHERS_OBJECT)) { +if (!Object.prototype.hasOwnProperty.call(globalThis, JEST_MATCHERS_OBJECT)) { const defaultState: Partial = { assertionCalls: 0, expectedAssertionsNumber: null, isExpectingAssertions: false, suppressedErrors: [], // errors that are not thrown immediately. }; - Object.defineProperty(global, JEST_MATCHERS_OBJECT, { + Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, { value: { matchers: Object.create(null), state: defaultState, @@ -38,16 +38,16 @@ if (!Object.prototype.hasOwnProperty.call(global, JEST_MATCHERS_OBJECT)) { } export const getState = (): State => - (global as any)[JEST_MATCHERS_OBJECT].state; + (globalThis as any)[JEST_MATCHERS_OBJECT].state; export const setState = ( state: Partial, ): void => { - Object.assign((global as any)[JEST_MATCHERS_OBJECT].state, state); + Object.assign((globalThis as any)[JEST_MATCHERS_OBJECT].state, state); }; export const getMatchers = (): MatchersObject => - (global as any)[JEST_MATCHERS_OBJECT].matchers; + (globalThis as any)[JEST_MATCHERS_OBJECT].matchers; export const setMatchers = ( matchers: MatchersObject, @@ -110,5 +110,5 @@ export const setMatchers = ( } }); - Object.assign((global as any)[JEST_MATCHERS_OBJECT].matchers, matchers); + Object.assign((globalThis as any)[JEST_MATCHERS_OBJECT].matchers, matchers); }; diff --git a/packages/jest-circus/src/eventHandler.ts b/packages/jest-circus/src/eventHandler.ts index 0f023cc525a8..481445c6623c 100644 --- a/packages/jest-circus/src/eventHandler.ts +++ b/packages/jest-circus/src/eventHandler.ts @@ -214,8 +214,10 @@ const eventHandler: Circus.EventHandler = ( } case 'run_start': { state.hasStarted = true; + /* eslint-disable no-restricted-globals */ global[TEST_TIMEOUT_SYMBOL] && (state.testTimeout = global[TEST_TIMEOUT_SYMBOL]); + /* eslint-enable */ break; } case 'run_finish': { diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index 65d0cf283a9b..d154709b79b2 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -43,6 +43,7 @@ const _runTestsForDescribeBlock = async ( } // Tests that fail and are retried we run after other tests + // eslint-disable-next-line no-restricted-globals const retryTimes = parseInt(global[RETRY_TIMES], 10) || 0; const deferredRetryTests = []; diff --git a/packages/jest-circus/src/state.ts b/packages/jest-circus/src/state.ts index 6f208ab04fcf..264e0d160746 100644 --- a/packages/jest-circus/src/state.ts +++ b/packages/jest-circus/src/state.ts @@ -35,6 +35,7 @@ const createState = (): Circus.State => { }; }; +/* eslint-disable no-restricted-globals */ export const resetState = (): void => { global[STATE_SYM] = createState(); }; @@ -44,6 +45,7 @@ resetState(); export const getState = (): Circus.State => global[STATE_SYM]; export const setState = (state: Circus.State): Circus.State => (global[STATE_SYM] = state); +/* eslint-enable */ export const dispatch = async (event: Circus.AsyncEvent): Promise => { for (const handler of eventHandlers) { diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index 1ae2b1b90997..bdefe44d5c82 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -162,7 +162,7 @@ const _makeTimeoutMessage = (timeout: number, isHook: boolean) => // Global values can be overwritten by mocks or tests. We'll capture // the original values in the variables before we require any files. -const {setTimeout, clearTimeout} = global; +const {setTimeout, clearTimeout} = globalThis; function checkIsError(error: unknown): error is Error { return !!(error && (error as Error).message && (error as Error).stack); diff --git a/packages/jest-core/src/__tests__/globals.test.ts b/packages/jest-core/src/__tests__/globals.test.ts index 7c6b2ca5354d..a6fa329a1d34 100644 --- a/packages/jest-core/src/__tests__/globals.test.ts +++ b/packages/jest-core/src/__tests__/globals.test.ts @@ -5,14 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -'use strict'; - describe('Common globals', () => { it('check process', () => { if (Symbol && Symbol.toStringTag) { - expect(Object.prototype.toString.call(global.process)).toBe( - '[object process]', - ); + expect(Object.prototype.toString.call(process)).toBe('[object process]'); } }); }); diff --git a/packages/jest-each/src/index.ts b/packages/jest-each/src/index.ts index 104ef047dc6b..529a17eca14f 100644 --- a/packages/jest-each/src/index.ts +++ b/packages/jest-each/src/index.ts @@ -70,7 +70,7 @@ const each = ( table: Global.EachTable, ...data: Global.TemplateData ): ReturnType => - install(global as unknown as Global, table, ...data); + install(globalThis as unknown as Global, table, ...data); each.withGlobal = (g: Global) => diff --git a/packages/jest-environment-jsdom/src/__mocks__/index.ts b/packages/jest-environment-jsdom/src/__mocks__/index.ts index 6f2641dfb719..9772dbf416bd 100644 --- a/packages/jest-environment-jsdom/src/__mocks__/index.ts +++ b/packages/jest-environment-jsdom/src/__mocks__/index.ts @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + 'use strict'; const vm = jest.requireActual('vm'); diff --git a/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts b/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts index e41be1303e39..291d7c17977d 100644 --- a/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts +++ b/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts @@ -514,7 +514,7 @@ describe('FakeTimers', () => { rootDir: __dirname, testMatch: [], }, - global, + global: globalThis, moduleMocker, timerConfig, }); @@ -1511,16 +1511,16 @@ describe('FakeTimers', () => { it('returns the correct count', () => { const timers = new FakeTimers({ config, - global, + global: globalThis, moduleMocker, timerConfig, }); timers.useFakeTimers(); - global.setTimeout(() => {}, 0); - global.setTimeout(() => {}, 0); - global.setTimeout(() => {}, 10); + globalThis.setTimeout(() => {}, 0); + globalThis.setTimeout(() => {}, 0); + globalThis.setTimeout(() => {}, 10); expect(timers.getTimerCount()).toEqual(3); @@ -1536,15 +1536,15 @@ describe('FakeTimers', () => { it('includes immediates and ticks', () => { const timers = new FakeTimers({ config, - global, + global: globalThis, moduleMocker, timerConfig, }); timers.useFakeTimers(); - global.setTimeout(() => {}, 0); - global.setImmediate(() => {}); + globalThis.setTimeout(() => {}, 0); + globalThis.setImmediate(() => {}); process.nextTick(() => {}); expect(timers.getTimerCount()).toEqual(3); @@ -1553,14 +1553,14 @@ describe('FakeTimers', () => { it('not includes cancelled immediates', () => { const timers = new FakeTimers({ config, - global, + global: globalThis, moduleMocker, timerConfig, }); timers.useFakeTimers(); - global.setImmediate(() => {}); + globalThis.setImmediate(() => {}); expect(timers.getTimerCount()).toEqual(1); timers.clearAllTimers(); diff --git a/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts b/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts index a731c594104c..36b14aecc128 100644 --- a/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts +++ b/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts @@ -11,28 +11,48 @@ import FakeTimers from '../modernFakeTimers'; describe('FakeTimers', () => { describe('construction', () => { it('installs setTimeout mock', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setTimeout).not.toBe(undefined); }); it('installs clearTimeout mock', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearTimeout).not.toBe(undefined); }); it('installs setInterval mock', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setInterval).not.toBe(undefined); }); it('installs clearInterval mock', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearInterval).not.toBe(undefined); @@ -47,7 +67,7 @@ describe('FakeTimers', () => { nextTick: origNextTick, }, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.process.nextTick).not.toBe(origNextTick); @@ -61,7 +81,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setImmediate).not.toBe(origSetImmediate); @@ -77,7 +97,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearImmediate).not.toBe(origClearImmediate); @@ -93,7 +113,7 @@ describe('FakeTimers', () => { nextTick: () => {}, }, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -124,7 +144,7 @@ describe('FakeTimers', () => { nextTick, }, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -141,7 +161,7 @@ describe('FakeTimers', () => { nextTick: () => {}, }, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -165,7 +185,7 @@ describe('FakeTimers', () => { nextTick: () => {}, }, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global, maxLoops: 100}); @@ -185,7 +205,12 @@ describe('FakeTimers', () => { describe('runAllTimers', () => { it('runs all timers in order', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -225,7 +250,7 @@ describe('FakeTimers', () => { config: { rootDir: __dirname, }, - global, + global: globalThis, }); timers.runAllTimers(); expect( @@ -241,7 +266,7 @@ describe('FakeTimers', () => { clearTimeout, process, setTimeout: nativeSetTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -249,7 +274,12 @@ describe('FakeTimers', () => { }); it('only runs a setTimeout callback once (ever)', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -265,7 +295,12 @@ describe('FakeTimers', () => { }); it('runs callbacks with arguments after the interval', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -285,7 +320,7 @@ describe('FakeTimers', () => { clearTimeout, process, setTimeout: nativeSetTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); // @sinonjs/fake-timers uses `setTimeout` during init to figure out if it's in Node or @@ -302,7 +337,12 @@ describe('FakeTimers', () => { }); it('throws before allowing infinite recursion', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global, maxLoops: 100}); timers.useFakeTimers(); @@ -320,7 +360,12 @@ describe('FakeTimers', () => { }); it('also clears ticks', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -337,7 +382,12 @@ describe('FakeTimers', () => { describe('advanceTimersByTime', () => { it('runs timers in order', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -376,7 +426,12 @@ describe('FakeTimers', () => { }); it('does nothing when no timers have been scheduled', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -386,7 +441,12 @@ describe('FakeTimers', () => { describe('advanceTimersToNextTimer', () => { it('runs timers in order', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -421,7 +481,12 @@ describe('FakeTimers', () => { }); it('run correct amount of steps', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -455,7 +520,12 @@ describe('FakeTimers', () => { }); it('setTimeout inside setTimeout', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -478,7 +548,12 @@ describe('FakeTimers', () => { }); it('does nothing when no timers have been scheduled', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -488,7 +563,12 @@ describe('FakeTimers', () => { describe('reset', () => { it('resets all pending setTimeouts', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -501,7 +581,12 @@ describe('FakeTimers', () => { }); it('resets all pending setIntervals', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -522,7 +607,7 @@ describe('FakeTimers', () => { }, setImmediate: () => {}, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -536,7 +621,12 @@ describe('FakeTimers', () => { }); it('resets current advanceTimersByTime time cursor', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -562,7 +652,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -623,7 +713,12 @@ describe('FakeTimers', () => { }); it('does not run timers that were cleared in another timer', () => { - const global = {Date, clearTimeout, process, setTimeout}; + const global = { + Date, + clearTimeout, + process, + setTimeout, + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -652,7 +747,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -679,7 +774,7 @@ describe('FakeTimers', () => { clearTimeout, process: {nextTick: nativeProcessNextTick}, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -703,7 +798,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useFakeTimers(); @@ -733,7 +828,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useRealTimers(); @@ -760,7 +855,7 @@ describe('FakeTimers', () => { clearTimeout, process: {nextTick: nativeProcessNextTick}, setTimeout, - }; + } as unknown as typeof globalThis; const timers = new FakeTimers({global}); timers.useRealTimers(); @@ -784,7 +879,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, setTimeout, - }; + } as unknown as typeof globalThis; const fakeTimers = new FakeTimers({global}); fakeTimers.useRealTimers(); @@ -802,13 +897,13 @@ describe('FakeTimers', () => { describe('getTimerCount', () => { it('returns the correct count', () => { - const timers = new FakeTimers({global}); + const timers = new FakeTimers({global: globalThis}); timers.useFakeTimers(); - global.setTimeout(() => {}, 0); - global.setTimeout(() => {}, 0); - global.setTimeout(() => {}, 10); + globalThis.setTimeout(() => {}, 0); + globalThis.setTimeout(() => {}, 0); + globalThis.setTimeout(() => {}, 10); expect(timers.getTimerCount()).toEqual(3); @@ -822,23 +917,23 @@ describe('FakeTimers', () => { }); it('includes immediates and ticks', () => { - const timers = new FakeTimers({global}); + const timers = new FakeTimers({global: globalThis}); timers.useFakeTimers(); - global.setTimeout(() => {}, 0); - global.setImmediate(() => {}); + globalThis.setTimeout(() => {}, 0); + globalThis.setImmediate(() => {}); process.nextTick(() => {}); expect(timers.getTimerCount()).toEqual(3); }); it('not includes cancelled immediates', () => { - const timers = new FakeTimers({global}); + const timers = new FakeTimers({global: globalThis}); timers.useFakeTimers(); - global.setImmediate(() => {}); + globalThis.setImmediate(() => {}); expect(timers.getTimerCount()).toEqual(1); timers.clearAllTimers(); diff --git a/packages/jest-fake-timers/src/legacyFakeTimers.ts b/packages/jest-fake-timers/src/legacyFakeTimers.ts index 46dce1ae005d..8ece544688e4 100644 --- a/packages/jest-fake-timers/src/legacyFakeTimers.ts +++ b/packages/jest-fake-timers/src/legacyFakeTimers.ts @@ -30,15 +30,15 @@ type Timer = { type TimerAPI = { cancelAnimationFrame: FakeTimersGlobal['cancelAnimationFrame']; - clearImmediate: typeof global.clearImmediate; - clearInterval: typeof global.clearInterval; - clearTimeout: typeof global.clearTimeout; + clearImmediate: typeof globalThis.clearImmediate; + clearInterval: typeof globalThis.clearInterval; + clearTimeout: typeof globalThis.clearTimeout; nextTick: typeof process.nextTick; requestAnimationFrame: FakeTimersGlobal['requestAnimationFrame']; - setImmediate: typeof global.setImmediate; - setInterval: typeof global.setInterval; - setTimeout: typeof global.setTimeout; + setImmediate: typeof globalThis.setImmediate; + setInterval: typeof globalThis.setInterval; + setTimeout: typeof globalThis.setTimeout; }; type TimerConfig = { diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index b87b9c13111a..e538d631702a 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -267,7 +267,7 @@ export default class HasteMap extends EventEmitter { useWatchman: options.useWatchman == null ? true : options.useWatchman, watch: !!options.watch, }; - this._console = options.console || global.console; + this._console = options.console || globalThis.console; if (options.ignorePattern) { if (options.ignorePattern instanceof RegExp) { diff --git a/packages/jest-jasmine2/src/__tests__/hooksError.test.ts b/packages/jest-jasmine2/src/__tests__/hooksError.test.ts index e760b48b7b1c..13b80219ceeb 100644 --- a/packages/jest-jasmine2/src/__tests__/hooksError.test.ts +++ b/packages/jest-jasmine2/src/__tests__/hooksError.test.ts @@ -24,7 +24,7 @@ describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])( `${fn} throws an error when %p is provided as a first argument to it`, el => { expect(() => { - global[fn](el); + globalThis[fn](el); }).toThrowError( 'Invalid first argument. It must be a callback function.', ); diff --git a/packages/jest-jasmine2/src/__tests__/pTimeout.test.ts b/packages/jest-jasmine2/src/__tests__/pTimeout.test.ts index d5099ba0f03d..371a352e3588 100644 --- a/packages/jest-jasmine2/src/__tests__/pTimeout.test.ts +++ b/packages/jest-jasmine2/src/__tests__/pTimeout.test.ts @@ -12,8 +12,8 @@ import pTimeout from '../pTimeout'; describe('pTimeout', () => { beforeEach(() => { - jest.spyOn(global, 'setTimeout'); - jest.spyOn(global, 'clearTimeout'); + jest.spyOn(globalThis, 'setTimeout'); + jest.spyOn(globalThis, 'clearTimeout'); }); it('calls `clearTimeout` and resolves when `promise` resolves.', async () => { diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 915d7b21feaf..cd8a34b074ea 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -104,10 +104,8 @@ export default function jasmineEnv(j$: Jasmine) { let catchExceptions = true; - const realSetTimeout = - global.setTimeout as typeof globalThis['setTimeout']; - const realClearTimeout = - global.clearTimeout as typeof globalThis['clearTimeout']; + const realSetTimeout = globalThis.setTimeout; + const realClearTimeout = globalThis.clearTimeout; const runnableResources: Record}> = {}; const currentlyExecutingSuites: Array = []; diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index 5f0c78886f5f..4cb23daaaf3f 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -57,9 +57,11 @@ export const create = function (createOptions: Record): Jasmine { configurable: true, enumerable: true, get() { + // eslint-disable-next-line no-restricted-globals return global[testTimeoutSymbol] || createOptions.testTimeout || 5000; }, set(value) { + // eslint-disable-next-line no-restricted-globals global[testTimeoutSymbol] = value; }, }); diff --git a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts index 3d9e85eb9552..70107c1393af 100644 --- a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts +++ b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts @@ -230,7 +230,7 @@ export default function jasmineAsyncInstall( globalConfig: Config.GlobalConfig, global: Global.Global, ): void { - const jasmine = global.jasmine; + const jasmine = globalThis.jasmine; const mutex = throat(globalConfig.maxConcurrency); const env = jasmine.getEnv(); diff --git a/packages/jest-jasmine2/src/jestExpect.ts b/packages/jest-jasmine2/src/jestExpect.ts index b95a661206b2..9d3c2e9385a1 100644 --- a/packages/jest-jasmine2/src/jestExpect.ts +++ b/packages/jest-jasmine2/src/jestExpect.ts @@ -11,9 +11,11 @@ import {jestExpect} from '@jest/expect'; import type {JasmineMatchersObject} from './types'; export default function jestExpectAdapter(config: {expand: boolean}): void { + // eslint-disable-next-line no-restricted-globals global.expect = jestExpect; jestExpect.setState({expand: config.expand}); + // eslint-disable-next-line no-restricted-globals const jasmine = global.jasmine; jasmine.anything = jestExpect.anything; jasmine.any = jestExpect.any; diff --git a/packages/jest-jasmine2/src/setup_jest_globals.ts b/packages/jest-jasmine2/src/setup_jest_globals.ts index 0265b547c4d3..1a8cec06c940 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -63,7 +63,8 @@ const addAssertionErrors = (result: SpecResult) => { }; const patchJasmine = () => { - global.jasmine.Spec = (realSpec => { + // @ts-expect-error + globalThis.jasmine.Spec = (realSpec => { class Spec extends realSpec { constructor(attr: Attributes) { const resultCallback = attr.resultCallback; @@ -82,7 +83,8 @@ const patchJasmine = () => { } return Spec; - })(global.jasmine.Spec); + // @ts-expect-error + })(globalThis.jasmine.Spec); }; export default async function setupJestGlobals({ diff --git a/packages/jest-leak-detector/src/__tests__/index.test.ts b/packages/jest-leak-detector/src/__tests__/index.test.ts index e5833ccf3acd..795ab6bc24c5 100644 --- a/packages/jest-leak-detector/src/__tests__/index.test.ts +++ b/packages/jest-leak-detector/src/__tests__/index.test.ts @@ -9,12 +9,12 @@ import LeakDetector from '../index'; -const gc = global.gc; +const gc = globalThis.gc; // Some tests override the "gc" value. Let's make sure we roll it back to its // previous value after executing the test. afterEach(() => { - global.gc = gc; + globalThis.gc = gc; }); it('complains if the value is a primitive', () => { @@ -32,17 +32,17 @@ it('does not show the GC if hidden', async () => { const detector = new LeakDetector({}); // @ts-expect-error: purposefully removed - global.gc = undefined; + globalThis.gc = undefined; await detector.isLeaking(); - expect(global.gc).not.toBeDefined(); + expect(globalThis.gc).not.toBeDefined(); }); it('does not hide the GC if visible', async () => { const detector = new LeakDetector({}); - global.gc = () => {}; + globalThis.gc = () => {}; await detector.isLeaking(); - expect(global.gc).toBeDefined(); + expect(globalThis.gc).toBeDefined(); }); it('correctly checks simple leaks', async () => { diff --git a/packages/jest-leak-detector/src/index.ts b/packages/jest-leak-detector/src/index.ts index ded8b0381582..460ae1d64dfc 100644 --- a/packages/jest-leak-detector/src/index.ts +++ b/packages/jest-leak-detector/src/index.ts @@ -61,7 +61,8 @@ export default class LeakDetector { } private _runGarbageCollector() { - const isGarbageCollectorHidden = !global.gc; + // @ts-expect-error + const isGarbageCollectorHidden = globalThis.gc == null; // GC is usually hidden, so we have to expose it before running. setFlagsFromString('--expose-gc'); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index db143d16fe59..793943cec8eb 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -1211,7 +1211,7 @@ export class ModuleMocker { } } -const JestMock = new ModuleMocker(global as unknown as typeof globalThis); +const JestMock = new ModuleMocker(globalThis); export const fn = JestMock.fn.bind(JestMock); export const spyOn = JestMock.spyOn.bind(JestMock); diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 89f9356f8565..e734262d81cb 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -318,9 +318,9 @@ async function runTestInternal( } if (globalConfig.logHeapUsage) { - if (global.gc) { - global.gc(); - } + // @ts-expect-error + globalThis.gc?.(); + result.memoryUsage = process.memoryUsage().heapUsed; } diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 821a322137b2..8759043aeca6 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -269,7 +269,7 @@ expectType>( expectError(jest.spyOn(spiedFunction, 'toString')); expectType>( - jest.spyOn(global, 'Date'), + jest.spyOn(globalThis, 'Date'), ); expectType>(jest.spyOn(Date, 'now')); diff --git a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts index f6a332584c4d..369cfbe2b48f 100644 --- a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts +++ b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts @@ -17,7 +17,7 @@ function getGlobal(): typeof globalThis { beforeEach(() => { fake = jest.fn(); // @ts-expect-error - global.DTRACE_NET_SERVER_CONNECTION = fake; + globalThis.DTRACE_NET_SERVER_CONNECTION = fake; installCommonGlobals = require('../installCommonGlobals').default; }); diff --git a/packages/jest-util/src/installCommonGlobals.ts b/packages/jest-util/src/installCommonGlobals.ts index 617974443fcf..f84e91af4a3b 100644 --- a/packages/jest-util/src/installCommonGlobals.ts +++ b/packages/jest-util/src/installCommonGlobals.ts @@ -10,7 +10,7 @@ import type {Config} from '@jest/types'; import createProcessObject from './createProcessObject'; import deepCyclicCopy from './deepCyclicCopy'; -const DTRACE = Object.keys(global).filter(key => key.startsWith('DTRACE')); +const DTRACE = Object.keys(globalThis).filter(key => key.startsWith('DTRACE')); export default function installCommonGlobals( globalObject: typeof globalThis, @@ -58,7 +58,7 @@ export default function installCommonGlobals( // @ts-expect-error: no index globalObject[dtrace] = function (...args: Array) { // @ts-expect-error: no index - return global[dtrace].apply(this, args); + return globalThis[dtrace].apply(this, args); }; }); diff --git a/packages/jest-util/src/isPromise.ts b/packages/jest-util/src/isPromise.ts index 1006dac25651..d24c6956223a 100644 --- a/packages/jest-util/src/isPromise.ts +++ b/packages/jest-util/src/isPromise.ts @@ -5,8 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -// capture global.Promise before it may potentially be overwritten -const Promise: any = global.Promise; +// capture globalThis.Promise before it may potentially be overwritten +const Promise: any = globalThis.Promise; // see ES2015 spec 25.4.4.5, https://stackoverflow.com/a/38339199 const isPromise = (candidate: unknown): candidate is Promise => diff --git a/packages/jest-worker/src/__performance_tests__/test.js b/packages/jest-worker/src/__performance_tests__/test.js index 87bf958b670d..77bd431357a8 100644 --- a/packages/jest-worker/src/__performance_tests__/test.js +++ b/packages/jest-worker/src/__performance_tests__/test.js @@ -126,7 +126,7 @@ function profileEnd(x) { } async function main() { - if (!global.gc) { + if (!globalThis.gc) { console.warn('GC not present, start with node --expose-gc'); } @@ -140,15 +140,13 @@ async function main() { const wF = await testWorkerFarm(); profileEnd('worker farm'); await sleep(3000); - // eslint-disable-next-line no-undef - global.gc && gc(); + globalThis.gc?.(); profile('jest worker'); const jW = await testJestWorker(); profileEnd('jest worker'); await sleep(3000); - // eslint-disable-next-line no-undef - global.gc && gc(); + globalThis.gc?.(); wFResults.push(wF); jWResults.push(jW); diff --git a/packages/jest-worker/src/workers/ChildProcessWorker.ts b/packages/jest-worker/src/workers/ChildProcessWorker.ts index 8b4fa8843ead..52d0d42d1d87 100644 --- a/packages/jest-worker/src/workers/ChildProcessWorker.ts +++ b/packages/jest-worker/src/workers/ChildProcessWorker.ts @@ -173,7 +173,7 @@ export default class ChildProcessWorker implements WorkerInterface { if (error != null && typeof error === 'object') { const extra = error; // @ts-expect-error: no index - const NativeCtor = global[response[1]]; + const NativeCtor = globalThis[response[1]]; const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error; error = new Ctor(response[2]); diff --git a/packages/jest-worker/src/workers/NodeThreadsWorker.ts b/packages/jest-worker/src/workers/NodeThreadsWorker.ts index cc47b763bd77..4fe04beda556 100644 --- a/packages/jest-worker/src/workers/NodeThreadsWorker.ts +++ b/packages/jest-worker/src/workers/NodeThreadsWorker.ts @@ -140,7 +140,7 @@ export default class ExperimentalWorker implements WorkerInterface { if (error != null && typeof error === 'object') { const extra = error; // @ts-expect-error: no index - const NativeCtor = global[response[1]]; + const NativeCtor = globalThis[response[1]]; const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error; error = new Ctor(response[2]); diff --git a/scripts/babel-plugin-jest-native-globals.js b/scripts/babel-plugin-jest-native-globals.js index 32e9cceb65af..8a2e91b34427 100644 --- a/scripts/babel-plugin-jest-native-globals.js +++ b/scripts/babel-plugin-jest-native-globals.js @@ -12,100 +12,22 @@ module.exports = ({template}) => { const promiseDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var Promise = global[Symbol.for('jest-native-promise')] || global.Promise; + var Promise = globalThis[Symbol.for('jest-native-promise')] || globalThis.Promise; `); const symbolDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol; + var Symbol = globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol; `); const nowDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now; + var jestNow = globalThis[Symbol.for('jest-native-now')] || globalThis.Date.now; `); const fsReadFileDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var jestReadFile = global[Symbol.for('jest-native-read-file')] || fs.readFileSync; + var jestReadFile = globalThis[Symbol.for('jest-native-read-file')] || fs.readFileSync; `); const fsWriteFileDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var jestWriteFile = global[Symbol.for('jest-native-write-file')] || fs.writeFileSync; + var jestWriteFile = globalThis[Symbol.for('jest-native-write-file')] || fs.writeFileSync; `); const fsExistsFileDeclaration = template(` - var global = (function() { - if (typeof globalThis !== 'undefined') { - return globalThis; - } else if (typeof global !== 'undefined') { - return global; - } else if (typeof self !== 'undefined') { - return self; - } else if (typeof window !== 'undefined') { - return window; - } else { - return Function('return this')(); - } - }()) - var jestExistsFile = global[Symbol.for('jest-native-exists-file')] || fs.existsSync; + var jestExistsFile = globalThis[Symbol.for('jest-native-exists-file')] || fs.existsSync; `); return { diff --git a/scripts/babel-plugin-jest-require-outside-vm.js b/scripts/babel-plugin-jest-require-outside-vm.js index 07bd0eab714c..43faa79ba48a 100644 --- a/scripts/babel-plugin-jest-require-outside-vm.js +++ b/scripts/babel-plugin-jest-require-outside-vm.js @@ -26,7 +26,7 @@ const REQUIRE_OUTSIDE_FUNCTION_NAME = 'requireOutside'; module.exports = ({template, types: t}) => { const replacement = template(` require(require.resolve(IMPORT_PATH, { - [(global['jest-symbol-do-not-touch'] || global.Symbol).for('jest-resolve-outside-vm-option')]: true, + [(globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol).for('jest-resolve-outside-vm-option')]: true, })); `); return { From fb382a53b6c4a4ea0deab985d8c471308187d1e0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 11:35:32 +0100 Subject: [PATCH 2/5] oops --- packages/jest-jasmine2/src/jasmineAsyncInstall.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts index 70107c1393af..3d9e85eb9552 100644 --- a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts +++ b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts @@ -230,7 +230,7 @@ export default function jasmineAsyncInstall( globalConfig: Config.GlobalConfig, global: Global.Global, ): void { - const jasmine = globalThis.jasmine; + const jasmine = global.jasmine; const mutex = throat(globalConfig.maxConcurrency); const env = jasmine.getEnv(); From b45166dc314807fef06aba796612b37134562d0c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 11:48:09 +0100 Subject: [PATCH 3/5] snap --- .../__snapshots__/circusDeclarationErrors.test.ts.snap | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap index b7287b434772..dc3ae1059c3f 100644 --- a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap +++ b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap @@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 14 | }); 15 | }); - at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11) at test (__tests__/asyncDefinition.test.js:12:5) ● Test suite failed to run @@ -31,7 +31,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 15 | }); 16 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11) at afterAll (__tests__/asyncDefinition.test.js:13:5) ● Test suite failed to run @@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11) at test (__tests__/asyncDefinition.test.js:18:3) ● Test suite failed to run @@ -60,6 +60,6 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11) at afterAll (__tests__/asyncDefinition.test.js:19:3)" `; From df4a3d284ddf1bf06904db5bdf81d7e13d4e3bf8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 11:59:14 +0100 Subject: [PATCH 4/5] transform test --- e2e/__tests__/transform.test.ts | 4 +++- .../custom-instrumenting-preprocessor/preprocessor.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/transform.test.ts b/e2e/__tests__/transform.test.ts index 7580cfbce565..6569de347b9d 100644 --- a/e2e/__tests__/transform.test.ts +++ b/e2e/__tests__/transform.test.ts @@ -114,7 +114,9 @@ describe('custom transformer', () => { it('preprocesses files', () => { const {json, stderr} = runWithJson(dir, ['--no-cache']); expect(stderr).toMatch(/FAIL/); - expect(stderr).toMatch(/instruments by setting.*global\.__INSTRUMENTED__/); + expect(stderr).toMatch( + /instruments by setting.*globalThis\.__INSTRUMENTED__/, + ); expect(json.numTotalTests).toBe(2); expect(json.numPassedTests).toBe(1); expect(json.numFailedTests).toBe(1); diff --git a/e2e/transform/custom-instrumenting-preprocessor/preprocessor.js b/e2e/transform/custom-instrumenting-preprocessor/preprocessor.js index b440dd977ed6..99e12ff008e4 100644 --- a/e2e/transform/custom-instrumenting-preprocessor/preprocessor.js +++ b/e2e/transform/custom-instrumenting-preprocessor/preprocessor.js @@ -8,10 +8,10 @@ module.exports = { canInstrument: true, process(src, filename, options) { - src = `${src};\nglobal.__PREPROCESSED__ = true;`; + src = `${src};\nglobalThis.__PREPROCESSED__ = true;`; if (options.instrument) { - src = `${src};\nglobal.__INSTRUMENTED__ = true;`; + src = `${src};\nglobalThis.__INSTRUMENTED__ = true;`; } return src; From da3061a814b702c8d318d6871218d60b290e0033 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 12:28:10 +0100 Subject: [PATCH 5/5] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27efaa30ea04..c86b32a89e74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - `[*]` [**BREAKING**] Drop support for Node v10 and v15 and target first LTS `16.13.0` ([#12220](https://github.com/facebook/jest/pull/12220)) - `[*]` [**BREAKING**] Drop support for `typescript@3.8`, minimum version is now `4.2` ([#11142](https://github.com/facebook/jest/pull/11142)) - `[*]` Bundle all `.d.ts` files into a single `index.d.ts` per module ([#12345](https://github.com/facebook/jest/pull/12345)) +- `[*]` Use `globalThis` instead of `global` ([#12447](https://github.com/facebook/jest/pull/12447)) - `[docs]` Add note about not mixing `done()` with Promises ([#11077](https://github.com/facebook/jest/pull/11077)) - `[docs, examples]` Update React examples to match with the new React guidelines for code examples ([#12217](https://github.com/facebook/jest/pull/12217)) - `[expect]` [**BREAKING**] Remove support for importing `build/utils` ([#12323](https://github.com/facebook/jest/pull/12323))