diff --git a/.eslintrc.js b/.eslintrc.js index a8a373d154d3..a6e843aef4e5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -428,6 +428,7 @@ module.exports = { 'no-process-env': 'off', 'no-process-exit': 'off', 'no-proto': 'error', + 'no-prototype-builtins': 'error', 'no-redeclare': 'warn', 'no-regex-spaces': 'warn', 'no-restricted-imports': [ diff --git a/docs/Configuration.md b/docs/Configuration.md index 29d1fdc2c61a..ec927f3dbf9d 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1018,7 +1018,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/e2e/snapshot-serializers/__tests__/snapshot.test.js b/e2e/snapshot-serializers/__tests__/snapshot.test.js index 093024192c62..e6aaf4e79632 100644 --- a/e2e/snapshot-serializers/__tests__/snapshot.test.js +++ b/e2e/snapshot-serializers/__tests__/snapshot.test.js @@ -69,7 +69,7 @@ describe('snapshot serializers', () => { // Add plugin that overrides foo specified by Jest config in package.json expect.addSnapshotSerializer({ print: (val, serialize) => `Foo: ${serialize(val.foo)}`, - test: val => val && val.hasOwnProperty('foo'), + test: val => val && Object.prototype.hasOwnProperty.call(val, 'foo'), }); expect(test).toMatchSnapshot(); }); @@ -87,7 +87,7 @@ describe('snapshot serializers', () => { // Add plugin that overrides preceding added plugin expect.addSnapshotSerializer({ print: (val, serialize) => `FOO: ${serialize(val.foo)}`, - test: val => val && val.hasOwnProperty('foo'), + test: val => val && Object.prototype.hasOwnProperty.call(val, 'foo'), }); expect(test).toMatchSnapshot(); }); diff --git a/e2e/snapshot-serializers/utils.js b/e2e/snapshot-serializers/utils.js index af5a97e85cd2..8f016089498f 100644 --- a/e2e/snapshot-serializers/utils.js +++ b/e2e/snapshot-serializers/utils.js @@ -8,5 +8,5 @@ exports.createPlugin = prop => ({ print: (val, serialize) => `${prop} - ${serialize(val[prop])}`, - test: val => val && val.hasOwnProperty(prop), + test: val => val && Object.prototype.hasOwnProperty.call(val, prop), }); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index 758909b40909..16175ca70c72 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -22,7 +22,7 @@ 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 (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) { +if (!Object.prototype.hasOwnProperty.call(global, JEST_MATCHERS_OBJECT)) { const defaultState: Partial = { assertionCalls: 0, expectedAssertionsNumber: null, diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 1cc5440d74c9..055471d52bff 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -9,7 +9,10 @@ import type {Config} from '@jest/types'; import {constants, isJSONString} from 'jest-config'; export function check(argv: Config.Argv): true { - if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) { + if ( + argv.runInBand && + Object.prototype.hasOwnProperty.call(argv, 'maxWorkers') + ) { throw new Error( 'Both --runInBand and --maxWorkers were specified, but these two ' + 'options do not make sense together. Which is it?', @@ -46,7 +49,10 @@ export function check(argv: Config.Argv): true { ); } - if (argv.hasOwnProperty('maxWorkers') && argv.maxWorkers === undefined) { + if ( + Object.prototype.hasOwnProperty.call(argv, 'maxWorkers') && + argv.maxWorkers === undefined + ) { throw new Error( 'The --maxWorkers (-w) option requires a number or string to be specified.\n' + 'Example usage: jest --maxWorkers 2\n' + diff --git a/packages/jest-each/src/table/interpolation.ts b/packages/jest-each/src/table/interpolation.ts index b2b5160dcd28..cee7c5a93567 100644 --- a/packages/jest-each/src/table/interpolation.ts +++ b/packages/jest-each/src/table/interpolation.ts @@ -77,7 +77,7 @@ export function getPath( template: Template, [head, ...tail]: Array, ): unknown { - if (!head || !template.hasOwnProperty || !template.hasOwnProperty(head)) + if (!head || !Object.prototype.hasOwnProperty.call(template, head)) return template; return getPath(template[head] as Template, tail); } diff --git a/packages/jest-fake-timers/src/legacyFakeTimers.ts b/packages/jest-fake-timers/src/legacyFakeTimers.ts index 8d07c01920bf..46dce1ae005d 100644 --- a/packages/jest-fake-timers/src/legacyFakeTimers.ts +++ b/packages/jest-fake-timers/src/legacyFakeTimers.ts @@ -137,7 +137,9 @@ export default class FakeTimers { break; } - if (!this._cancelledTicks.hasOwnProperty(tick.uuid)) { + if ( + !Object.prototype.hasOwnProperty.call(this._cancelledTicks, tick.uuid) + ) { // Callback may throw, so update the map prior calling. this._cancelledTicks[tick.uuid] = true; tick.callback(); @@ -472,7 +474,7 @@ export default class FakeTimers { const cancelledTicks = this._cancelledTicks; this._timerAPIs.nextTick(() => { - if (!cancelledTicks.hasOwnProperty(uuid)) { + if (!Object.prototype.hasOwnProperty.call(cancelledTicks, uuid)) { // Callback may throw, so update the map prior calling. cancelledTicks[uuid] = true; callback.apply(null, args); diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index e424fc14610c..bbfc2677d2b9 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -553,7 +553,7 @@ describe('moduleMocker', () => { moduleMocker.spyOn(child, 'func').mockReturnValue('efgh'); - expect(child.hasOwnProperty('func')).toBe(true); + expect(Object.prototype.hasOwnProperty.call(child, 'func')).toBe(true); expect(child.func()).toEqual('efgh'); expect(parent.func()).toEqual('abcd'); }); @@ -569,7 +569,7 @@ describe('moduleMocker', () => { moduleMocker.spyOn(parent, 'func').mockReturnValue('jklm'); - expect(child.hasOwnProperty('func')).toBe(false); + expect(Object.prototype.hasOwnProperty.call(child, 'func')).toBe(false); expect(child.func()).toEqual('jklm'); }); diff --git a/packages/jest-util/src/__tests__/createProcessObject.test.ts b/packages/jest-util/src/__tests__/createProcessObject.test.ts index 670e4f66b386..20ad012fc31b 100644 --- a/packages/jest-util/src/__tests__/createProcessObject.test.ts +++ b/packages/jest-util/src/__tests__/createProcessObject.test.ts @@ -102,6 +102,6 @@ it('checks that process.env works as expected in Windows platforms', () => { // You can delete through case-insensitiveness too. delete fake.prop_string; - expect(fake.hasOwnProperty('PROP_STRING')).toBe(false); - expect(fake.hasOwnProperty('PROP_string')).toBe(false); + expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false); + expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false); }); diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 32661b47d44a..049db7ebbdc2 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -22,7 +22,7 @@ function createProcessEnv(): NodeJS.ProcessEnv { function deletePropertyWin32(_target: unknown, key: unknown) { for (const name in real) { - if (real.hasOwnProperty(name)) { + if (Object.prototype.hasOwnProperty.call(real, name)) { if (typeof key === 'string') { if (name.toLowerCase() === key.toLowerCase()) { delete real[name]; diff --git a/packages/jest-worker/src/index.ts b/packages/jest-worker/src/index.ts index caf909265a8d..b8cbdd2c17a2 100644 --- a/packages/jest-worker/src/index.ts +++ b/packages/jest-worker/src/index.ts @@ -124,6 +124,7 @@ export class Worker { return; } + // eslint-disable-next-line no-prototype-builtins if (this.constructor.prototype.hasOwnProperty(name)) { throw new TypeError('Cannot define a method called ' + name); } diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 3d9d4ce90564..10165c7392b0 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -414,7 +414,7 @@ export const DEFAULT_OPTIONS: Options = { function validateOptions(options: OptionsReceived) { Object.keys(options).forEach(key => { - if (!DEFAULT_OPTIONS.hasOwnProperty(key)) { + if (!Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, key)) { throw new Error(`pretty-format: Unknown option "${key}".`); } }); diff --git a/website/versioned_docs/version-25.x/Configuration.md b/website/versioned_docs/version-25.x/Configuration.md index 773ba0963d8d..04a0f58d8efd 100644 --- a/website/versioned_docs/version-25.x/Configuration.md +++ b/website/versioned_docs/version-25.x/Configuration.md @@ -864,7 +864,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-26.x/Configuration.md b/website/versioned_docs/version-26.x/Configuration.md index fc4fdb04db63..b4b98bab1422 100644 --- a/website/versioned_docs/version-26.x/Configuration.md +++ b/website/versioned_docs/version-26.x/Configuration.md @@ -950,7 +950,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-27.0/Configuration.md b/website/versioned_docs/version-27.0/Configuration.md index d8dd15dc88db..c26c71d80c78 100644 --- a/website/versioned_docs/version-27.0/Configuration.md +++ b/website/versioned_docs/version-27.0/Configuration.md @@ -977,7 +977,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-27.1/Configuration.md b/website/versioned_docs/version-27.1/Configuration.md index d5e823f5d1ef..98758f595a94 100644 --- a/website/versioned_docs/version-27.1/Configuration.md +++ b/website/versioned_docs/version-27.1/Configuration.md @@ -1012,7 +1012,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-27.2/Configuration.md b/website/versioned_docs/version-27.2/Configuration.md index 1a96fdb361b4..9a3164b6dfaa 100644 --- a/website/versioned_docs/version-27.2/Configuration.md +++ b/website/versioned_docs/version-27.2/Configuration.md @@ -1016,7 +1016,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-27.4/Configuration.md b/website/versioned_docs/version-27.4/Configuration.md index 337f2061eaf3..61382b6cfbb6 100644 --- a/website/versioned_docs/version-27.4/Configuration.md +++ b/website/versioned_docs/version-27.4/Configuration.md @@ -1018,7 +1018,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ``` diff --git a/website/versioned_docs/version-27.5/Configuration.md b/website/versioned_docs/version-27.5/Configuration.md index 337f2061eaf3..61382b6cfbb6 100644 --- a/website/versioned_docs/version-27.5/Configuration.md +++ b/website/versioned_docs/version-27.5/Configuration.md @@ -1018,7 +1018,7 @@ module.exports = { }, test(val) { - return val && val.hasOwnProperty('foo'); + return val && Object.prototype.hasOwnProperty.call(val, 'foo'); }, }; ```