resolver_sync.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('foo', function (t) {
  5. var dir = path.join(__dirname, 'resolver');
  6. t.equal(
  7. resolve.sync('./foo', { basedir: dir }),
  8. path.join(dir, 'foo.js')
  9. );
  10. t.equal(
  11. resolve.sync('./foo.js', { basedir: dir }),
  12. path.join(dir, 'foo.js')
  13. );
  14. t.equal(
  15. resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
  16. path.join(dir, 'foo.js')
  17. );
  18. t.throws(function () {
  19. resolve.sync('foo', { basedir: dir });
  20. });
  21. // Test that filename is reported as the "from" value when passed.
  22. t.throws(
  23. function () {
  24. resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
  25. },
  26. {
  27. name: 'Error',
  28. message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
  29. }
  30. );
  31. t.end();
  32. });
  33. test('bar', function (t) {
  34. var dir = path.join(__dirname, 'resolver');
  35. t.equal(
  36. resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
  37. path.join(dir, 'bar/node_modules/foo/index.js')
  38. );
  39. t.end();
  40. });
  41. test('baz', function (t) {
  42. var dir = path.join(__dirname, 'resolver');
  43. t.equal(
  44. resolve.sync('./baz', { basedir: dir }),
  45. path.join(dir, 'baz/quux.js')
  46. );
  47. t.end();
  48. });
  49. test('biz', function (t) {
  50. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  51. t.equal(
  52. resolve.sync('./grux', { basedir: dir }),
  53. path.join(dir, 'grux/index.js')
  54. );
  55. t.equal(
  56. resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
  57. path.join(dir, 'tiv/index.js')
  58. );
  59. t.equal(
  60. resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
  61. path.join(dir, 'grux/index.js')
  62. );
  63. t.end();
  64. });
  65. test('normalize', function (t) {
  66. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  67. t.equal(
  68. resolve.sync('../grux', { basedir: dir }),
  69. path.join(dir, 'index.js')
  70. );
  71. t.end();
  72. });
  73. test('cup', function (t) {
  74. var dir = path.join(__dirname, 'resolver');
  75. t.equal(
  76. resolve.sync('./cup', {
  77. basedir: dir,
  78. extensions: ['.js', '.coffee']
  79. }),
  80. path.join(dir, 'cup.coffee')
  81. );
  82. t.equal(
  83. resolve.sync('./cup.coffee', { basedir: dir }),
  84. path.join(dir, 'cup.coffee')
  85. );
  86. t.throws(function () {
  87. resolve.sync('./cup', {
  88. basedir: dir,
  89. extensions: ['.js']
  90. });
  91. });
  92. t.end();
  93. });
  94. test('mug', function (t) {
  95. var dir = path.join(__dirname, 'resolver');
  96. t.equal(
  97. resolve.sync('./mug', { basedir: dir }),
  98. path.join(dir, 'mug.js')
  99. );
  100. t.equal(
  101. resolve.sync('./mug', {
  102. basedir: dir,
  103. extensions: ['.coffee', '.js']
  104. }),
  105. path.join(dir, 'mug.coffee')
  106. );
  107. t.equal(
  108. resolve.sync('./mug', {
  109. basedir: dir,
  110. extensions: ['.js', '.coffee']
  111. }),
  112. path.join(dir, 'mug.js')
  113. );
  114. t.end();
  115. });
  116. test('other path', function (t) {
  117. var resolverDir = path.join(__dirname, 'resolver');
  118. var dir = path.join(resolverDir, 'bar');
  119. var otherDir = path.join(resolverDir, 'other_path');
  120. t.equal(
  121. resolve.sync('root', {
  122. basedir: dir,
  123. paths: [otherDir]
  124. }),
  125. path.join(resolverDir, 'other_path/root.js')
  126. );
  127. t.equal(
  128. resolve.sync('lib/other-lib', {
  129. basedir: dir,
  130. paths: [otherDir]
  131. }),
  132. path.join(resolverDir, 'other_path/lib/other-lib.js')
  133. );
  134. t.throws(function () {
  135. resolve.sync('root', { basedir: dir });
  136. });
  137. t.throws(function () {
  138. resolve.sync('zzz', {
  139. basedir: dir,
  140. paths: [otherDir]
  141. });
  142. });
  143. t.end();
  144. });
  145. test('path iterator', function (t) {
  146. var resolverDir = path.join(__dirname, 'resolver');
  147. var exactIterator = function (x, start, getPackageCandidates, opts) {
  148. return [path.join(resolverDir, x)];
  149. };
  150. t.equal(
  151. resolve.sync('baz', { packageIterator: exactIterator }),
  152. path.join(resolverDir, 'baz/quux.js')
  153. );
  154. t.end();
  155. });
  156. test('incorrect main', function (t) {
  157. var resolverDir = path.join(__dirname, 'resolver');
  158. var dir = path.join(resolverDir, 'incorrect_main');
  159. t.equal(
  160. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  161. path.join(dir, 'index.js')
  162. );
  163. t.end();
  164. });
  165. var stubStatSync = function stubStatSync(fn) {
  166. var fs = require('fs');
  167. var statSync = fs.statSync;
  168. try {
  169. fs.statSync = function () {
  170. throw new EvalError('Unknown Error');
  171. };
  172. return fn();
  173. } finally {
  174. fs.statSync = statSync;
  175. }
  176. };
  177. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  178. var dir = path.join(__dirname, 'resolver');
  179. stubStatSync(function () {
  180. t.throws(function () {
  181. resolve.sync('foo', { basedir: dir });
  182. }, /Unknown Error/);
  183. });
  184. t.end();
  185. });
  186. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  187. var dir = path.join(__dirname, 'resolver');
  188. t.equal(
  189. resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
  190. path.join(dir, 'same_names/foo.js')
  191. );
  192. t.equal(
  193. resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
  194. path.join(dir, 'same_names/foo/index.js')
  195. );
  196. t.end();
  197. });
  198. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  199. var dir = path.join(__dirname, 'resolver');
  200. t.equal(
  201. resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
  202. path.join(dir, 'same_names/foo/index.js')
  203. );
  204. t.equal(
  205. resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
  206. path.join(dir, 'same_names/foo/index.js')
  207. );
  208. t.end();
  209. });
  210. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  211. var testFile = path.basename(__filename);
  212. t.test('sanity check', function (st) {
  213. st.equal(
  214. resolve.sync('./' + testFile),
  215. __filename,
  216. 'sanity check'
  217. );
  218. st.end();
  219. });
  220. t.test('with a fake directory', function (st) {
  221. function run() { return resolve.sync('./' + testFile + '/blah'); }
  222. st.throws(run, 'throws an error');
  223. try {
  224. run();
  225. } catch (e) {
  226. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  227. st.equal(
  228. e.message,
  229. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  230. 'can not find nonexistent module'
  231. );
  232. }
  233. st.end();
  234. });
  235. t.end();
  236. });
  237. test('sync dot main', function (t) {
  238. var start = new Date();
  239. t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
  240. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  241. t.end();
  242. });
  243. test('sync dot slash main', function (t) {
  244. var start = new Date();
  245. t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  246. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  247. t.end();
  248. });
  249. test('not a directory', function (t) {
  250. var path = './foo';
  251. try {
  252. resolve.sync(path, { basedir: __filename });
  253. t.fail();
  254. } catch (err) {
  255. t.ok(err, 'a non-directory errors');
  256. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  257. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  258. }
  259. t.end();
  260. });
  261. test('non-string "main" field in package.json', function (t) {
  262. var dir = path.join(__dirname, 'resolver');
  263. try {
  264. var result = resolve.sync('./invalid_main', { basedir: dir });
  265. t.equal(result, undefined, 'result should not exist');
  266. t.fail('should not get here');
  267. } catch (err) {
  268. t.ok(err, 'errors on non-string main');
  269. t.equal(err.message, 'package “invalid main” `main` must be a string');
  270. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  271. }
  272. t.end();
  273. });
  274. test('non-string "main" field in package.json', function (t) {
  275. var dir = path.join(__dirname, 'resolver');
  276. try {
  277. var result = resolve.sync('./invalid_main', { basedir: dir });
  278. t.equal(result, undefined, 'result should not exist');
  279. t.fail('should not get here');
  280. } catch (err) {
  281. t.ok(err, 'errors on non-string main');
  282. t.equal(err.message, 'package “invalid main” `main` must be a string');
  283. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  284. }
  285. t.end();
  286. });
  287. test('browser field in package.json', function (t) {
  288. var dir = path.join(__dirname, 'resolver');
  289. var res = resolve.sync('./browser_field', {
  290. basedir: dir,
  291. packageFilter: function packageFilter(pkg) {
  292. if (pkg.browser) {
  293. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  294. delete pkg.browser; // eslint-disable-line no-param-reassign
  295. }
  296. return pkg;
  297. }
  298. });
  299. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  300. t.end();
  301. });