UNPKG

9.25 kBJavaScriptView Raw
1var tape = require('../');
2var tap = require('tap');
3var concat = require('concat-stream');
4
5var stripFullStack = require('./common').stripFullStack;
6
7function fn() {
8 throw new TypeError('RegExp');
9}
10
11function getNonFunctionMessage(fn) {
12 try {
13 fn();
14 } catch (e) {
15 return e.message;
16 }
17}
18
19var getter = function () { return 'message'; };
20var messageGetterError = Object.defineProperty(
21 { custom: 'error' },
22 'message',
23 { configurable: true, enumerable: true, get: getter }
24);
25var thrower = function () { throw messageGetterError; };
26
27tap.test('failures', function (tt) {
28 tt.plan(1);
29
30 var test = tape.createHarness();
31 test.createStream().pipe(concat(function (body) {
32 tt.equal(
33 stripFullStack(body.toString('utf8')),
34 'TAP version 13\n'
35 + '# non functions\n'
36 + 'not ok 1 should throw\n'
37 + ' ---\n'
38 + ' operator: throws\n'
39 + ' expected: |-\n'
40 + ' undefined\n'
41 + ' actual: |-\n'
42 + " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n"
43 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
44 + ' stack: |-\n'
45 + ' TypeError: ' + getNonFunctionMessage(undefined) + '\n'
46 + ' [... stack stripped ...]\n'
47 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
48 + ' [... stack stripped ...]\n'
49 + ' ...\n'
50 + 'not ok 2 should throw\n'
51 + ' ---\n'
52 + ' operator: throws\n'
53 + ' expected: |-\n'
54 + ' undefined\n'
55 + ' actual: |-\n'
56 + " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n"
57 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
58 + ' stack: |-\n'
59 + ' TypeError: ' + getNonFunctionMessage(null) + '\n'
60 + ' [... stack stripped ...]\n'
61 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
62 + ' [... stack stripped ...]\n'
63 + ' ...\n'
64 + 'not ok 3 should throw\n'
65 + ' ---\n'
66 + ' operator: throws\n'
67 + ' expected: |-\n'
68 + ' undefined\n'
69 + ' actual: |-\n'
70 + " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n"
71 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
72 + ' stack: |-\n'
73 + ' TypeError: ' + getNonFunctionMessage(true) + '\n'
74 + ' [... stack stripped ...]\n'
75 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
76 + ' [... stack stripped ...]\n'
77 + ' ...\n'
78 + 'not ok 4 should throw\n'
79 + ' ---\n'
80 + ' operator: throws\n'
81 + ' expected: |-\n'
82 + ' undefined\n'
83 + ' actual: |-\n'
84 + " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n"
85 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
86 + ' stack: |-\n'
87 + ' TypeError: ' + getNonFunctionMessage(false) + '\n'
88 + ' [... stack stripped ...]\n'
89 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
90 + ' [... stack stripped ...]\n'
91 + ' ...\n'
92 + 'not ok 5 should throw\n'
93 + ' ---\n'
94 + ' operator: throws\n'
95 + ' expected: |-\n'
96 + ' undefined\n'
97 + ' actual: |-\n'
98 + " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n"
99 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
100 + ' stack: |-\n'
101 + ' TypeError: ' + getNonFunctionMessage('abc') + '\n'
102 + ' [... stack stripped ...]\n'
103 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
104 + ' [... stack stripped ...]\n'
105 + ' ...\n'
106 + 'not ok 6 should throw\n'
107 + ' ---\n'
108 + ' operator: throws\n'
109 + ' expected: |-\n'
110 + ' undefined\n'
111 + ' actual: |-\n'
112 + " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n"
113 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
114 + ' stack: |-\n'
115 + ' TypeError: ' + getNonFunctionMessage(/a/g) + '\n'
116 + ' [... stack stripped ...]\n'
117 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
118 + ' [... stack stripped ...]\n'
119 + ' ...\n'
120 + 'not ok 7 should throw\n'
121 + ' ---\n'
122 + ' operator: throws\n'
123 + ' expected: |-\n'
124 + ' undefined\n'
125 + ' actual: |-\n'
126 + " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n"
127 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
128 + ' stack: |-\n'
129 + ' TypeError: ' + getNonFunctionMessage([]) + '\n'
130 + ' [... stack stripped ...]\n'
131 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
132 + ' [... stack stripped ...]\n'
133 + ' ...\n'
134 + 'not ok 8 should throw\n'
135 + ' ---\n'
136 + ' operator: throws\n'
137 + ' expected: |-\n'
138 + ' undefined\n'
139 + ' actual: |-\n'
140 + " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n"
141 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
142 + ' stack: |-\n'
143 + ' TypeError: ' + getNonFunctionMessage({}) + '\n'
144 + ' [... stack stripped ...]\n'
145 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
146 + ' [... stack stripped ...]\n'
147 + ' ...\n'
148 + '# function\n'
149 + 'not ok 9 should throw\n'
150 + ' ---\n'
151 + ' operator: throws\n'
152 + ' expected: undefined\n'
153 + ' actual: undefined\n'
154 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
155 + ' stack: |-\n'
156 + ' Error: should throw\n'
157 + ' [... stack stripped ...]\n'
158 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
159 + ' [... stack stripped ...]\n'
160 + ' ...\n'
161 + '# custom error messages\n'
162 + 'ok 10 "message" is enumerable\n'
163 + "ok 11 { custom: 'error', message: 'message' }\n"
164 + 'ok 12 getter is still the same\n'
165 + '# throws null\n'
166 + 'ok 13 throws null\n'
167 + '# wrong type of error\n'
168 + 'not ok 14 throws actual\n'
169 + ' ---\n'
170 + ' operator: throws\n'
171 + ' expected: |-\n'
172 + ' [Function: TypeError]\n'
173 + ' actual: |-\n'
174 + " { [RangeError: actual!] message: 'actual!' }\n"
175 + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
176 + ' stack: |-\n'
177 + ' RangeError: actual!\n'
178 + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
179 + ' [... stack stripped ...]\n'
180 + ' ...\n'
181 + '\n1..14\n'
182 + '# tests 14\n'
183 + '# pass 4\n'
184 + '# fail 10\n'
185 );
186 }));
187
188 test('non functions', function (t) {
189 t.plan(8);
190 t.throws();
191 t.throws(null);
192 t.throws(true);
193 t.throws(false);
194 t.throws('abc');
195 t.throws(/a/g);
196 t.throws([]);
197 t.throws({});
198 });
199
200 test('function', function (t) {
201 t.plan(1);
202 t.throws(function () {});
203 });
204
205 test('custom error messages', function (t) {
206 t.plan(3);
207 t.equal(Object.prototype.propertyIsEnumerable.call(messageGetterError, 'message'), true, '"message" is enumerable');
208 t.throws(thrower, "{ custom: 'error', message: 'message' }");
209 t.equal(Object.getOwnPropertyDescriptor(messageGetterError, 'message').get, getter, 'getter is still the same');
210 });
211
212 test('throws null', function (t) {
213 t.plan(1);
214 t.throws(function () { throw null; }, 'throws null');
215 t.end();
216 });
217
218 test('wrong type of error', function (t) {
219 t.plan(1);
220 var actual = new RangeError('actual!');
221 t.throws(function () { throw actual; }, TypeError, 'throws actual');
222 t.end();
223 });
224});