From 3704db3dcee711e129f171011da84abf689d87e0 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 2 Mar 2020 12:21:14 +0100 Subject: [PATCH] test: use index.js if package.json "main" is empty Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: https://github.com/nodejs/node/issues/32013 --- test/fixtures/require-empty-main/index.js | 2 ++ test/fixtures/require-empty-main/package.json | 1 + test/parallel/test-require-empty-main.js | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 test/fixtures/require-empty-main/index.js create mode 100644 test/fixtures/require-empty-main/package.json create mode 100644 test/parallel/test-require-empty-main.js diff --git a/test/fixtures/require-empty-main/index.js b/test/fixtures/require-empty-main/index.js new file mode 100644 index 00000000000000..d2ed2dd2202c19 --- /dev/null +++ b/test/fixtures/require-empty-main/index.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = 42; diff --git a/test/fixtures/require-empty-main/package.json b/test/fixtures/require-empty-main/package.json new file mode 100644 index 00000000000000..3f0b7c677acf3c --- /dev/null +++ b/test/fixtures/require-empty-main/package.json @@ -0,0 +1 @@ +{"main":""} diff --git a/test/parallel/test-require-empty-main.js b/test/parallel/test-require-empty-main.js new file mode 100644 index 00000000000000..73f141d1f9ea8f --- /dev/null +++ b/test/parallel/test-require-empty-main.js @@ -0,0 +1,25 @@ +'use strict'; +require('../common'); + +// A package.json with an empty "main" property should use index.js if present. +// require.resolve() should resolve to index.js for the same reason. +// +// In fact, any "main" property that doesn't resolve to a file should result +// in index.js being used, but that's already checked for by other tests. +// This test only concerns itself with the empty string. + +const assert = require('assert'); +const path = require('path'); +const fixtures = require('../common/fixtures'); + +const where = fixtures.path('require-empty-main'); +const expected = path.join(where, 'index.js'); + +test(); +setImmediate(test); + +function test() { + assert.strictEqual(require.resolve(where), expected); + assert.strictEqual(require(where), 42); + assert.strictEqual(require.resolve(where), expected); +}