wallet-core/node_modules/resolve/test/mock_sync.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
var path = require('path');
2016-10-10 03:43:44 +02:00
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(4);
2017-05-28 00:38:50 +02:00
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, file);
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
readFileSync: function (file) {
return files[file];
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
2017-05-28 00:38:50 +02:00
path.resolve('/foo/bar/baz.js')
2016-10-10 03:43:44 +02:00
);
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
2017-05-28 00:38:50 +02:00
path.resolve('/foo/bar/baz.js')
2016-10-10 03:43:44 +02:00
);
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
t.throws(function () {
resolve.sync('baz', opts('/foo/bar'));
});
t.throws(function () {
resolve.sync('../baz', opts('/foo/bar'));
});
});
test('mock package', function (t) {
t.plan(1);
2017-05-28 00:38:50 +02:00
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, file);
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
readFileSync: function (file) {
return files[file];
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
t.equal(
resolve.sync('bar', opts('/foo')),
2017-05-28 00:38:50 +02:00
path.resolve('/foo/node_modules/bar/baz.js')
2016-10-10 03:43:44 +02:00
);
});