vitest tsconfig path failed to load url in node js (express)

22 views Asked by At

I would like to perform some unit testing in Node.js (Express) using Vitest. I'm wondering if there might be a conflict between the two, as I have installed both tsconfig-path and vite-tsconfig-path.

All my files with import paths like @/controller/auth.controller.ts work fine, and the server runs smoothly. However, I'm encountering a problem only with the testing files. It says:

Error: Failed to load url @/path/to/file.ts (resolved id: @/path/to/file.ts) in C:/error/occur/to/this/file. Does the file exist?

folder structure:

my-project/
│
├── server/     
│   ├── dist             
│   ├── node_modules/         
│   ├── src/      
│   │   ├── __test__/             
│   │   ├── controllers/      
│   │   ├── models/           
│   │   ├── routes/           
│   │   ├── utils/            
│   │   ├── index.js            
│   │   └── vitest.config.ts          
│   ├── package.json          
│   ├── package-lock.json     
│   └── tsconfig.json
│
├── client/                 
│   ├── node_modules/         
│   ├── public/               
│   ├── src/                  
│   │   ├── components/       
│   │   ├── pages/            
│   │   ├── utils/             
│   │   ├── app.js
│   │   └── index.js        
│   ├── package.json          
│   ├── package-lock.json     
│   └── ...

tsconfig.json

{
  "extends": "ts-node/node16/tsconfig.json",
  "ts-node": {
    "transpileOnly": true,
    "files": true,
  },
  "compilerOptions": {
    /* Base Options: */
    "target": "es2022",
    "moduleDetection": "force",
    "moduleResolution": "node",
    "module": "CommonJS",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "resolveJsonModule": true,
    "lib": ["es2022"],
    "types": ["vitest/globals"],

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,

    /* Module Resolution Options: */
    "rootDir": "src",
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*",
      ]
    }
  },
  "exclude": ["node_modules"]
}

vitest.config.ts

import tsconfigPaths from "vite-tsconfig-paths";
import { defineConfig, configDefaults } from "vitest/config";
import path from "path";

const config = defineConfig({
  base: ".",
  resolve: {
    alias: [
      {
        find: "@",
        replacement: path.resolve(__dirname, "./src"),
      },
    ],
  },
  test: {
    ...configDefaults,
    globals: true,
  },
  plugins: [tsconfigPaths({
    loose: true
  })],
});

export default config;

package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "commonjs",
  "scripts": {
    "test": "vitest run",
    "test:watch": "vitest",
    "test:coverage": "vitest run --coverage",
    "build": "tsc",
    "start:dev": "SET NODE_ENV=development && ts-node-dev --watch -r tsconfig-paths/register src/index.ts"
  },
  ...
}

auth.spec.ts

import UserRepository                                from "@/repository/user/user.repository.impl";
import { describe, it, expect, vi, Mock, afterEach } from "vitest";

describe("UserRepository", () => {
  test("should create an instance of UserRepository", () => {
    const userRepository: UserRepository = new UserRepository();
    expect(userRepository).toBeInstanceOf(UserRepository);
  });
});
0

There are 0 answers