GithubHelp home page GithubHelp logo

Comments (16)

iambumblehead avatar iambumblehead commented on June 5, 2024 1

At the PR, I removed the invalid moduleId test, because the moduleId was actually invalid (the file path being imported did not exist) and esmock is expected to throw an error in that situation. That is separate from the unknown-file-extension-error which seems to be a real issue.

from esmock.

tommy-mitchell avatar tommy-mitchell commented on June 5, 2024 1

Nice find.

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024 1

@tonisives respectfully, the example you gave probably does not use esmock correctly. Call esmock to mock values that are imported by the target module. See this diff, for example, which results in a passing test using the two files attached further below.

diff --git a/src/simpletest.test.ts b/src/simpletest.test.ts
index a1633c6..13a3370 100644
--- a/src/simpletest.test.ts
+++ b/src/simpletest.test.ts
@@ -1,13 +1,14 @@
 import { test } from "uvu"
 import esmock from "esmock"
-import { myFun } from "./simpletest.js"
+import simpletest from "./simpletest.js"
 
 test("test not real", async () => {
   let mock = await esmock("./simpletest.js", {
-    myFun: () => console.log("mocked")
+    "./simplefunctions.js": { myFun: () => console.log("mocked") }
   })
-  
-  myFun()
+
+  mock()
+  simpletest()
 })
 
 test.run()

src/simplefunctions.ts

export const myFun = () => {
  console.log("still real");
}

src/simpletest.ts

import { myFun } from './simplefunctions.js'

export default myFun

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

There's not an esmock tsx test and it would be great to have one. Feel free to submit a PR, otherwise I'll respond in a week or so with a solution.

related,

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

it would be awesome if you would submit a PR with a failing test

from esmock.

tommy-mitchell avatar tommy-mitchell commented on June 5, 2024

Added failing tests at #210.

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

@tommy-mitchell thank you for the test cases. Possibly related, ava is not fully compatible with esm --loader

Will take a look later this evening

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

this seems to come from a configuration issue, such as the configurations discussed here https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension

the error occurs at this line, where importing the ts file causes the error

const importedModule = await import(fileURLKey)
//   TypeError {
//     code: 'ERR_UNKNOWN_FILE_EXTENSION',
//     message: 'Unknown file extension ".ts" for /home/bumble/software/esmock/tests/local/main.ts',

I'm not great at setting up configurations for things like this but will keep trying a little bit

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

PR is here #211

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

this issue seems related avajs/ava#2593 (comment)

from esmock.

tommy-mitchell avatar tommy-mitchell commented on June 5, 2024

Potentially related too: avajs/ava#3213 (reply in thread)

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

these results indicate the problem may come from tsx,

  • if tsx is used, the tests fail if either node:test or ava are used as the test runner
  • if ts-node/esm is used, the tests pass if either node:test or ava are used as the test runner

I'll push up some changes to the PR that make it a little easier to switch between the two transpilers and test-runners

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

@tommy-mitchell I think I've narrowed down the issue to something that could be used to submit an issue at the tsx project,

test('tsx should not break when import paths include query params', async () => {
  assert.ok(await import('../local/main.ts?n=4'))
})

when query params are removed, the above test passes

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

related issue is opened here esbuild-kit/tsx#264

from esmock.

iambumblehead avatar iambumblehead commented on June 5, 2024

Closing with this #211

A tsx note is embedded to the README scripts example. Feel free to reopen for any reason.

from esmock.

tonisives avatar tonisives commented on June 5, 2024

I get the same invalid moduleId error for node --loader=ts-node/esm src/simpletest.test.ts

import { test } from "uvu"
import esmock from "esmock"
import { myFun } from "./simpletest.js"


test("test not real", async () => {
  let mock = await esmock("./simpletest.js", {
    myFun: () => console.log("mocked")
  })
  
  myFun()
})


test.run()
export const myFun = () => {
  console.log("still real");
}
➜ node --loader=ts-node/esm src/simpletest.test.ts
(node:5062) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
✘   (0 / 1)

   FAIL  "test not real"
    invalid moduleId: "myFun" (used by file:///Users/tonis/workspace/js/audit-hero/parsers/packages/findings/src/simpletest.test.ts)

uvu runner
"ts-node": "^10.9.1",
node 20.6.1
"uvu": "^0.5.6"

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.