Skip to content

How to test with relative import #1285

Answered by itsmnthn
itsmnthn asked this question in Q&A
Discussion options

You must be logged in to vote

Using alias in vite config similar to tsconfig.json solves the relative import issue in tests.

// alias.ts
import { resolve } from 'path'

const r = (p: string) => resolve(__dirname, p)

export const alias: Record<string, string> = {
  '~~': r('.'),
  '~~/': r('./'),
  '@@': r('.'),
  '@@/': r('./'),
  assets: r('./assets'),
  public: r('./public'),
  'public/': r('./public/'),
}
// vitest.config.ts
import { defineConfig } from 'vite'
import { alias } from './alias'

export default defineConfig({
  root: '.',
  esbuild: {
    tsconfigRaw: '{}',
  },
  resolve: {
    alias,
  },
})

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by itsmnthn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant