vuejs / vue-cli

🛠️ webpack-based tooling for Vue.js Development
https://cli.vuejs.org/
MIT License
29.75k stars 6.32k forks source link

Jest not working with custom paths in tsconfig #5879

Open Mikilll94 opened 4 years ago

Mikilll94 commented 4 years ago

Version

4.5.6

Environment info

System:
    OS: macOS 10.15.4
    CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
  Binaries:
    Node: 12.16.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  Browsers:
    Chrome: 85.0.4183.102
    Edge: Not Found
    Firefox: Not Found
    Safari: 13.1
  npmPackages:
    @vue/babel-helper-vue-jsx-merge-props:  1.0.0 
    @vue/babel-helper-vue-transform-on:  1.0.0-rc.2 
    @vue/babel-plugin-jsx:  1.0.0-rc.3 
    @vue/babel-plugin-transform-vue-jsx:  1.1.2 
    @vue/babel-preset-app:  4.5.6 
    @vue/babel-preset-jsx:  1.1.2 
    @vue/babel-sugar-functional-vue:  1.1.2 
    @vue/babel-sugar-inject-h:  1.1.2 
    @vue/babel-sugar-v-model:  1.1.2 
    @vue/babel-sugar-v-on:  1.1.2 
    @vue/cli-overlay:  4.5.6 
    @vue/cli-plugin-babel: ~4.5.0 => 4.5.6 
    @vue/cli-plugin-eslint: ~4.5.0 => 4.5.6 
    @vue/cli-plugin-router:  4.5.6 
    @vue/cli-plugin-typescript: ~4.5.0 => 4.5.6 
    @vue/cli-plugin-unit-jest: ~4.5.0 => 4.5.6 
    @vue/cli-plugin-vuex:  4.5.6 
    @vue/cli-service: ~4.5.0 => 4.5.6 
    @vue/cli-shared-utils:  4.5.6 
    @vue/component-compiler-utils:  3.2.0 
    @vue/eslint-config-airbnb: ^5.0.2 => 5.1.0 
    @vue/eslint-config-typescript: ^5.0.2 => 5.1.0 
    @vue/preload-webpack-plugin:  1.1.2 
    @vue/test-utils: ^1.0.3 => 1.1.0 
    @vue/web-component-wrapper:  1.2.0 
    eslint-plugin-vue: ^6.2.2 => 6.2.2 
    jest-serializer-vue:  2.0.2 
    typescript: ~3.9.3 => 3.9.7 
    vue: ^2.6.11 => 2.6.12 
    vue-eslint-parser:  7.1.0 
    vue-hot-reload-api:  2.3.4 
    vue-jest:  3.0.7 
    vue-loader:  15.9.3 (16.0.0-beta.7)
    vue-style-loader:  4.1.2 
    vue-template-compiler: ^2.6.11 => 2.6.12 
    vue-template-es2015-compiler:  1.9.1 
  npmGlobalPackages:
    @vue/cli: 4.5.6

Steps to reproduce

  1. Run unit tests in this repro project (npm run test:unit) unit-tests-typescript.zip

  2. The following error pops up: Screenshot 2020-09-14 at 23 27 28

What is expected?

There should be no error.

What is actually happening?

An error pops up.


In tsconfig.json I have added a custom "path" like this:

Screenshot 2020-09-14 at 23 38 14

Then in my unit test, I import another file like this:

Screenshot 2020-09-14 at 23 39 10

For VSCode this import is fine. It does not highlight any errors. However, when running unit tests with Jest, this import fails.

haoqunjiang commented 4 years ago

Path mapping needs to be set separately in the Jest config https://kulshekhar.github.io/ts-jest/user/config/#paths-mapping

But it's a nice feature to add in the built-in preset. Thanks for noting this!