Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repro express failing test #287

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
}
}
}))
})
Loading