mock_sync.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var test = require('tape');
  2. var resolve = require('../');
  3. test('mock', function (t) {
  4. t.plan(4);
  5. var files = {
  6. '/foo/bar/baz.js' : 'beep'
  7. };
  8. function opts (basedir) {
  9. return {
  10. basedir : basedir,
  11. isFile : function (file) {
  12. return files.hasOwnProperty(file)
  13. },
  14. readFileSync : function (file) {
  15. return files[file]
  16. }
  17. }
  18. }
  19. t.equal(
  20. resolve.sync('./baz', opts('/foo/bar')),
  21. '/foo/bar/baz.js'
  22. );
  23. t.equal(
  24. resolve.sync('./baz.js', opts('/foo/bar')),
  25. '/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. '/foo/node_modules/bar/baz.js' : 'beep',
  38. '/foo/node_modules/bar/package.json' : JSON.stringify({
  39. main : './baz.js'
  40. })
  41. };
  42. function opts (basedir) {
  43. return {
  44. basedir : basedir,
  45. isFile : function (file) {
  46. return files.hasOwnProperty(file)
  47. },
  48. readFileSync : function (file) {
  49. return files[file]
  50. }
  51. }
  52. }
  53. t.equal(
  54. resolve.sync('bar', opts('/foo')),
  55. '/foo/node_modules/bar/baz.js'
  56. );
  57. });