Vitest - @src 文件夹别名在测试文件中未解析

Eri*_*ria 34 typescript vuejs3 vite vitest

我有一个使用 Vite/Vitest 的 vue3 项目,如 Vue.js 文档中的推荐。

这是该项目的结构:

src
  components
    // my Vue components here, potentially in sub-folders. For example:
    HelloWorld.vue 
  router
    index.ts
  App.vue
  main.ts
vitest
  components
    // My test specs. For example:
    HelloWorld.spec.ts
// ...
tsconfig.app.json
tsconfig.json
tsconfig.vite-config.json
tsconfig.vitest.json
vite.config.ts
Run Code Online (Sandbox Code Playgroud)

@/文件夹的别名在src组件文件中正确解析。但是,在我的测试文件中,我收到错误:找不到模块。

例如,在HelloWorld.spec.ts

src
  components
    // my Vue components here, potentially in sub-folders. For example:
    HelloWorld.vue 
  router
    index.ts
  App.vue
  main.ts
vitest
  components
    // My test specs. For example:
    HelloWorld.spec.ts
// ...
tsconfig.app.json
tsconfig.json
tsconfig.vite-config.json
tsconfig.vitest.json
vite.config.ts
Run Code Online (Sandbox Code Playgroud)

tsconfig.app.json

import HelloWorld from '@/components/HelloWorld.vue'; // <-- error !
import { describe, it } from 'vitest';

describe('HelloWorld', () => {
  it('should day hello', () => {
    // ...
  });
});
Run Code Online (Sandbox Code Playgroud)

vite.config.js

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": [
    "env.d.ts",
    "src/**/*",
    "src/**/*.vue"
  ],
  "exclude": [
    "vitest/**/*"
  ],
  "compilerOptions": {
    "composite": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    "strict": true,
    "experimentalDecorators": true
  }
}
Run Code Online (Sandbox Code Playgroud)

Adr*_* HM 53

您只需在vitest.config.js文件中指定别名:

import path from 'path'
import vue from '@vitejs/plugin-vue'

export default {
  plugins: [vue()],
  test: {
    globals: true,
    environment: 'jsdom',
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    },
  },
}
Run Code Online (Sandbox Code Playgroud)


Ian*_*amz 6

对于 SvelteKit 用户,我遇到了类似的问题,必须添加$libvitest.config.js

import { defineConfig } from 'vitest/config';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import path from 'path';

export default defineConfig({
  plugins: [svelte({ hot: !process.env.VITEST })],
  test: {
    globals: true,
    environment: 'jsdom',
  },
  resolve: {
    alias: {
      $lib: path.resolve(__dirname, './src/lib'),
    },
  },
});
Run Code Online (Sandbox Code Playgroud)


Eri*_*ria 3

对我有用的最终解决方案是将测试文件包含在tsconfig.vitest.json

tsconfig.app.json与问题帖子相同

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": [
    "env.d.ts",
    "src/**/*",
    "src/**/*.vue"
  ],
  "exclude": [
    "vitest/**/*"
  ],
  "compilerOptions": {
    "composite": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    "strict": true,
    "experimentalDecorators": true
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.vitest.json

{
  "extends": "./tsconfig.app.json",
  "include": [
    "vitest/**/*",
  ],
  "exclude": [],
  "compilerOptions": {
    "composite": true,
    "lib": [],
    "types": [
      "node",
      "jsdom"
    ],
  }
}
Run Code Online (Sandbox Code Playgroud)