Comments (16)
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.
Nice find.
from esmock.
@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.
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,
- current tests folders https://github.com/iambumblehead/esmock/tree/master/tests
- https://github.com/iambumblehead/esmock/blob/master/CONTRIBUTING.md
from esmock.
it would be awesome if you would submit a PR with a failing test
from esmock.
Added failing tests at #210.
from esmock.
@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.
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.
PR is here #211
from esmock.
this issue seems related avajs/ava#2593 (comment)
from esmock.
Potentially related too: avajs/ava#3213 (reply in thread)
from esmock.
these results indicate the problem may come from tsx,
- if tsx is used, the tests fail if either
node:test
orava
are used as the test runner - if ts-node/esm is used, the tests pass if either
node:test
orava
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.
@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.
related issue is opened here esbuild-kit/tsx#264
from esmock.
Closing with this #211
A tsx note is embedded to the README scripts example. Feel free to reopen for any reason.
from esmock.
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)
- Issues with pnpm HOT 8
- strictest silently allows to pass {} or undefined as defs -> real dependencies are used HOT 1
- Is it possible to mock `process.cwd()`? HOT 6
- Overriding `globalThis` per mock HOT 6
- Node 20.x: import.meta.resolve is not a promise anymore HOT 1
- Unexpected token 'delete' HOT 9
- Mock a package globally HOT 1
- Add factory method(s) to avoid code duplication / custom wrappers HOT 4
- Mock dependency HOT 8
- Is it possible to mock a binary's imports? HOT 6
- Mocking a file with a hashbang using the `import` key fails HOT 7
- Error when using `import` key with modules that have CJS imports HOT 9
- 2.3.4 - 'RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: undefined' HOT 8
- Breakage in Node.js v20.6.0 HOT 18
- `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
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.