summaryrefslogtreecommitdiff
path: root/test/es-module/test-esm-exports.mjs
diff options
context:
space:
mode:
authorJan Krems <jan.krems@gmail.com>2019-07-23 21:25:24 -0700
committerRich Trott <rtrott@gmail.com>2019-07-31 13:33:46 -0700
commit61f3a5c60ad78506e9e0caae061a04ccab878ca1 (patch)
tree06ca06f666a4986f46d460295a3397f659275518 /test/es-module/test-esm-exports.mjs
parent9d37013ef0e0f0f243da6057498a79fd43a2f784 (diff)
downloadandroid-node-v8-61f3a5c60ad78506e9e0caae061a04ccab878ca1.tar.gz
android-node-v8-61f3a5c60ad78506e9e0caae061a04ccab878ca1.tar.bz2
android-node-v8-61f3a5c60ad78506e9e0caae061a04ccab878ca1.zip
module: unify package exports test for CJS and ESM
Refs: https://github.com/nodejs/modules/issues/358 PR-URL: https://github.com/nodejs/node/pull/28831 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/es-module/test-esm-exports.mjs')
-rw-r--r--test/es-module/test-esm-exports.mjs111
1 files changed, 85 insertions, 26 deletions
diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs
index 38cd81511f..ca10097aa9 100644
--- a/test/es-module/test-esm-exports.mjs
+++ b/test/es-module/test-esm-exports.mjs
@@ -1,29 +1,88 @@
// Flags: --experimental-modules
import { mustCall } from '../common/index.mjs';
-import { ok, strictEqual } from 'assert';
-
-import { asdf, asdf2, space } from '../fixtures/pkgexports.mjs';
-import {
- loadMissing,
- loadFromNumber,
- loadDot,
-} from '../fixtures/pkgexports-missing.mjs';
-
-strictEqual(asdf, 'asdf');
-strictEqual(asdf2, 'asdf');
-strictEqual(space, 'encoded path');
-
-loadMissing().catch(mustCall((err) => {
- ok(err.message.toString().startsWith('Package exports'));
- ok(err.message.toString().indexOf('do not define a \'./missing\' subpath'));
-}));
-
-loadFromNumber().catch(mustCall((err) => {
- ok(err.message.toString().startsWith('Package exports'));
- ok(err.message.toString().indexOf('do not define a \'./missing\' subpath'));
-}));
-
-loadDot().catch(mustCall((err) => {
- ok(err.message.toString().startsWith('Cannot find main entry point'));
-}));
+import { ok, deepStrictEqual, strictEqual } from 'assert';
+
+import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
+
+[requireFixture, importFixture].forEach((loadFixture) => {
+ const isRequire = loadFixture === requireFixture;
+
+ const validSpecifiers = new Map([
+ // A simple mapping of a path.
+ ['pkgexports/valid-cjs', { default: 'asdf' }],
+ // A directory mapping, pointing to the package root.
+ ['pkgexports/sub/asdf.js', { default: 'asdf' }],
+ // A mapping pointing to a file that needs special encoding (%20) in URLs.
+ ['pkgexports/space', { default: 'encoded path' }],
+ // Verifying that normal packages still work with exports turned on.
+ isRequire ? ['baz/index', { default: 'eye catcher' }] : [null],
+ ]);
+ for (const [validSpecifier, expected] of validSpecifiers) {
+ if (validSpecifier === null) continue;
+
+ loadFixture(validSpecifier)
+ .then(mustCall((actual) => {
+ deepStrictEqual({ ...actual }, expected);
+ }));
+ }
+
+ // There's no such export - so there's nothing to do.
+ loadFixture('pkgexports/missing').catch(mustCall((err) => {
+ strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
+ assertStartsWith(err.message, 'Package exports');
+ assertIncludes(err.message, 'do not define a \'./missing\' subpath');
+ }));
+
+ // The file exists but isn't exported. The exports is a number which counts
+ // as a non-null value without any properties, just like `{}`.
+ loadFixture('pkgexports-number/hidden.js').catch(mustCall((err) => {
+ strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
+ assertStartsWith(err.message, 'Package exports');
+ assertIncludes(err.message, 'do not define a \'./hidden.js\' subpath');
+ }));
+
+ // There's no main field so we won't find anything when importing the name.
+ // The fact that "." is mapped is ignored, it's not a valid main config.
+ loadFixture('pkgexports').catch(mustCall((err) => {
+ if (isRequire) {
+ strictEqual(err.code, 'MODULE_NOT_FOUND');
+ assertStartsWith(err.message, 'Cannot find module \'pkgexports\'');
+ } else {
+ strictEqual(err.code, 'ERR_MODULE_NOT_FOUND');
+ assertStartsWith(err.message, 'Cannot find main entry point');
+ }
+ }));
+
+ // Even though 'pkgexports/sub/asdf.js' works, alternate "path-like" variants
+ // do not to prevent confusion and accidental loopholes.
+ loadFixture('pkgexports/sub/./../asdf.js').catch(mustCall((err) => {
+ strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
+ assertStartsWith(err.message, 'Package exports');
+ assertIncludes(err.message,
+ 'do not define a \'./sub/./../asdf.js\' subpath');
+ }));
+
+ // Covering out bases - not a file is still not a file after dir mapping.
+ loadFixture('pkgexports/sub/not-a-file.js').catch(mustCall((err) => {
+ if (isRequire) {
+ strictEqual(err.code, 'MODULE_NOT_FOUND');
+ assertStartsWith(err.message,
+ 'Cannot find module \'pkgexports/sub/not-a-file.js\'');
+ } else {
+ strictEqual(err.code, 'ERR_MODULE_NOT_FOUND');
+ // ESM currently returns a full file path
+ assertStartsWith(err.message, 'Cannot find module');
+ }
+ }));
+});
+
+function assertStartsWith(actual, expected) {
+ const start = actual.toString().substr(0, expected.length);
+ strictEqual(start, expected);
+}
+
+function assertIncludes(actual, expected) {
+ ok(actual.toString().indexOf(expected),
+ `${JSON.stringify(actual)} includes ${JSON.stringify(expected)}`);
+}