buddy

node MVC discord bot
Log | Files | Refs | README

mock.js (3832B)


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