// @ts-ignore
import dotenv from 'dotenv'
import path from 'path';
/**
* 初始化测试环境变量
*/
export const initTestEnv = () => {
const __dirname = path.dirname(import.meta.url);
const envPath = path.relative(process.cwd(), path.join(__dirname, '../.env.development.local')).replace("file:\\", "")
// console.log(envPath)
dotenv.config({path: envPath});
// console.log(process.env.VITE_LIANDI_API_URL)
console.log("env loaded.")
}
另外 ts-node测试单文件的方法
"test-script": "node --experimental-specifier-resolution=node --loader ts-node/esm test/test.ts",
"testEsm-script": "ts-node-esm --experimental-specifier-resolution=node test/test.ts",
标签:node,变量,ts,test,dotenv,path,dirname,加载
From: https://www.cnblogs.com/tangyouwei/p/dotenv-load-variable-z2tw9iy.html