s-error.js
1.7 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
/* global describe, it, xit, expect */
describe('Error', function () {
'use strict';
var supportsDescriptors = Object.defineProperty && (function () {
try {
var obj = {};
Object.defineProperty(obj, 'x', { enumerable: false, value: obj });
for (var _ in obj) { return false; }
return obj.x === obj;
} catch (e) { /* this is ES3 */
return false;
}
}());
var ifSupportsDescriptorsIt = supportsDescriptors ? it : xit;
describe('#toString()', function () {
it('stringifies a newed error properly', function () {
var msg = 'test';
var error = new RangeError(msg);
expect(error.name).toBe('RangeError');
expect(error.message).toBe(msg);
expect(String(error)).toBe(error.name + ': ' + msg);
});
it('stringifies a thrown error properly', function () {
var msg = 'test';
var error;
try {
throw new RangeError(msg);
} catch (e) {
error = e;
}
expect(error.name).toBe('RangeError');
expect(error.message).toBe(msg);
expect(String(error)).toBe(error.name + ': ' + msg);
});
});
describe('enumerability of prototype properties', function () {
ifSupportsDescriptorsIt('#message', function () {
expect(Object.prototype.propertyIsEnumerable.call(Error.prototype, 'message')).toBe(false);
});
ifSupportsDescriptorsIt('#name', function () {
expect(Object.prototype.propertyIsEnumerable.call(Error.prototype, 'name')).toBe(false);
});
});
});