resolve.js
2.71 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var fs = require('fs');
var path = require('path');
var mdeps = require('../');
var test = require('tap').test;
var nodeResolve = require('resolve');
var browserResolve = require('browser-resolve');
var missing = path.join(__dirname, '/missing');
var files = {
foo: path.join(__dirname, '/files/resolve/foo/foo.js'),
bar: path.join(__dirname, '/files/resolve/bar/bar.js'),
bar2: path.join(__dirname, '/files/resolve/bar/bar2.js'),
baz: path.join(__dirname, '/files/resolve/foo/baz/baz.js')
};
var sources = Object.keys(files)
.reduce(function (acc, file) {
acc[file] = fs.readFileSync(files[file], 'utf8');
return acc;
}, {});
var expectedRows = [
{
"deps": {},
"file": files.baz,
"id": files.baz,
"source": sources.baz
},
{
"deps": {},
"file": files.bar2,
"id": files.bar2,
"source": sources.bar2
},
{
"deps": {
"./bar2": files.bar2
},
"file": files.bar,
"id": files.bar,
"source": sources.bar
},
{
"deps": {
"../bar/bar.js": files.bar,
"./baz/baz.js": files.baz
},
"entry": true,
"file": files.foo,
"id": "foo",
"source": sources.foo
}
];
test('browser resolve - missing', function (t) {
t.plan(1);
var d = mdeps({resolve: browserResolve});
d.end({id: 'missing', file: missing, entry: true});
d.on('end', function () {
t.fail('errored');
});
d.on('error', function (err) {
t.match(
String(err),
/Cannot find module .*/
);
});
});
test('node resolve - missing', function (t) {
t.plan(1);
var d = mdeps({resolve: nodeResolve});
d.end({id: 'missing', file: missing, entry: true});
d.on('end', function () {
t.fail('errored');
});
d.on('error', function (err) {
t.match(
String(err),
/Cannot find module .*/
);
});
});
test('browser resolve', function (t) {
t.plan(1);
var d = mdeps({resolve: browserResolve});
d.end({id: 'foo', file: files.foo, entry: true});
var rows = [];
d.on('data', function (row) {rows.push(row)});
d.on('end', function () {
t.same(rows, expectedRows);
});
d.on('error', function () {
t.fail('errored');
});
});
test('node resolve', function (t) {
t.plan(1);
var d = mdeps({resolve: nodeResolve});
d.end({id: 'foo', file: files.foo, entry: true});
var rows = [];
d.on('data', function (row) {rows.push(row)});
d.on('end', function () {
t.same(rows, expectedRows);
});
d.on('error', function () {
t.fail('errored');
});
});