diff --git a/lib/utils.js b/lib/utils.js index 9db34cb1ad..5b99aa6b92 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -215,7 +215,7 @@ exports.type = function type(value) { exports.stringify = function (value) { var typeHint = canonicalType(value); - if (!~['object', 'array', 'function'].indexOf(typeHint)) { + if (!~['object', 'array', 'function', 'module'].indexOf(typeHint)) { if (typeHint === 'buffer') { var json = Buffer.prototype.toJSON.call(value); // Based on the toJSON result @@ -401,6 +401,12 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) { break; } /* falls through */ + case 'module': + if (value[Symbol.toStringTag] === 'Module') { + canonicalizedObj = canonicalizedObj || {}; + canonicalizedObj['[Symbol.toStringTag]'] = 'Module'; + } + /* falls through */ case 'object': canonicalizedObj = canonicalizedObj || {}; withStack(value, function () { diff --git a/test/unit/fixtures/module.mjs b/test/unit/fixtures/module.mjs new file mode 100644 index 0000000000..bec42311d7 --- /dev/null +++ b/test/unit/fixtures/module.mjs @@ -0,0 +1,4 @@ +export default 123; + +export const foo = 'abc'; +export const bar = true; diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index 336bb1698e..2e0c1517a3 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -2,6 +2,8 @@ 'use strict'; var utils = require('../../lib/utils'); +const esmUtils = require('../../lib/nodejs/esm-utils'); +const Path = require('node:path'); var sinon = require('sinon'); describe('lib/utils', function () { @@ -288,6 +290,22 @@ describe('lib/utils', function () { ].join('\n'); expect(stringify(expected), 'to be', actual); }); + + it('should represent modules', async function () { + const expected = await esmUtils.requireOrImport( + Path.join(__dirname, './fixtures/module.mjs') + ); + const actual = [ + '{', + ' "[Symbol.toStringTag]": "Module"', + ' "bar": true', + ' "default": 123', + ' "foo": "abc"', + '}' + ].join('\n'); + + expect(stringify(expected), 'to be', actual); + }); }); it('should canonicalize the object', function () {