-
Notifications
You must be signed in to change notification settings - Fork 19
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
[Question] Is it possible to mock indirect imports? #300
Comments
do one of these work for you? // mock deep-function.js' 'deepFun' only for
// function.js -> more-fuction.js -> deep-function.js
const { impFun } = await esmock('./function.js', {
'./more-function.js': await esmock('./more-function.js', {
'./deep-function.js': {
deepFun: () => "mocked"
}
})
}) // globally mock deep-function.js' 'deepFun'
const { impFun } = await esmock('./function.js', null, {
'./deep-function.js': {
deepFun: () => "mocked"
}
}) |
closing, feel free to reopen for any reason |
Thank you @iambumblehead . I can confirm that both approaches work! |
Hi @iambumblehead I have a slight follow up question: Let's say I have
And I would like to mock
It's probably a beginner's mistake 😄 |
It does something like this function.js -import deep from './deep-function.js'
+import deep from './deep-function.js?with-mocked-definitions'
deep('is mocked') |
Probably esmock is difficult to understand. A review I found on the internet recently https://adamtuttle.codes/blog/2024/mocking-esm-dependencies-for-tests/ writes,
Thanks for using esmock despite difficulties. esmock tries to be as useful and practical as possible. |
Thanks for explaining @iambumblehead I was thinking, maybe I can do it by adding a level of indirection? E.g. adding a Something like this
And then use it something like this:
|
@bosschaert this test resembling yours passes here #301 |
Thanks for your explanations @iambumblehead now that I know the boundaries of esmock I am able to use it effectively. Kudos to the project! |
@bosschaert thanks :) |
Lets say my JS source has
function.js
:then I have
more-function.js
:and
deep-function.js
:Then in my unit test I would like to mock the
deepFun()
that is indirectly importedBut this doesn't work. I get a
SyntaxError: Unexpected token '.' at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:168:18)
Is something like this possible with esmock?
The text was updated successfully, but these errors were encountered: