GithubHelp home page GithubHelp logo

Comments (9)

mcbalomirean avatar mcbalomirean commented on June 11, 2024 1

@iambumblehead Sure, here's a .zip with a small project. Error should repro when you run test.

image

esmock-unexpected-token-delete.zip

from esmock.

mcbalomirean avatar mcbalomirean commented on June 11, 2024 1

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.

Swivelgames avatar Swivelgames commented on June 11, 2024

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.

mcbalomirean avatar mcbalomirean commented on June 11, 2024

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.

iambumblehead avatar iambumblehead commented on June 11, 2024

@mcbalomirean if you could possibly make a small reproduction demonstration, that would be really great an helpful

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

could try to make a reproduction myself later but might need to do so on Friday or Saturday (busy today)

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

@mcbalomirean thanks that's a terrific reproduction

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

@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.

iambumblehead avatar iambumblehead commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.