-
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
Can't mock package 'vue' if it doesn't installed #126
Comments
this is an interesting scenario I did not think of @cawa-93 |
I think esmock cannot practically solve this issue. node's native module resolution will throw an error when it fails to locate the package. esmock's loader only return a different 'vue' definition, but the 'vue' package would still need to be present import {h} from 'vue'; something esmock does to test various test-runners and packages is, setup special test directories where packages like vue are downloaded and installed. eg like this https://github.com/iambumblehead/esmock/blob/master/tests/tests-jest/package.json this folder downloads jest and related things specifically for unit-tests |
another suggestion one could try, write a file like this before running the test "./node_modules/vue/index.js" this would probably be resolved as a cjs module, by node, which would make the error go away |
@iambumblehead This loader works for me in node v16.14.0 import module from 'module'
// Resolve unexisting package
export async function resolve(specifier, context, nextResolve) {
if (specifier === 'vue') {
return {
shortCircuit: true,
url: specifier
};
}
return nextResolve(specifier, context);
}
// Load unexisting package
export function load(url, context, nextLoad) {
if (url === 'vue') {
return {
format: 'module',
shortCircuit: true,
source: `export const h = (...args) => args`, // <- moke code
}
}
return nextLoad(url, context);
} |
@cawa-93 awesome ok let's try to update esmock. Do you need a solution now or is it ok if day or two go by before changes are made? |
@cawa-93 thanks for ignoring my earlier statements and finding that solution. If you're interested to try making a PR, from esmock's root folder use npm install && npm run test:install to fetch all dependencies needed, then from whichever test folder is being used, npm test will run tests. |
the soonest I could look into this myself is maybe 7 or 8 hours from now |
Ok. I will try to make pr ASAP. A few days, I think |
@cawa-93 if there is a PR with passing tests it can be merged and published as soon as its ready |
After thinking again, I think I won't find enough time to figure it out and make a change. In any case, you can do it faster (although, personally, these changes are not urgent for me). Therefore, I will leave PR to you. Thanks again for the quick response. |
@cawa-93 please confirm if this resolves the issue for you https://github.com/iambumblehead/esmock/releases/tag/v1.9.7 if you think the interface should be changed or have any criticisms or suggestions feel free to give your opinion |
@iambumblehead It's work like a charm ✨ Thanks Lines 22 to 25 in 968b357
|
@cawa-93 yes I did forget |
My case: I have generated package like below:
And I want to test this package. The point of the test is just to check that function
h
was called with the correct parameters.So I tried mock
h
function fromvue
It works great. But, only if the
vue
is installed as a dependency. Because in reality novue
functionality is used anywhere, I tried to remove this dependency, but then I get the error:What can I do about it? I wouldn't want to install an entire framework just to mock a single function in tests.
You can reproduce the problem
git clone https://github.com/cawa-93/iconify-prerendered.git
vue
dependency:npm un vue
npm ci
npm run build
npm run test
The text was updated successfully, but these errors were encountered: