Skip to content

Commit

Permalink
Added more test cases
Browse files Browse the repository at this point in the history
- When the value of "main" is an empty string (empty_main)

- When the "main" parameter does not exist in package.json (missing_main)

- When the value of "main" is null (null_main)
  • Loading branch information
Cheesetouched committed Dec 21, 2020
1 parent 6e0366e commit 5792b01
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
42 changes: 40 additions & 2 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ test('path iterator', function (t) {
});
});

test('empty main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'empty_main');

resolve('./empty_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

test('incorrect main', function (t) {
t.plan(1);

Expand All @@ -284,11 +296,37 @@ test('incorrect main', function (t) {
});
});

test('missing index', function (t) {
t.plan(2);

var resolverDir = path.join(__dirname, 'resolver');
resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
t.ok(err instanceof Error);
t.equal(err && err.code, 'INCORRECT_PACKAGE_MAIN', 'error has correct error code');
});
});

test('missing main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'missing_main');

resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
if (!err) t.fail('No error thrown when main was found missing.');
t.end();
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

test('null main', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'null_main');

resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});

Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test/resolver/empty_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": ""
}
3 changes: 3 additions & 0 deletions test/resolver/missing_index/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "index.js"
}
Empty file.
2 changes: 1 addition & 1 deletion test/resolver/missing_main/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"main": "index.js"
"notmain": "index.js"
}
Empty file.
3 changes: 3 additions & 0 deletions test/resolver/null_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": null
}

0 comments on commit 5792b01

Please sign in to comment.