mock.js 3.7 KB

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