Skip to content

Commit

Permalink
Merge pull request #287 from iambumblehead/repro-express-failing-test
Browse files Browse the repository at this point in the history
Repro express failing test
  • Loading branch information
iambumblehead authored Jan 14, 2024
2 parents 06973b4 + 1168a89 commit 26cfb9b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# changelog

* 2.6.1 _Jan.14.2023_
* 2.6.2 _Jan.14.2023_
* [remove reserved keywords](https://github.com/iambumblehead/esmock/pull/287) from export names lists, allows express to be mocked, thanks @lcapel
* 2.6.1 _Jan.13.2023_
* [update ci job to use checkout v4](https://github.com/iambumblehead/esmock/pull/279)
* [update README to work w/ eslint-plugin-markdown](https://github.com/iambumblehead/esmock/pull/275)
* [update eslint](https://github.com/iambumblehead/esmock/pull/276) disallow trailing whitespace
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esmock",
"type": "module",
"version": "2.6.1",
"version": "2.6.2",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import and globals mocking for unit tests",
Expand Down
6 changes: 6 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,8 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => {
'isfound=' + Boolean(fileURL),
'isesm=' + esmockModuleIsESM(fileURL),
'exportNames=' + Object.keys(def).sort().join()
.replace(reservedKeywordsFoundInWild, a => (
a.startsWith(',') && a.endsWith(',') ? ',' : ''))
].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 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 26cfb9b

Please sign in to comment.