summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGuy Bedford <guybedford@gmail.com>2019-10-09 17:21:31 -0400
committerGuy Bedford <guybedford@gmail.com>2019-10-11 17:37:46 -0400
commitaca1c283bd8c6778b477286b8516f7c38dc9cefb (patch)
tree8b63435cde4623163602b9872d1d1acd9e62e071 /test
parent7812a615ab3cf992e1f4c5b63a2e5cddcf1fedf6 (diff)
downloadandroid-node-v8-aca1c283bd8c6778b477286b8516f7c38dc9cefb.tar.gz
android-node-v8-aca1c283bd8c6778b477286b8516f7c38dc9cefb.tar.bz2
android-node-v8-aca1c283bd8c6778b477286b8516f7c38dc9cefb.zip
module: warn on require of .js inside type: module
PR-URL: https://github.com/nodejs/node/pull/29909 Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/es-module/test-cjs-esm-warn.js38
-rw-r--r--test/fixtures/es-modules/cjs-esm.js1
-rw-r--r--test/fixtures/es-modules/package-type-module/cjs.js1
3 files changed, 40 insertions, 0 deletions
diff --git a/test/es-module/test-cjs-esm-warn.js b/test/es-module/test-cjs-esm-warn.js
new file mode 100644
index 0000000000..ec368c73e2
--- /dev/null
+++ b/test/es-module/test-cjs-esm-warn.js
@@ -0,0 +1,38 @@
+'use strict';
+
+const common = require('../common');
+const fixtures = require('../common/fixtures');
+const { spawn } = require('child_process');
+const assert = require('assert');
+const path = require('path');
+
+const requiring = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
+const required = path.resolve(
+ fixtures.path('/es-modules/package-type-module/cjs.js')
+);
+const pjson = path.resolve(
+ fixtures.path('/es-modules/package-type-module/package.json')
+);
+
+const basename = 'cjs.js';
+
+const child = spawn(process.execPath, [requiring]);
+let stderr = '';
+child.stderr.setEncoding('utf8');
+child.stderr.on('data', (data) => {
+ stderr += data;
+});
+child.on('close', common.mustCall((code, signal) => {
+ assert.strictEqual(code, 0);
+ assert.strictEqual(signal, null);
+
+ assert.strictEqual(stderr, `(node:${child.pid}) Warning: ` +
+ 'require() of ES modules is not supported.\nrequire() of ' +
+ `${required} from ${requiring} ` +
+ 'is an ES module file as it is a .js file whose nearest parent ' +
+ 'package.json contains "type": "module" which defines all .js ' +
+ 'files in that package scope as ES modules.\nInstead rename ' +
+ `${basename} to end in .cjs, change the requiring code to use ` +
+ 'import(), or remove "type": "module" from ' +
+ `${pjson}.\n`);
+}));
diff --git a/test/fixtures/es-modules/cjs-esm.js b/test/fixtures/es-modules/cjs-esm.js
new file mode 100644
index 0000000000..3599178996
--- /dev/null
+++ b/test/fixtures/es-modules/cjs-esm.js
@@ -0,0 +1 @@
+require('./package-type-module/cjs.js');
diff --git a/test/fixtures/es-modules/package-type-module/cjs.js b/test/fixtures/es-modules/package-type-module/cjs.js
new file mode 100644
index 0000000000..683f2d8ba6
--- /dev/null
+++ b/test/fixtures/es-modules/package-type-module/cjs.js
@@ -0,0 +1 @@
+module.exports = 'asdf';