mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-12 11:16:09 +09:00
Clean up the oopsie. Rebuild the blank exercises.
This commit is contained in:
+102
-11
@@ -15,10 +15,26 @@ test('foo', function (t) {
|
||||
path.join(dir, 'foo.js')
|
||||
);
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
|
||||
path.join(dir, 'foo.js')
|
||||
);
|
||||
|
||||
t.throws(function () {
|
||||
resolve.sync('foo', { basedir: dir });
|
||||
});
|
||||
|
||||
// Test that filename is reported as the "from" value when passed.
|
||||
t.throws(
|
||||
function () {
|
||||
resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
|
||||
}
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
@@ -156,6 +172,21 @@ test('other path', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('path iterator', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver');
|
||||
|
||||
var exactIterator = function (x, start, getPackageCandidates, opts) {
|
||||
return [path.join(resolverDir, x)];
|
||||
};
|
||||
|
||||
t.equal(
|
||||
resolve.sync('baz', { packageIterator: exactIterator }),
|
||||
path.join(resolverDir, 'baz/quux.js')
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('incorrect main', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver');
|
||||
var dir = path.join(resolverDir, 'incorrect_main');
|
||||
@@ -168,17 +199,6 @@ test('incorrect main', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('#25: node modules with the same name as node stdlib modules', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver/punycode');
|
||||
|
||||
t.equal(
|
||||
resolve.sync('punycode', { basedir: resolverDir }),
|
||||
path.join(resolverDir, 'node_modules/punycode/index.js')
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
var stubStatSync = function stubStatSync(fn) {
|
||||
var fs = require('fs');
|
||||
var statSync = fs.statSync;
|
||||
@@ -218,6 +238,20 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
|
||||
path.join(dir, 'same_names/foo/index.js')
|
||||
);
|
||||
t.equal(
|
||||
resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
|
||||
path.join(dir, 'same_names/foo/index.js')
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
|
||||
var testFile = path.basename(__filename);
|
||||
|
||||
@@ -265,3 +299,60 @@ test('sync dot slash main', function (t) {
|
||||
t.ok(new Date() - start < 50, 'resolve.sync timedout');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('not a directory', function (t) {
|
||||
var path = './foo';
|
||||
try {
|
||||
resolve.sync(path, { basedir: __filename });
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.ok(err, 'a non-directory errors');
|
||||
t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
|
||||
t.equal(err && err.code, 'MODULE_NOT_FOUND');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
try {
|
||||
var result = resolve.sync('./invalid_main', { basedir: dir });
|
||||
t.equal(result, undefined, 'result should not exist');
|
||||
t.fail('should not get here');
|
||||
} catch (err) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
try {
|
||||
var result = resolve.sync('./invalid_main', { basedir: dir });
|
||||
t.equal(result, undefined, 'result should not exist');
|
||||
t.fail('should not get here');
|
||||
} catch (err) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('browser field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
var res = resolve.sync('./browser_field', {
|
||||
basedir: dir,
|
||||
packageFilter: function packageFilter(pkg) {
|
||||
if (pkg.browser) {
|
||||
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
|
||||
delete pkg.browser; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
return pkg;
|
||||
}
|
||||
});
|
||||
t.equal(res, path.join(dir, 'browser_field', 'b.js'));
|
||||
t.end();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user