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.

80 lines
2.0 KiB

4 years ago
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('mock', function (t) {
  5. t.plan(4);
  6. var files = {};
  7. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  8. var dirs = {};
  9. dirs[path.resolve('/foo/bar')] = true;
  10. function opts(basedir) {
  11. return {
  12. basedir: path.resolve(basedir),
  13. isFile: function (file) {
  14. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  15. },
  16. isDirectory: function (dir) {
  17. return !!dirs[path.resolve(dir)];
  18. },
  19. readFileSync: function (file) {
  20. return files[path.resolve(file)];
  21. }
  22. };
  23. }
  24. t.equal(
  25. resolve.sync('./baz', opts('/foo/bar')),
  26. path.resolve('/foo/bar/baz.js')
  27. );
  28. t.equal(
  29. resolve.sync('./baz.js', opts('/foo/bar')),
  30. path.resolve('/foo/bar/baz.js')
  31. );
  32. t.throws(function () {
  33. resolve.sync('baz', opts('/foo/bar'));
  34. });
  35. t.throws(function () {
  36. resolve.sync('../baz', opts('/foo/bar'));
  37. });
  38. });
  39. test('mock package', function (t) {
  40. t.plan(1);
  41. var files = {};
  42. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  43. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  44. main: './baz.js'
  45. });
  46. var dirs = {};
  47. dirs[path.resolve('/foo')] = true;
  48. dirs[path.resolve('/foo/node_modules')] = true;
  49. function opts(basedir) {
  50. return {
  51. basedir: path.resolve(basedir),
  52. isFile: function (file) {
  53. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  54. },
  55. isDirectory: function (dir) {
  56. return !!dirs[path.resolve(dir)];
  57. },
  58. readFileSync: function (file) {
  59. return files[path.resolve(file)];
  60. }
  61. };
  62. }
  63. t.equal(
  64. resolve.sync('bar', opts('/foo')),
  65. path.resolve('/foo/node_modules/bar/baz.js')
  66. );
  67. });