You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.5 KiB

4 years ago
  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('incorrect main', function (t) {
  146. var resolverDir = path.join(__dirname, 'resolver');
  147. var dir = path.join(resolverDir, 'incorrect_main');
  148. t.equal(
  149. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  150. path.join(dir, 'index.js')
  151. );
  152. t.end();
  153. });
  154. var stubStatSync = function stubStatSync(fn) {
  155. var fs = require('fs');
  156. var statSync = fs.statSync;
  157. try {
  158. fs.statSync = function () {
  159. throw new EvalError('Unknown Error');
  160. };
  161. return fn();
  162. } finally {
  163. fs.statSync = statSync;
  164. }
  165. };
  166. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  167. var dir = path.join(__dirname, 'resolver');
  168. stubStatSync(function () {
  169. t.throws(function () {
  170. resolve.sync('foo', { basedir: dir });
  171. }, /Unknown Error/);
  172. });
  173. t.end();
  174. });
  175. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  176. var dir = path.join(__dirname, 'resolver');
  177. t.equal(
  178. resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
  179. path.join(dir, 'same_names/foo.js')
  180. );
  181. t.equal(
  182. resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
  183. path.join(dir, 'same_names/foo/index.js')
  184. );
  185. t.end();
  186. });
  187. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  188. var testFile = path.basename(__filename);
  189. t.test('sanity check', function (st) {
  190. st.equal(
  191. resolve.sync('./' + testFile),
  192. __filename,
  193. 'sanity check'
  194. );
  195. st.end();
  196. });
  197. t.test('with a fake directory', function (st) {
  198. function run() { return resolve.sync('./' + testFile + '/blah'); }
  199. st.throws(run, 'throws an error');
  200. try {
  201. run();
  202. } catch (e) {
  203. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  204. st.equal(
  205. e.message,
  206. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  207. 'can not find nonexistent module'
  208. );
  209. }
  210. st.end();
  211. });
  212. t.end();
  213. });
  214. test('sync dot main', function (t) {
  215. var start = new Date();
  216. t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
  217. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  218. t.end();
  219. });
  220. test('sync dot slash main', function (t) {
  221. var start = new Date();
  222. t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  223. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  224. t.end();
  225. });
  226. test('not a directory', function (t) {
  227. var path = './foo';
  228. try {
  229. resolve.sync(path, { basedir: __filename });
  230. t.fail();
  231. } catch (err) {
  232. t.ok(err, 'a non-directory errors');
  233. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  234. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  235. }
  236. t.end();
  237. });
  238. test('non-string "main" field in package.json', function (t) {
  239. var dir = path.join(__dirname, 'resolver');
  240. try {
  241. var result = resolve.sync('./invalid_main', { basedir: dir });
  242. t.equal(result, undefined, 'result should not exist');
  243. t.fail('should not get here');
  244. } catch (err) {
  245. t.ok(err, 'errors on non-string main');
  246. t.equal(err.message, 'package “invalid main” `main` must be a string');
  247. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  248. }
  249. t.end();
  250. });
  251. test('non-string "main" field in package.json', function (t) {
  252. var dir = path.join(__dirname, 'resolver');
  253. try {
  254. var result = resolve.sync('./invalid_main', { basedir: dir });
  255. t.equal(result, undefined, 'result should not exist');
  256. t.fail('should not get here');
  257. } catch (err) {
  258. t.ok(err, 'errors on non-string main');
  259. t.equal(err.message, 'package “invalid main” `main` must be a string');
  260. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  261. }
  262. t.end();
  263. });
  264. test('browser field in package.json', function (t) {
  265. var dir = path.join(__dirname, 'resolver');
  266. var res = resolve.sync('./browser_field', {
  267. basedir: dir,
  268. packageFilter: function packageFilter(pkg) {
  269. if (pkg.browser) {
  270. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  271. delete pkg.browser; // eslint-disable-line no-param-reassign
  272. }
  273. return pkg;
  274. }
  275. });
  276. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  277. t.end();
  278. });