mock.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('mock', function (t) {
  5. t.plan(8);
  6. var files = {};
  7. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  8. function opts(basedir) {
  9. return {
  10. basedir: path.resolve(basedir),
  11. isFile: function (file, cb) {
  12. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  13. },
  14. readFile: function (file, cb) {
  15. cb(null, files[path.resolve(file)]);
  16. }
  17. };
  18. }
  19. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  20. if (err) return t.fail(err);
  21. t.equal(res, path.resolve('/foo/bar/baz.js'));
  22. t.equal(pkg, undefined);
  23. });
  24. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  25. if (err) return t.fail(err);
  26. t.equal(res, path.resolve('/foo/bar/baz.js'));
  27. t.equal(pkg, undefined);
  28. });
  29. resolve('baz', opts('/foo/bar'), function (err, res) {
  30. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  31. t.equal(err.code, 'MODULE_NOT_FOUND');
  32. });
  33. resolve('../baz', opts('/foo/bar'), function (err, res) {
  34. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  35. t.equal(err.code, 'MODULE_NOT_FOUND');
  36. });
  37. });
  38. test('mock from package', function (t) {
  39. t.plan(8);
  40. var files = {};
  41. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  42. function opts(basedir) {
  43. return {
  44. basedir: path.resolve(basedir),
  45. isFile: function (file, cb) {
  46. cb(null, Object.prototype.hasOwnProperty.call(files, file));
  47. },
  48. 'package': { main: 'bar' },
  49. readFile: function (file, cb) {
  50. cb(null, files[file]);
  51. }
  52. };
  53. }
  54. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  55. if (err) return t.fail(err);
  56. t.equal(res, path.resolve('/foo/bar/baz.js'));
  57. t.equal(pkg && pkg.main, 'bar');
  58. });
  59. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  60. if (err) return t.fail(err);
  61. t.equal(res, path.resolve('/foo/bar/baz.js'));
  62. t.equal(pkg && pkg.main, 'bar');
  63. });
  64. resolve('baz', opts('/foo/bar'), function (err, res) {
  65. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  66. t.equal(err.code, 'MODULE_NOT_FOUND');
  67. });
  68. resolve('../baz', opts('/foo/bar'), function (err, res) {
  69. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  70. t.equal(err.code, 'MODULE_NOT_FOUND');
  71. });
  72. });
  73. test('mock package', function (t) {
  74. t.plan(2);
  75. var files = {};
  76. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  77. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  78. main: './baz.js'
  79. });
  80. function opts(basedir) {
  81. return {
  82. basedir: path.resolve(basedir),
  83. isFile: function (file, cb) {
  84. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  85. },
  86. readFile: function (file, cb) {
  87. cb(null, files[path.resolve(file)]);
  88. }
  89. };
  90. }
  91. resolve('bar', opts('/foo'), function (err, res, pkg) {
  92. if (err) return t.fail(err);
  93. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  94. t.equal(pkg && pkg.main, './baz.js');
  95. });
  96. });
  97. test('mock package from package', function (t) {
  98. t.plan(2);
  99. var files = {};
  100. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  101. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  102. main: './baz.js'
  103. });
  104. function opts(basedir) {
  105. return {
  106. basedir: path.resolve(basedir),
  107. isFile: function (file, cb) {
  108. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  109. },
  110. 'package': { main: 'bar' },
  111. readFile: function (file, cb) {
  112. cb(null, files[path.resolve(file)]);
  113. }
  114. };
  115. }
  116. resolve('bar', opts('/foo'), function (err, res, pkg) {
  117. if (err) return t.fail(err);
  118. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  119. t.equal(pkg && pkg.main, './baz.js');
  120. });
  121. });