Comments (9)
@iambumblehead Sure, here's a .zip with a small project. Error should repro when you run test
.
esmock-unexpected-token-delete.zip
from esmock.
Thank you for looking at this, After looking at your replies, I managed to work around the issue by doing something like the following:
import esmock from "esmock";
await esmock("../src/to_test.js", {
"../src/some_module.js": {
default: {
delete: () => {
console.log("Oh no, I've been mocked.");
},
},
},
});
The original code (not the test package I uploaded, but the actual code I'm working on) exported an object which had the delete
method in it, and when mocked (by giving ESMock an object with a delete
method as the mock) it would be interpreted as if it were exported separately, i.e. export const delete
as you mentioned. What I was expecting was that the object I'm passing as the mock be interpreted as the default export, which was a misunderstanding I had.
When I made the test package I got the export scenario wrong, which didn't help me in illustrating the problem. 😅 Instead of exporting contents
it should have been something like:
export default {
delete: () => {
console.log("deleted");
},
};
I hope this helps anyone encountering this problem in the future.
from esmock.
Have you tried wrapping the property name? I'm not entirely sure if this is specifically an issue with esmock
, and more so JavaScript not liking the fact that you're trying to use a keyword when its expecting to see an object member identifier.
Try this instead:
await esmock('./path/to/module.js', {
'./path/to/dependency.js': { 'delete': () => {} }
})
Please close the issue if this resolved the problem! 🙂
from esmock.
Unfortunately, that didn't work. I've tried wrapping the function name in a variety of ways and it didn't solve the problem.
I haven't encountered this problem with other mocking frameworks, or with the native ESM loader, so I don't believe it's a problem with JavaScript. If it were a problem with JavaScript, Express.js would be the first to raise some issues because they have delete
functions, as do many other frameworks that could be mocked.
from esmock.
@mcbalomirean if you could possibly make a small reproduction demonstration, that would be really great an helpful
from esmock.
could try to make a reproduction myself later but might need to do so on Friday or Saturday (busy today)
from esmock.
@mcbalomirean thanks that's a terrific reproduction
from esmock.
@mcbalomirean looking at the test package you uploaded... the mock definition there is not correct. The original file "some_module.js" exports the name "contents" and does not export "delete". Try mocking "contents" using changes in this diff
import esmock from "esmock";
await esmock("../src/to_test.js", {
"../src/some_module.js": {
+ contents: {
delete: () => {
console.log("Oh no, I've been mocked.");
},
+ }
},
});
from esmock.
exporting "delete" is disallowed by node. closing this issue but if there is a misunderstanding or some reason to reopen feel free to reopen
$ echo "export const delete = 1" >> test.mjs && node test.mjs
file:///home/bumble/test.mjs:1
export const delete = 1
^^^^^^
SyntaxError: Unexpected token 'delete'
at DefaultModuleLoader.moduleStrategy (node:internal/modules/esm/translators:116:18)
at DefaultModuleLoader.moduleProvider (node:internal/modules/esm/loader:206:14)
at async link (node:internal/modules/esm/module_job:67:21)
Node.js v20.2.0
from esmock.
Related Issues (20)
- `invalid moduleId` in a TypeScript monorepo HOT 35
- yarn pnp issues HOT 14
- Is it possible to mock json imports with assert? HOT 6
- Support Yarn PnP resolver out of the box HOT 11
- Get rid of old Node versions support HOT 8
- resolve error introduced with 2.5.2 HOT 10
- Error when mocking a file that exports an array. HOT 1
- c8 incorrect branch coverage; modules and `import` globals HOT 11
- how to mock globalThis HOT 4
- import('node:fs/promises') called within a library does not seem to be replaced HOT 30
- Path alias support HOT 1
- Unexpected strict mode reserved word syntax error when mocking express HOT 2
- incompatible with eslint-plugin-import HOT 3
- No support for subpath patterns? HOT 14
- Version 2.6.3 changed the way to mock libraries HOT 7
- ESMock seems to wrongfully alter Sinon stubs. HOT 1
- Using `import:` to mock globals messes up coverage (with c8) HOT 7
- [Question] Is it possible to mock indirect imports? HOT 10
- Scoped mocks failing with pnpm but working with npm HOT 9
- esmock static import for classes not matching HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esmock.