Skip to content
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

Closed
cawa-93 opened this issue Aug 24, 2022 · 13 comments · Fixed by #127
Closed

Can't mock package 'vue' if it doesn't installed #126

cawa-93 opened this issue Aug 24, 2022 · 13 comments · Fixed by #127

Comments

@cawa-93
Copy link

cawa-93 commented Aug 24, 2022

My case: I have generated package like below:

import {h} from 'vue';
export const ComponentName = () => h('svg', { /* some properties */ });

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 from vue

// test.spec.js
const component= await esmock(`../component.js`, {}, {
  vue: {
    h: (...args) => args
  }
})

It works great. But, only if the vue is installed as a dependency. Because in reality no vue functionality is used anywhere, I tried to remove this dependency, but then I get the error:

not a valid path: "vue" 

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

  1. Clone repo: git clone https://github.com/cawa-93/iconify-prerendered.git
  2. Delete vue dependency: npm un vue
  3. Install others dependencies: npm ci
  4. Generate code for testing: npm run build
  5. Run test: npm run test
@iambumblehead
Copy link
Owner

this is an interesting scenario I did not think of @cawa-93

@iambumblehead
Copy link
Owner

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

@iambumblehead
Copy link
Owner

iambumblehead commented Aug 24, 2022

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

@cawa-93
Copy link
Author

cawa-93 commented Aug 24, 2022

@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);
}

@iambumblehead
Copy link
Owner

@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?

@iambumblehead
Copy link
Owner

@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.

@iambumblehead
Copy link
Owner

the soonest I could look into this myself is maybe 7 or 8 hours from now

@cawa-93
Copy link
Author

cawa-93 commented Aug 24, 2022

Ok. I will try to make pr ASAP. A few days, I think

@iambumblehead
Copy link
Owner

@cawa-93 if there is a PR with passing tests it can be merged and published as soon as its ready

@cawa-93
Copy link
Author

cawa-93 commented Aug 24, 2022

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.

@iambumblehead
Copy link
Owner

@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

@cawa-93
Copy link
Author

cawa-93 commented Aug 25, 2022

@iambumblehead It's work like a charm ✨ Thanks
Did you forget to add a new isPackageNotFoundError parameter to the type declaration?

esmock/src/esmock.d.ts

Lines 22 to 25 in 968b357

interface Options {
partial?: boolean | undefined;
purge?: boolean | undefined;
}

@iambumblehead
Copy link
Owner

@cawa-93 yes I did forget

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants