From 174b3c4b1b84fd43946dfc283bc518cb5a4ceeba Mon Sep 17 00:00:00 2001 From: Evgenii Shchepotev Date: Wed, 29 May 2019 19:45:26 +0300 Subject: [PATCH] test: add eval ESM module tests PR-URL: https://github.com/nodejs/node/pull/27956 Reviewed-By: Guy Bedford Reviewed-By: Jan Krems Reviewed-By: James M Snell Reviewed-By: Yongsheng Zhang Reviewed-By: Ruben Bridgewater --- test/parallel/test-cli-eval.js | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index 3fd00e7cdb4581..acf20bb77fc9d1 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -229,3 +229,48 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, assert.strictEqual(err, null); })); }); + +// ESModule eval tests + + +// Assert that "42\n" is written to stdout on module eval. +const execOptions = '--experimental-modules --input-type module'; +child.exec( + `${nodejs} ${execOptions} --eval "console.log(42)"`, + common.mustCall((err, stdout) => { + assert.ifError(err); + assert.strictEqual(stdout, '42\n'); + })); + +// Assert that "42\n" is written to stdout with print option. +child.exec( + `${nodejs} ${execOptions} --print --eval "42"`, + common.mustCall((err, stdout) => { + assert.ifError(err); + assert.strictEqual(stdout, '42\n'); + })); + +// Assert that error is written to stderr on invalid input. +child.exec( + `${nodejs} ${execOptions} --eval "!!!!"`, + common.mustCall((err, stdout, stderr) => { + assert.ok(err); + assert.strictEqual(stdout, ''); + assert.ok(stderr.indexOf('SyntaxError: Unexpected end of input') > 0); + })); + +// Assert that require is undefined in ESM support +child.exec( + `${nodejs} ${execOptions} --eval "console.log(typeof require);"`, + common.mustCall((err, stdout) => { + assert.ifError(err); + assert.strictEqual(stdout, 'undefined\n'); + })); + +// Assert that import.meta is defined in ESM +child.exec( + `${nodejs} ${execOptions} --eval "console.log(typeof import.meta);"`, + common.mustCall((err, stdout) => { + assert.ifError(err); + assert.strictEqual(stdout, 'object\n'); + }));