mock_sync.js 2.0 KB

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