Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Vitest cannot resolve imports using aliases #17685

Open
julianctni opened this issue Dec 6, 2024 · 9 comments
Open

Vitest cannot resolve imports using aliases #17685

julianctni opened this issue Dec 6, 2024 · 9 comments
Labels
area/project-creation Related to project creation process using the commands: `yarn create quasar` and `npm init quasar` bug/1-repro-available A reproduction is available and needs to be confirmed. flavour/quasar-cli-vite kind/bug 🐞 Qv2 🔝 Quasar v2 issues

Comments

@julianctni
Copy link

What happened?

Hello,

yesterday I set up a completly new and empty quasar project with app-vite 2.0. After that I added testing (vitest) via quasar ext add @quasar/testing-unit-vitest as mentioned in the documentation. However, as soon as I import something from my source folder, I get this error message running the tests.

image

It seems like a problem with the folder aliases that cannot be resolved using vitest.

I hope there is an easy/quick solution. :)

Best regards and thanks for you work!
Julian

What did you expect to happen?

The imports should be resolved.

Reproduction URL

https://stackblitz.com/edit/stackblitz-starters-khocqz

How to reproduce?

  1. Go to the reproduction link
  2. Run yarn test:unit

Flavour

Quasar CLI with Vite (@quasar/cli | @quasar/app-vite)

Areas

Project Creation (create-quasar)

Platforms/Browsers

No response

Quasar info output

No response

Relevant log output

No response

Additional context

No response

@julianctni julianctni added kind/bug 🐞 Qv2 🔝 Quasar v2 issues labels Dec 6, 2024
@github-actions github-actions bot added area/project-creation Related to project creation process using the commands: `yarn create quasar` and `npm init quasar` bug/1-repro-available A reproduction is available and needs to be confirmed. flavour/quasar-cli-vite labels Dec 6, 2024
@Smrtnyk
Copy link

Smrtnyk commented Dec 6, 2024

did you setup the same aliases in the vitest as well?

@julianctni
Copy link
Author

did you setup the same aliases in the vitest as well?

@Smrtnyk I tried for some, it worked for them, however I thought the vitest config will get that information from the generated tsconfig.json file. There are a lot of pre-defined and it feels wrong just to copy/paste these generated artifacts somewhere else.

@rstoenescu
Copy link
Member

Why copying and not just extending the same file (.quasar/tsconfig.json)?

@julianctni
Copy link
Author

Why copying and not just extending the same file (.quasar/tsconfig.json)?

@rstoenescu I am confused. How can I extend the vitest.config.mts from .quasar/tsconfig.json? The aliases in the tsconfig file are under compilerOptions.paths whereas in the vitest.config they would appear under test.resolve.alias.

@yusufkandemir
Copy link
Member

Related issues:

Workaround:

// create a file called /app-vite-testing-shim.d.ts
declare module '@quasar/app-vite/lib/testing.js' {
  import { type UserConfig } from 'vite'

  export function getTestingConfig(ctxParams?: Record<string, unknown>): Promise<UserConfig>
}
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import { quasar, transformAssetUrls } from '@quasar/vite-plugin'
import { getTestingConfig } from '@quasar/app-vite/lib/testing.js'

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  resolve: {
    alias: (await getTestingConfig()).resolve.alias,
  },
  test: {
    environment: 'happy-dom',
    setupFiles: 'test/vitest/setup-file.ts',
    include: [
      // Matches vitest tests in any subfolder of 'src' or into 'test/vitest/__tests__'
      // Matches all files with extension 'js', 'jsx', 'ts' and 'tsx'
      'src/**/*.vitest.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
      'test/vitest/__tests__/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
    ],
  },
  plugins: [
    vue({
      template: { transformAssetUrls },
    }),
    quasar({
      sassVariables: 'src/quasar-variables.scss',
    }),
  ],
}))

@yusufkandemir
Copy link
Member

I investigated this yesterday. Now, the problem should be gone with vite-tsconfig-paths v5.1.4.

But, we will also consider improving the getTestingConfig-based solution to improve other potential aspects.

@yusufkandemir
Copy link
Member

Also, next time, please use https://github.com/quasarframework/quasar-testing/issues for testing-related issues, thanks.

@julianctni
Copy link
Author

julianctni commented Dec 7, 2024

I investigated this yesterday. Now, the problem should be gone with vite-tsconfig-paths v5.1.4.

I updates vite-tsconfig-paths to v5.1.4 (see repro link), the import errors are now gone, however it now claims, that defineConfig is not a function.

image

However the getTestingConfig solution works!

@yusufkandemir
Copy link
Member

That means we should improve the getTestingConfig solution and use it. Thanks for testing and confirming!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area/project-creation Related to project creation process using the commands: `yarn create quasar` and `npm init quasar` bug/1-repro-available A reproduction is available and needs to be confirmed. flavour/quasar-cli-vite kind/bug 🐞 Qv2 🔝 Quasar v2 issues
Projects
None yet
Development

No branches or pull requests

4 participants