Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

mock.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. var dirs = {};
  9. dirs[path.resolve('/foo/bar')] = true;
  10. function opts(basedir) {
  11. return {
  12. basedir: path.resolve(basedir),
  13. isFile: function (file, cb) {
  14. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  15. },
  16. isDirectory: function (dir, cb) {
  17. cb(null, !!dirs[path.resolve(dir)]);
  18. },
  19. readFile: function (file, cb) {
  20. cb(null, files[path.resolve(file)]);
  21. }
  22. };
  23. }
  24. resolve('./baz', 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.js', 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. resolve('baz', opts('/foo/bar'), function (err, res) {
  35. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  36. t.equal(err.code, 'MODULE_NOT_FOUND');
  37. });
  38. resolve('../baz', opts('/foo/bar'), function (err, res) {
  39. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  40. t.equal(err.code, 'MODULE_NOT_FOUND');
  41. });
  42. });
  43. test('mock from package', function (t) {
  44. t.plan(8);
  45. var files = {};
  46. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  47. var dirs = {};
  48. dirs[path.resolve('/foo/bar')] = true;
  49. function opts(basedir) {
  50. return {
  51. basedir: path.resolve(basedir),
  52. isFile: function (file, cb) {
  53. cb(null, Object.prototype.hasOwnProperty.call(files, file));
  54. },
  55. isDirectory: function (dir, cb) {
  56. cb(null, !!dirs[path.resolve(dir)]);
  57. },
  58. 'package': { main: 'bar' },
  59. readFile: function (file, cb) {
  60. cb(null, files[file]);
  61. }
  62. };
  63. }
  64. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  65. if (err) return t.fail(err);
  66. t.equal(res, path.resolve('/foo/bar/baz.js'));
  67. t.equal(pkg && pkg.main, 'bar');
  68. });
  69. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  70. if (err) return t.fail(err);
  71. t.equal(res, path.resolve('/foo/bar/baz.js'));
  72. t.equal(pkg && pkg.main, 'bar');
  73. });
  74. resolve('baz', opts('/foo/bar'), function (err, res) {
  75. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  76. t.equal(err.code, 'MODULE_NOT_FOUND');
  77. });
  78. resolve('../baz', opts('/foo/bar'), function (err, res) {
  79. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  80. t.equal(err.code, 'MODULE_NOT_FOUND');
  81. });
  82. });
  83. test('mock package', function (t) {
  84. t.plan(2);
  85. var files = {};
  86. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  87. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  88. main: './baz.js'
  89. });
  90. var dirs = {};
  91. dirs[path.resolve('/foo')] = true;
  92. dirs[path.resolve('/foo/node_modules')] = true;
  93. function opts(basedir) {
  94. return {
  95. basedir: path.resolve(basedir),
  96. isFile: function (file, cb) {
  97. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  98. },
  99. isDirectory: function (dir, cb) {
  100. cb(null, !!dirs[path.resolve(dir)]);
  101. },
  102. readFile: function (file, cb) {
  103. cb(null, files[path.resolve(file)]);
  104. }
  105. };
  106. }
  107. resolve('bar', opts('/foo'), function (err, res, pkg) {
  108. if (err) return t.fail(err);
  109. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  110. t.equal(pkg && pkg.main, './baz.js');
  111. });
  112. });
  113. test('mock package from package', function (t) {
  114. t.plan(2);
  115. var files = {};
  116. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  117. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  118. main: './baz.js'
  119. });
  120. var dirs = {};
  121. dirs[path.resolve('/foo')] = true;
  122. dirs[path.resolve('/foo/node_modules')] = true;
  123. function opts(basedir) {
  124. return {
  125. basedir: path.resolve(basedir),
  126. isFile: function (file, cb) {
  127. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  128. },
  129. isDirectory: function (dir, cb) {
  130. cb(null, !!dirs[path.resolve(dir)]);
  131. },
  132. 'package': { main: 'bar' },
  133. readFile: function (file, cb) {
  134. cb(null, files[path.resolve(file)]);
  135. }
  136. };
  137. }
  138. resolve('bar', opts('/foo'), function (err, res, pkg) {
  139. if (err) return t.fail(err);
  140. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  141. t.equal(pkg && pkg.main, './baz.js');
  142. });
  143. });