kareem 2.4.1 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/post.test.js DELETED
@@ -1,264 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const Kareem = require('../');
5
- const { beforeEach, describe, it } = require('mocha');
6
-
7
- describe('execPost', function() {
8
- var hooks;
9
-
10
- beforeEach(function() {
11
- hooks = new Kareem();
12
- });
13
-
14
- it('handles errors', function(done) {
15
- hooks.post('cook', function(eggs, callback) {
16
- callback('error!');
17
- });
18
-
19
- hooks.execPost('cook', null, [4], function(error, eggs) {
20
- assert.equal('error!', error);
21
- assert.ok(!eggs);
22
- done();
23
- });
24
- });
25
-
26
- it('unshift', function() {
27
- var f1 = function() {};
28
- var f2 = function() {};
29
- hooks.post('cook', f1);
30
- hooks.post('cook', f2, true);
31
- assert.strictEqual(hooks._posts.get('cook')[0].fn, f2);
32
- assert.strictEqual(hooks._posts.get('cook')[1].fn, f1);
33
- });
34
-
35
- it('arbitrary options', function() {
36
- const f1 = function() {};
37
- const f2 = function() {};
38
- hooks.post('cook', { foo: 'bar' }, f1);
39
- hooks.post('cook', { bar: 'baz' }, f2, true);
40
- assert.equal(hooks._posts.get('cook')[1].foo, 'bar');
41
- assert.equal(hooks._posts.get('cook')[0].bar, 'baz');
42
- });
43
-
44
- it('throws error if no function', function() {
45
- assert.throws(() => hooks.post('test'), /got "undefined"/);
46
- });
47
-
48
- it('multiple posts', function(done) {
49
- hooks.post('cook', function(eggs, callback) {
50
- setTimeout(
51
- function() {
52
- callback();
53
- },
54
- 5);
55
- });
56
-
57
- hooks.post('cook', function(eggs, callback) {
58
- setTimeout(
59
- function() {
60
- callback();
61
- },
62
- 5);
63
- });
64
-
65
- hooks.execPost('cook', null, [4], function(error, eggs) {
66
- assert.ifError(error);
67
- assert.equal(4, eggs);
68
- done();
69
- });
70
- });
71
-
72
- it('error posts', function(done) {
73
- var called = {};
74
- hooks.post('cook', function(eggs, callback) {
75
- called.first = true;
76
- callback();
77
- });
78
-
79
- hooks.post('cook', function(eggs, callback) {
80
- called.second = true;
81
- callback(new Error('fail'));
82
- });
83
-
84
- hooks.post('cook', function(eggs, callback) {
85
- assert.ok(false);
86
- });
87
-
88
- hooks.post('cook', function(error, eggs, callback) {
89
- called.fourth = true;
90
- assert.equal(error.message, 'fail');
91
- callback(new Error('fourth'));
92
- });
93
-
94
- hooks.post('cook', function(error, eggs, callback) {
95
- called.fifth = true;
96
- assert.equal(error.message, 'fourth');
97
- callback(new Error('fifth'));
98
- });
99
-
100
- hooks.execPost('cook', null, [4], function(error, eggs) {
101
- assert.ok(error);
102
- assert.equal(error.message, 'fifth');
103
- assert.deepEqual(called, {
104
- first: true,
105
- second: true,
106
- fourth: true,
107
- fifth: true
108
- });
109
- done();
110
- });
111
- });
112
-
113
- it('error posts with initial error', function(done) {
114
- var called = {};
115
-
116
- hooks.post('cook', function(eggs, callback) {
117
- assert.ok(false);
118
- });
119
-
120
- hooks.post('cook', function(error, eggs, callback) {
121
- called.second = true;
122
- assert.equal(error.message, 'fail');
123
- callback(new Error('second'));
124
- });
125
-
126
- hooks.post('cook', function(error, eggs, callback) {
127
- called.third = true;
128
- assert.equal(error.message, 'second');
129
- callback(new Error('third'));
130
- });
131
-
132
- hooks.post('cook', function(error, eggs, callback) {
133
- called.fourth = true;
134
- assert.equal(error.message, 'third');
135
- callback();
136
- });
137
-
138
- var options = { error: new Error('fail') };
139
- hooks.execPost('cook', null, [4], options, function(error, eggs) {
140
- assert.ok(error);
141
- assert.equal(error.message, 'third');
142
- assert.deepEqual(called, {
143
- second: true,
144
- third: true,
145
- fourth: true
146
- });
147
- done();
148
- });
149
- });
150
-
151
- it('supports returning a promise', function(done) {
152
- var calledPost = 0;
153
-
154
- hooks.post('cook', function() {
155
- return new Promise(resolve => {
156
- setTimeout(() => {
157
- ++calledPost;
158
- resolve();
159
- }, 100);
160
- });
161
- });
162
-
163
- hooks.execPost('cook', null, [], {}, function(error) {
164
- assert.ifError(error);
165
- assert.equal(calledPost, 1);
166
- done();
167
- });
168
- });
169
-
170
- it('supports overwriteResult', function(done) {
171
- hooks.post('cook', function(eggs, callback) {
172
- callback(Kareem.overwriteResult(5));
173
- });
174
-
175
- hooks.post('cook', function(eggs, callback) {
176
- assert.equal(eggs, 5);
177
- callback();
178
- });
179
-
180
- var options = {};
181
- hooks.execPost('cook', null, [4], options, function(error, eggs) {
182
- assert.equal(eggs, 5);
183
- done();
184
- });
185
- });
186
-
187
- it('supports sync returning overwriteResult', function(done) {
188
- hooks.post('cook', function() {
189
- return Kareem.overwriteResult(5);
190
- });
191
-
192
- hooks.post('cook', function(eggs, callback) {
193
- assert.equal(eggs, 5);
194
- callback();
195
- });
196
-
197
- var options = {};
198
- hooks.execPost('cook', null, [4], options, function(error, eggs) {
199
- assert.ifError(error);
200
- assert.equal(eggs, 5);
201
- done();
202
- });
203
- });
204
-
205
- it('supports sync overwriteResult', function() {
206
- hooks.post('cook', function(eggs) {
207
- return Kareem.overwriteResult(5);
208
- });
209
-
210
- hooks.post('cook', function(eggs) {
211
- assert.equal(eggs, 5);
212
- });
213
-
214
- var options = {};
215
- const res = hooks.execPostSync('cook', null, [4], options);
216
- assert.deepEqual(res, [5]);
217
- });
218
-
219
- it('supports overwriteResult with promises', function(done) {
220
- hooks.post('cook', function(eggs) {
221
- return Promise.resolve(Kareem.overwriteResult(5));
222
- });
223
-
224
- hooks.post('cook', function(eggs) {
225
- assert.equal(eggs, 5);
226
- });
227
-
228
- var options = {};
229
- hooks.execPost('cook', null, [4], options, function(error, eggs) {
230
- assert.equal(eggs, 5);
231
- done();
232
- });
233
- });
234
- });
235
-
236
- describe('execPostSync', function() {
237
- var hooks;
238
-
239
- beforeEach(function() {
240
- hooks = new Kareem();
241
- });
242
-
243
- it('executes hooks synchronously', function() {
244
- var execed = {};
245
-
246
- hooks.post('cook', function() {
247
- execed.first = true;
248
- });
249
-
250
- hooks.post('cook', function() {
251
- execed.second = true;
252
- });
253
-
254
- hooks.execPostSync('cook', null);
255
- assert.ok(execed.first);
256
- assert.ok(execed.second);
257
- });
258
-
259
- it('works with no hooks specified', function() {
260
- assert.doesNotThrow(function() {
261
- hooks.execPostSync('cook', null);
262
- });
263
- });
264
- });
package/test/pre.test.js DELETED
@@ -1,359 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const Kareem = require('../');
5
- const { beforeEach, describe, it } = require('mocha');
6
-
7
- describe('execPre', function() {
8
- var hooks;
9
-
10
- beforeEach(function() {
11
- hooks = new Kareem();
12
- });
13
-
14
- it('handles errors with multiple pres', function(done) {
15
- var execed = {};
16
-
17
- hooks.pre('cook', function(done) {
18
- execed.first = true;
19
- done();
20
- });
21
-
22
- hooks.pre('cook', function(done) {
23
- execed.second = true;
24
- done('error!');
25
- });
26
-
27
- hooks.pre('cook', function(done) {
28
- execed.third = true;
29
- done();
30
- });
31
-
32
- hooks.execPre('cook', null, function(err) {
33
- assert.equal('error!', err);
34
- assert.equal(2, Object.keys(execed).length);
35
- assert.ok(execed.first);
36
- assert.ok(execed.second);
37
- done();
38
- });
39
- });
40
-
41
- it('sync errors', function(done) {
42
- var called = 0;
43
-
44
- hooks.pre('cook', function(next) {
45
- throw new Error('woops!');
46
- });
47
-
48
- hooks.pre('cook', function(next) {
49
- ++called;
50
- next();
51
- });
52
-
53
- hooks.execPre('cook', null, function(err) {
54
- assert.equal(err.message, 'woops!');
55
- assert.equal(called, 0);
56
- done();
57
- });
58
- });
59
-
60
- it('unshift', function() {
61
- var f1 = function() {};
62
- var f2 = function() {};
63
- hooks.pre('cook', false, f1);
64
- hooks.pre('cook', false, f2, null, true);
65
- assert.strictEqual(hooks._pres.get('cook')[0].fn, f2);
66
- assert.strictEqual(hooks._pres.get('cook')[1].fn, f1);
67
- });
68
-
69
- it('throws error if no function', function() {
70
- assert.throws(() => hooks.pre('test'), /got "undefined"/);
71
- });
72
-
73
- it('arbitrary options', function() {
74
- const f1 = function() {};
75
- const f2 = function() {};
76
- hooks.pre('cook', { foo: 'bar' }, f1);
77
- hooks.pre('cook', { bar: 'baz' }, f2, null, true);
78
- assert.equal(hooks._pres.get('cook')[1].foo, 'bar');
79
- assert.equal(hooks._pres.get('cook')[0].bar, 'baz');
80
- });
81
-
82
- it('handles async errors', function(done) {
83
- var execed = {};
84
-
85
- hooks.pre('cook', true, function(next, done) {
86
- execed.first = true;
87
- setTimeout(
88
- function() {
89
- done('error!');
90
- },
91
- 5);
92
-
93
- next();
94
- });
95
-
96
- hooks.pre('cook', true, function(next, done) {
97
- execed.second = true;
98
- setTimeout(
99
- function() {
100
- done('other error!');
101
- },
102
- 10);
103
-
104
- next();
105
- });
106
-
107
- hooks.execPre('cook', null, function(err) {
108
- assert.equal('error!', err);
109
- assert.equal(2, Object.keys(execed).length);
110
- assert.ok(execed.first);
111
- assert.ok(execed.second);
112
- done();
113
- });
114
- });
115
-
116
- it('handles async errors in next()', function(done) {
117
- var execed = {};
118
-
119
- hooks.pre('cook', true, function(next, done) {
120
- execed.first = true;
121
- setTimeout(
122
- function() {
123
- done('other error!');
124
- },
125
- 15);
126
-
127
- next();
128
- });
129
-
130
- hooks.pre('cook', true, function(next, done) {
131
- execed.second = true;
132
- setTimeout(
133
- function() {
134
- next('error!');
135
- done('another error!');
136
- },
137
- 5);
138
- });
139
-
140
- hooks.execPre('cook', null, function(err) {
141
- assert.equal('error!', err);
142
- assert.equal(2, Object.keys(execed).length);
143
- assert.ok(execed.first);
144
- assert.ok(execed.second);
145
- done();
146
- });
147
- });
148
-
149
- it('handles async errors in next() when already done', function(done) {
150
- var execed = {};
151
-
152
- hooks.pre('cook', true, function(next, done) {
153
- execed.first = true;
154
- setTimeout(
155
- function() {
156
- done('other error!');
157
- },
158
- 5);
159
-
160
- next();
161
- });
162
-
163
- hooks.pre('cook', true, function(next, done) {
164
- execed.second = true;
165
- setTimeout(
166
- function() {
167
- next('error!');
168
- done('another error!');
169
- },
170
- 25);
171
- });
172
-
173
- hooks.execPre('cook', null, function(err) {
174
- assert.equal('other error!', err);
175
- assert.equal(2, Object.keys(execed).length);
176
- assert.ok(execed.first);
177
- assert.ok(execed.second);
178
- done();
179
- });
180
- });
181
-
182
- it('async pres with clone()', function(done) {
183
- var execed = false;
184
-
185
- hooks.pre('cook', true, function(next, done) {
186
- execed = true;
187
- setTimeout(
188
- function() {
189
- done();
190
- },
191
- 5);
192
-
193
- next();
194
- });
195
-
196
- hooks.clone().execPre('cook', null, function(err) {
197
- assert.ifError(err);
198
- assert.ok(execed);
199
- done();
200
- });
201
- });
202
-
203
- it('returns correct error when async pre errors', function(done) {
204
- var execed = {};
205
-
206
- hooks.pre('cook', true, function(next, done) {
207
- execed.first = true;
208
- setTimeout(
209
- function() {
210
- done('other error!');
211
- },
212
- 5);
213
-
214
- next();
215
- });
216
-
217
- hooks.pre('cook', function(next) {
218
- execed.second = true;
219
- setTimeout(
220
- function() {
221
- next('error!');
222
- },
223
- 15);
224
- });
225
-
226
- hooks.execPre('cook', null, function(err) {
227
- assert.equal('other error!', err);
228
- assert.equal(2, Object.keys(execed).length);
229
- assert.ok(execed.first);
230
- assert.ok(execed.second);
231
- done();
232
- });
233
- });
234
-
235
- it('lets async pres run when fully sync pres are done', function(done) {
236
- var execed = {};
237
-
238
- hooks.pre('cook', true, function(next, done) {
239
- execed.first = true;
240
- setTimeout(
241
- function() {
242
- done();
243
- },
244
- 5);
245
-
246
- next();
247
- });
248
-
249
- hooks.pre('cook', function() {
250
- execed.second = true;
251
- });
252
-
253
- hooks.execPre('cook', null, function(err) {
254
- assert.ifError(err);
255
- assert.equal(2, Object.keys(execed).length);
256
- assert.ok(execed.first);
257
- assert.ok(execed.second);
258
- done();
259
- });
260
- });
261
-
262
- it('allows passing arguments to the next pre', function(done) {
263
- var execed = {};
264
-
265
- hooks.pre('cook', function(next) {
266
- execed.first = true;
267
- next(null, 'test');
268
- });
269
-
270
- hooks.pre('cook', function(next, p) {
271
- execed.second = true;
272
- assert.equal(p, 'test');
273
- next();
274
- });
275
-
276
- hooks.pre('cook', function(next, p) {
277
- execed.third = true;
278
- assert.ok(!p);
279
- next();
280
- });
281
-
282
- hooks.execPre('cook', null, function(err) {
283
- assert.ifError(err);
284
- assert.equal(3, Object.keys(execed).length);
285
- assert.ok(execed.first);
286
- assert.ok(execed.second);
287
- assert.ok(execed.third);
288
- done();
289
- });
290
- });
291
-
292
- it('handles sync errors in pre if there are more hooks', function(done) {
293
- var execed = {};
294
-
295
- hooks.pre('cook', function() {
296
- execed.first = true;
297
- throw new Error('Oops!');
298
- });
299
-
300
- hooks.pre('cook', function() {
301
- execed.second = true;
302
- });
303
-
304
- hooks.execPre('cook', null, function(err) {
305
- assert.ok(err);
306
- assert.ok(execed.first);
307
- assert.equal(err.message, 'Oops!');
308
- done();
309
- });
310
- });
311
-
312
- it('supports skipWrappedFunction', function(done) {
313
- var execed = {};
314
-
315
- hooks.pre('cook', function(callback) {
316
- callback(Kareem.skipWrappedFunction(42));
317
- });
318
-
319
- hooks.pre('cook', function() {
320
- execed.second = true;
321
- });
322
-
323
- hooks.execPre('cook', null, function(err) {
324
- assert.ok(execed.second);
325
- assert.ok(err instanceof Kareem.skipWrappedFunction);
326
- done();
327
- });
328
- });
329
- });
330
-
331
- describe('execPreSync', function() {
332
- var hooks;
333
-
334
- beforeEach(function() {
335
- hooks = new Kareem();
336
- });
337
-
338
- it('executes hooks synchronously', function() {
339
- var execed = {};
340
-
341
- hooks.pre('cook', function() {
342
- execed.first = true;
343
- });
344
-
345
- hooks.pre('cook', function() {
346
- execed.second = true;
347
- });
348
-
349
- hooks.execPreSync('cook', null);
350
- assert.ok(execed.first);
351
- assert.ok(execed.second);
352
- });
353
-
354
- it('works with no hooks specified', function() {
355
- assert.doesNotThrow(function() {
356
- hooks.execPreSync('cook', null);
357
- });
358
- });
359
- });