Skip to content

Commit

Permalink
[Tests] use mock-property
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 16, 2022
1 parent b05244b commit 4ec8893
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 6 additions & 25 deletions test/has.js
Original file line number Diff line number Diff line change
@@ -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 ]');
});
6 changes: 3 additions & 3 deletions test/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();

Expand All @@ -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) {
Expand Down

0 comments on commit 4ec8893

Please sign in to comment.