UNPKG

5.32 kBJavaScriptView Raw
1var path = require('path');
2var test = require('tape');
3var resolve = require('../');
4
5test('mock', function (t) {
6 t.plan(8);
7
8 var files = {};
9 files[path.resolve('/foo/bar/baz.js')] = 'beep';
10
11 var dirs = {};
12 dirs[path.resolve('/foo/bar')] = true;
13
14 function opts(basedir) {
15 return {
16 basedir: path.resolve(basedir),
17 isFile: function (file, cb) {
18 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
19 },
20 isDirectory: function (dir, cb) {
21 cb(null, !!dirs[path.resolve(dir)]);
22 },
23 readFile: function (file, cb) {
24 cb(null, files[path.resolve(file)]);
25 }
26 };
27 }
28
29 resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
30 if (err) return t.fail(err);
31 t.equal(res, path.resolve('/foo/bar/baz.js'));
32 t.equal(pkg, undefined);
33 });
34
35 resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
36 if (err) return t.fail(err);
37 t.equal(res, path.resolve('/foo/bar/baz.js'));
38 t.equal(pkg, undefined);
39 });
40
41 resolve('baz', opts('/foo/bar'), function (err, res) {
42 t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
43 t.equal(err.code, 'MODULE_NOT_FOUND');
44 });
45
46 resolve('../baz', opts('/foo/bar'), function (err, res) {
47 t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
48 t.equal(err.code, 'MODULE_NOT_FOUND');
49 });
50});
51
52test('mock from package', function (t) {
53 t.plan(8);
54
55 var files = {};
56 files[path.resolve('/foo/bar/baz.js')] = 'beep';
57
58 var dirs = {};
59 dirs[path.resolve('/foo/bar')] = true;
60
61 function opts(basedir) {
62 return {
63 basedir: path.resolve(basedir),
64 isFile: function (file, cb) {
65 cb(null, Object.prototype.hasOwnProperty.call(files, file));
66 },
67 isDirectory: function (dir, cb) {
68 cb(null, !!dirs[path.resolve(dir)]);
69 },
70 'package': { main: 'bar' },
71 readFile: function (file, cb) {
72 cb(null, files[file]);
73 }
74 };
75 }
76
77 resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
78 if (err) return t.fail(err);
79 t.equal(res, path.resolve('/foo/bar/baz.js'));
80 t.equal(pkg && pkg.main, 'bar');
81 });
82
83 resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
84 if (err) return t.fail(err);
85 t.equal(res, path.resolve('/foo/bar/baz.js'));
86 t.equal(pkg && pkg.main, 'bar');
87 });
88
89 resolve('baz', opts('/foo/bar'), function (err, res) {
90 t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
91 t.equal(err.code, 'MODULE_NOT_FOUND');
92 });
93
94 resolve('../baz', opts('/foo/bar'), function (err, res) {
95 t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
96 t.equal(err.code, 'MODULE_NOT_FOUND');
97 });
98});
99
100test('mock package', function (t) {
101 t.plan(2);
102
103 var files = {};
104 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
105 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
106 main: './baz.js'
107 });
108
109 var dirs = {};
110 dirs[path.resolve('/foo')] = true;
111 dirs[path.resolve('/foo/node_modules')] = true;
112
113 function opts(basedir) {
114 return {
115 basedir: path.resolve(basedir),
116 isFile: function (file, cb) {
117 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
118 },
119 isDirectory: function (dir, cb) {
120 cb(null, !!dirs[path.resolve(dir)]);
121 },
122 readFile: function (file, cb) {
123 cb(null, files[path.resolve(file)]);
124 }
125 };
126 }
127
128 resolve('bar', opts('/foo'), function (err, res, pkg) {
129 if (err) return t.fail(err);
130 t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
131 t.equal(pkg && pkg.main, './baz.js');
132 });
133});
134
135test('mock package from package', function (t) {
136 t.plan(2);
137
138 var files = {};
139 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
140 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
141 main: './baz.js'
142 });
143
144 var dirs = {};
145 dirs[path.resolve('/foo')] = true;
146 dirs[path.resolve('/foo/node_modules')] = true;
147
148 function opts(basedir) {
149 return {
150 basedir: path.resolve(basedir),
151 isFile: function (file, cb) {
152 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
153 },
154 isDirectory: function (dir, cb) {
155 cb(null, !!dirs[path.resolve(dir)]);
156 },
157 'package': { main: 'bar' },
158 readFile: function (file, cb) {
159 cb(null, files[path.resolve(file)]);
160 }
161 };
162 }
163
164 resolve('bar', opts('/foo'), function (err, res, pkg) {
165 if (err) return t.fail(err);
166 t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
167 t.equal(pkg && pkg.main, './baz.js');
168 });
169});