mock_sync.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. function opts(basedir) {
  9. return {
  10. basedir: path.resolve(basedir),
  11. isFile: function (file) {
  12. return Object.prototype.hasOwnProperty.call(files, file);
  13. },
  14. readFileSync: function (file) {
  15. return files[file];
  16. }
  17. };
  18. }
  19. t.equal(
  20. resolve.sync('./baz', opts('/foo/bar')),
  21. path.resolve('/foo/bar/baz.js')
  22. );
  23. t.equal(
  24. resolve.sync('./baz.js', opts('/foo/bar')),
  25. path.resolve('/foo/bar/baz.js')
  26. );
  27. t.throws(function () {
  28. resolve.sync('baz', opts('/foo/bar'));
  29. });
  30. t.throws(function () {
  31. resolve.sync('../baz', opts('/foo/bar'));
  32. });
  33. });
  34. test('mock package', function (t) {
  35. t.plan(1);
  36. var files = {};
  37. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  38. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  39. main: './baz.js'
  40. });
  41. function opts(basedir) {
  42. return {
  43. basedir: path.resolve(basedir),
  44. isFile: function (file) {
  45. return Object.prototype.hasOwnProperty.call(files, file);
  46. },
  47. readFileSync: function (file) {
  48. return files[file];
  49. }
  50. };
  51. }
  52. t.equal(
  53. resolve.sync('bar', opts('/foo')),
  54. path.resolve('/foo/node_modules/bar/baz.js')
  55. );
  56. });