Skip to content

Tests failing when importing components using absolute path #25

@afontcu

Description

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

Current behavior
Tests fail when absolute path import is used

Expected behavior
All tests to pass (as they do when I replace the absolute path with the relative equivalent one).

Minimal reproduction of the problem with instructions
Just running tests with some component import. Only when using relative paths the test is able to import them:

import Gradient from '@/components/Gradient' <--- this does not work import Gradient from 'src/components/Gradient' <--- this does not work import Gradient from '../components/Gradient' <--- this does

Console output:

 FAIL src/components/Background/background.test.js ● Test suite failed to run Cannot find module '@/components/Gradient' from 'Background.vue' at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17) at src/components/Background/Background.vue:167:17 at Object.<anonymous> (src/components/Background/Background.vue:210:3)

Obviously, my import Component from @/components/Whatever works properly in my app.

Just in case, my jest object config from package.json:

 "jest": { "moduleFileExtensions": ["js", "vue"], "moduleNameMapper": { "src/components/([^\\.]*)$": "<rootDir>/src/components/$1.vue", "^vue$": "vue/dist/vue.common.js", "src/([^\\.]*)$": "<rootDir>/src/$1.vue", "(.*)/(.*)$": "$1/$2.vue" }, "snapshotSerializers": [ "jest-serializer-html" ], "collectCoverageFrom": [ "src/**/*.{js,jsx}" ], "transform": { "^.+\\.js$": "<rootDir>/node_modules/babel-jest", ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor" } }

Tried adding a new line in ModuleNameMapper: "@/([^\\.]*)$": "<rootDir>/src/$1.vue", but did not work. I'm not sure if that even make sense :)

I'm using vue-cli, so in webpack.base.conf.js there's the @ alias setup:

... resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src') } }

Please tell us about your environment:

  • jest-vue-preprocessor: 1.0.1
  • Node version : 8.1.2 (npm 5.3.0)

  • Platform: OSX

Thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions