jest-vue-preprocessor icon indicating copy to clipboard operation
jest-vue-preprocessor copied to clipboard

Relative imports in multi-file-components do not work in subfolders

Open kiyui opened this issue 7 years ago • 0 comments
trafficstars

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request

Current behavior

Multiple file components such as components/HelloWorld/ cannot import files relatively in jest if the script is in a subfolder:

<script src="./script/index.js"></script>

Because inside the script, if we do the following:

import { greeter } from './lib/something'

Jest will throw the following error looking for lib/something because it looks for the file relative to index.vue instead of script/index.js.

 FAIL  test/unit/specs/HelloWorld.spec.js
  ● Test suite failed to run

    Cannot find module './lib/something' from 'index.vue'
      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
      at src/components/HelloWorld/index.vue:8:18
      at Object.<anonymous> (src/components/HelloWorld/index.vue:18:3)

Current fix is to not put index.js inside a subfolder. Alternatively, the following can be done:

<script>
import Component from './script/index';
export default Component;
</script>

Expected behavior

Files imported inside a script should be relative to the script path instead of the index.vue file. The workaround shouldn't be necessary (or should be documented).

Minimal reproduction of the problem with instructions I've created a test repo here

  • Do npm install
  • Running npm run start works just fine
  • Running npm run test will throw the error

(I hope this is sufficient, and that my suspicion for origin of behaviour is correct)

What is the motivation / use case for changing the behavior?

Avoid using the workaround as it works as is with Webpack.

Please tell us about your environment:

  • jest-vue-preprocessor: 1.1.X
    "jest-vue-preprocessor": "git+https://github.com/vire/jest-vue-preprocessor.git",
  • Node version : [ OSX | Linux | Windows ] v9.3.0

  • Platform: [ OSX | Linux | Windows ] Linux

kiyui avatar Jan 02 '18 08:01 kiyui