Skip to content

Commit

Permalink
remove reserved keywords from serialized export names string
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Jan 14, 2024
1 parent 06973b4 commit 4e6c818
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 \'#<Object>\''
//
Expand Down Expand Up @@ -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)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/local/usesExpress.js
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
}))
})

0 comments on commit 4e6c818

Please sign in to comment.