proxyquire-argumentvalidation.js
1.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*jshint asi:true*/
/*global describe, before, beforeEach, it */
"use strict";
var assert = require('assert')
, proxyquire = require('..')
;
describe('Illegal parameters to resolve give meaningful errors', function () {
var bar = { bar: function () { return 'bar'; } }
, exception
;
function throws(action, regex) {
assert.throws(action, function (err) {
return err.name === 'ProxyquireError' && regex.test(err.message) && regex.test(err.toString());
});
}
describe('when I pass no request', function () {
function act () {
proxyquire(null, {});
}
it('throws an exception explaining that a request path must be provided', function () {
throws(act, /missing argument: "request"/i);
})
})
describe('when I pass an object as a request', function () {
function act () {
proxyquire({ }, { './bar': bar });
}
it('throws an exception explaining that request needs to be a requirable string', function () {
throws(act, /invalid argument: "request".+needs to be a requirable string/i);
})
})
describe('when I pass no stubs', function () {
function act () {
proxyquire('./samples/foo');
}
it('throws an exception explaining that resolve without stubs makes no sense', function () {
throws(act, /missing argument: "stubs".+use regular require instead/i);
})
})
describe('when I pass a string as stubs', function () {
function act () {
proxyquire('./samples/foo', 'stubs');
}
it('throws an exception explaining that stubs need to be an object', function () {
throws(act, /invalid argument: "stubs".+needs to be an object/i);
})
})
describe('when I pass an undefined stub', function () {
function act () {
proxyquire('./samples/foo', {
myStub: undefined
})
}
it('throws an exception with the stub key', function () {
throws(act, /Invalid stub: "myStub" cannot be undefined/)
})
})
})