diff --git a/package.json b/package.json index ddd0622..ed5e494 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "functions-have-names": "^1.2.2", "has-tostringtag": "^1.0.0", "make-arrow-function": "^1.2.0", + "mock-property": "^1.0.0", "nyc": "^10.3.2", "safe-publish-latest": "^2.0.0", "string.prototype.repeat": "^1.0.0", diff --git a/test/has.js b/test/has.js index 026d6d5..01800de 100644 --- a/test/has.js +++ b/test/has.js @@ -1,34 +1,15 @@ +'use strict'; + var inspect = require('../'); var test = require('tape'); - -function withoutProperty(object, property, fn) { - var original; - if (Object.getOwnPropertyDescriptor) { - original = Object.getOwnPropertyDescriptor(object, property); - } else { - original = object[property]; - } - delete object[property]; - try { - fn(); - } finally { - if (Object.getOwnPropertyDescriptor) { - Object.defineProperty(object, property, original); - } else { - object[property] = original; - } - } -} +var mockProperty = require('mock-property'); test('when Object#hasOwnProperty is deleted', function (t) { t.plan(1); var arr = [1, , 3]; // eslint-disable-line no-sparse-arrays - // eslint-disable-next-line no-extend-native - Array.prototype[1] = 2; // this is needed to account for "in" vs "hasOwnProperty" + t.teardown(mockProperty(Array.prototype, 1, { value: 2 })); // this is needed to account for "in" vs "hasOwnProperty" + t.teardown(mockProperty(Object.prototype, 'hasOwnProperty', { 'delete': true })); - withoutProperty(Object.prototype, 'hasOwnProperty', function () { - t.equal(inspect(arr), '[ 1, , 3 ]'); - }); - delete Array.prototype[1]; + t.equal(inspect(arr), '[ 1, , 3 ]'); }); diff --git a/test/values.js b/test/values.js index c83cb95..4832b9f 100644 --- a/test/values.js +++ b/test/values.js @@ -2,6 +2,7 @@ var inspect = require('../'); var test = require('tape'); +var mockProperty = require('mock-property'); var hasSymbols = require('has-symbols/shams')(); var hasToStringTag = require('has-tostringtag/shams')(); @@ -23,10 +24,9 @@ test('arrays with properties', function (t) { test('has', function (t) { t.plan(1); - var has = Object.prototype.hasOwnProperty; - delete Object.prototype.hasOwnProperty; + t.teardown(mockProperty(Object.prototype, 'hasOwnProperty', { 'delete': true })); + t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }'); - Object.prototype.hasOwnProperty = has; // eslint-disable-line no-extend-native }); test('indexOf seen', function (t) {