mock_sync.js (1488B)
1 var test = require('tape'); 2 var resolve = require('../'); 3 4 test('mock', function (t) { 5 t.plan(4); 6 7 var files = { 8 '/foo/bar/baz.js' : 'beep' 9 }; 10 11 function opts (basedir) { 12 return { 13 basedir : basedir, 14 isFile : function (file) { 15 return files.hasOwnProperty(file) 16 }, 17 readFileSync : function (file) { 18 return files[file] 19 } 20 } 21 } 22 23 t.equal( 24 resolve.sync('./baz', opts('/foo/bar')), 25 '/foo/bar/baz.js' 26 ); 27 28 t.equal( 29 resolve.sync('./baz.js', opts('/foo/bar')), 30 '/foo/bar/baz.js' 31 ); 32 33 t.throws(function () { 34 resolve.sync('baz', opts('/foo/bar')); 35 }); 36 37 t.throws(function () { 38 resolve.sync('../baz', opts('/foo/bar')); 39 }); 40 }); 41 42 test('mock package', function (t) { 43 t.plan(1); 44 45 var files = { 46 '/foo/node_modules/bar/baz.js' : 'beep', 47 '/foo/node_modules/bar/package.json' : JSON.stringify({ 48 main : './baz.js' 49 }) 50 }; 51 52 function opts (basedir) { 53 return { 54 basedir : basedir, 55 isFile : function (file) { 56 return files.hasOwnProperty(file) 57 }, 58 readFileSync : function (file) { 59 return files[file] 60 } 61 } 62 } 63 64 t.equal( 65 resolve.sync('bar', opts('/foo')), 66 '/foo/node_modules/bar/baz.js' 67 ); 68 });