From 1f8eaec45409de9eae3071b4fde16b76aa63af6f Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 7 May 2024 08:11:54 +0800 Subject: [PATCH] test: add common.expectRequiredModule() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To minimize changes if/when we change the layout of the result returned by require(esm). PR-URL: https://github.com/nodejs/node/pull/52868 Fixes: https://github.com/nodejs/node/issues/52864 Reviewed-By: Moshe Atlow Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Antoine du Hamel Reviewed-By: Chengzhong Wu --- test/common/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/common/index.js b/test/common/index.js index d2c68c0fafb01b..84805723e4be6a 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -32,6 +32,7 @@ const net = require('net'); const path = require('path'); const { inspect } = require('util'); const { isMainThread } = require('worker_threads'); +const { isModuleNamespaceObject } = require('util/types'); const tmpdir = require('./tmpdir'); const bits = ['arm64', 'loong64', 'mips', 'mipsel', 'ppc64', 'riscv64', 's390x', 'x64'] @@ -938,6 +939,18 @@ function getPrintedStackTrace(stderr) { return result; } +/** + * Check the exports of require(esm). + * TODO(joyeecheung): use it in all the test-require-module-* tests to minimize changes + * if/when we change the layout of the result returned by require(esm). + * @param {object} mod result returned by require() + * @param {object} expectation shape of expected namespace. + */ +function expectRequiredModule(mod, expectation) { + assert(isModuleNamespaceObject(mod)); + assert.deepStrictEqual({ ...mod }, { ...expectation }); +} + const common = { allowGlobals, buildType, @@ -946,6 +959,7 @@ const common = { createZeroFilledFile, defaultAutoSelectFamilyAttemptTimeout, expectsError, + expectRequiredModule, expectWarning, gcUntil, getArrayBufferViews,