question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't resolve with babel-plugin-module-resolver files in SFCs

See original GitHub issue

A component that imports from an alias with babel-plugin-module-resolver will fail to resolve in jest. Seems like it’s not resolving ./ as the babel cwd, but from the component location.

Example

.babelrc:

{
  "plugins": [
    ["module-resolver", { "alias": { "@": "src/components" } } ]
  ]
}

src/components/A.vue:

<template>
  <p>{{ text }}</p>
</template>

<script>
export default {
  data() {
    return {
      text: 'hi'
    }
  }
};
</script>

src/components/B.vue:

<template>
  <ComponentA />
</template>

<script>
import ComponentA from '@/A.vue';

export default {
  components: { ComponentA }
};
</script>

Result:

  ● Test suite failed to run

    Cannot find module './src/components/A.vue' from 'B.vue'
      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
      ...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
txantslusamcommented, Oct 26, 2017

Can confirm this bug. I have a component, which import another one from different directory. Importing this component with @ alias results in: Cannot find module './src/components/partials/B' from 'A.vue' But when i change import path to relative path, eg. '../../components/partials/B' it resolves correctly. I added moduleNameMapper to Jest and configure babel alias.

1reaction
pmesgaricommented, Nov 10, 2017

Hi @eddyerburgh As we spoke before, today I started working on debugging this issue. So far I have found out that using alias in .babelrc file will result in jest to look for the module as a hastePackage which results in the error. Without the alias nothing is being searched for as a hastePackage.

I am looking to find out what is a hastePackage in jest, I was wondering if you or someone else has any ideas on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-plugin-module-resolver not working on .jsx files
The module-resolver is able to resolve paths on .ts files but when it comes to render the first Component (.jsx) it shows me...
Read more >
babel-plugin-module-resolver - npm
A Babel plugin to add a new resolver for your modules when compiling your code using Babel. This plugin allows you to add...
Read more >
can't resolve 'fs' webpack 5 | The AI Search Engine You Control
Module not found: Error: Can't resolve 'fs' with webpack ... The .js file's loader is very comprehensive that covers node, web, web worker,...
Read more >
Vue Test Utils and Jest: how to write simple unit tests for Vue ...
That's where babel comes in. There's a plugin — babel-plugin-module-resolver — that resolves aliases in babel.
Read more >
Why You Should Use Babel Resolvers | by Izaak Schroeder
doesn't really tell you where the import lives, other than it's far away. ... There is a babel plugin for doing custom module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found