diff --git a/src/esmockModule.js b/src/esmockModule.js index 31ade8f..3f28ca0 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -22,6 +22,10 @@ const isPlainObj = o => Object.getPrototypeOf(o) === objProto const iscoremodule = resolvewith.iscoremodule const isJSONExtnRe = /\.json$/i +// https://github.com/iambumblehead/esmock/issues/284 +// older applications may export names that are reserved in newer runtimes +const reservedKeywordsFoundInWild = /(^|,)static($|,)/g + // assigning the object to its own prototypal inheritor can error, eg // 'Cannot assign to read only property \'F_OK\' of object \'#\'' // @@ -112,6 +116,7 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { 'isfound=' + Boolean(fileURL), 'isesm=' + esmockModuleIsESM(fileURL), 'exportNames=' + Object.keys(def).sort().join() + .replace(reservedKeywordsFoundInWild, '') ].join('&') if (isJSONExtnRe.test(fileURL)) { diff --git a/tests/local/usesExpress.js b/tests/local/usesExpress.js new file mode 100644 index 0000000..110d800 --- /dev/null +++ b/tests/local/usesExpress.js @@ -0,0 +1,11 @@ +import express from 'express' + +export default () => { + const router = express.Router() + + router.get('/route', (req, res, next) => { + return [req, res, next] + }) + + return router +} diff --git a/tests/package.json b/tests/package.json index dc23784..75edd54 100644 --- a/tests/package.json +++ b/tests/package.json @@ -15,6 +15,7 @@ "#sub": "./local/subpath.js" }, "dependencies": { + "express": "^4.18.2", "@aws-sdk/client-s3": "^3.408.0", "babelGeneratedDoubleDefault": "file:./local/babelGeneratedDoubleDefault", "eslint": "^8.12.0", diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index c43cac1..9b78946 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -603,3 +603,24 @@ test('mocks await import node:fs/promises (global)', async () => { assert.deepStrictEqual( await main.importFSPromisesReadDir(), ['mock', 'global']) }) + +// https://github.com/iambumblehead/esmock/issues/284 +// older applications may export names that are reserved in newer runtimes +// +// express exports this... +// ```js +// exports.static = require('serve-static'); +// ``` +test('mocks express, exports disallowed reserved keyword "static"', async () => { + const calls = [] + + assert.ok(await esmock('../local/usesExpress.js', import.meta.url, { + express: { + Router: { + get: (path, fn) => { + calls.push([path, fn]) + } + } + } + })) +})