-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis-data-descriptor-test.js
More file actions
60 lines (49 loc) · 1.99 KB
/
is-data-descriptor-test.js
File metadata and controls
60 lines (49 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const assert = require('node:assert/strict');
(async () => {
const isData = require('is-data-descriptor');
assert.equal(typeof isData, 'function');
// A data descriptor with value should be recognized
const dataDesc = {
value: 'hello',
writable: true,
enumerable: true,
configurable: true
};
assert.equal(isData(dataDesc), true, '{value, writable} is a data descriptor');
// A value-only descriptor is still a data descriptor
const valueOnly = { value: 42 };
assert.equal(isData(valueOnly), true, '{value} alone is a data descriptor');
// value: undefined is still a data descriptor
const undefinedValue = { value: undefined, writable: true };
assert.equal(isData(undefinedValue), true, '{value: undefined, writable} is a data descriptor');
// An accessor descriptor should NOT be a data descriptor
const accessorDesc = {
get: function() { return 'val'; },
set: function(v) {},
enumerable: true,
configurable: true
};
assert.equal(isData(accessorDesc), false, '{get, set} is NOT a data descriptor');
// A plain empty object is not a data descriptor
assert.equal(isData({}), false, 'empty object is not a data descriptor');
// Non-objects should return false
assert.equal(isData(null), false, 'null is not a data descriptor');
assert.equal(isData('string'), false, 'string is not a data descriptor');
assert.equal(isData(123), false, 'number is not a data descriptor');
// Test with a real data descriptor from defineProperty
const obj = {};
Object.defineProperty(obj, 'y', {
value: 99,
writable: false,
enumerable: true,
configurable: false
});
const realDesc = Object.getOwnPropertyDescriptor(obj, 'y');
assert.equal(isData(realDesc), true, 'real data descriptor from Object.getOwnPropertyDescriptor');
console.log('is-data-descriptor-test:ok');
})().catch((err) => {
console.error('is-data-descriptor-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});