-
Notifications
You must be signed in to change notification settings - Fork 18
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
invalid moduleId
error when using tsx
#209
Comments
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, |
it would be awesome if you would submit a PR with a failing test |
Added failing tests at #210. |
@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 |
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 |
PR is here #211 |
this issue seems related avajs/ava#2593 (comment) |
Potentially related too: avajs/ava#3213 (reply in thread) |
these results indicate the problem may come from tsx,
I'll push up some changes to the PR that make it a little easier to switch between the two transpilers and test-runners |
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. |
@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 |
related issue is opened here privatenumber/tsx#264 |
invalid moduleId
error when trying to use with TypeScript and AVAinvalid moduleId
error when using tsx
Nice find. |
Closing with this #211 A tsx note is embedded to the README scripts example. Feel free to reopen for any reason. |
I get the same invalid moduleId error for
uvu runner |
@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 |
I'm trying to use
esmock
with AVA and TypeScript via transpilation withtsx
(i.e.esbuild
) on Node.js v18.16.1. My loader chain is effectivelyAVA configuration
Project structure
When trying to mock my source file, I get an
invalid moduleId
error:Am I missing something?
The text was updated successfully, but these errors were encountered: