Skip to content

Instantly share code, notes, and snippets.

@BoscoDomingo
Created April 25, 2024 13:44
Show Gist options
  • Save BoscoDomingo/a06329ca1a2f36ccb7c168d4a8de6c92 to your computer and use it in GitHub Desktop.
Save BoscoDomingo/a06329ca1a2f36ccb7c168d4a8de6c92 to your computer and use it in GitHub Desktop.
Set up Node Test Runner with TypeScript and ENV variables
EXAMPLE_SECRET="12345"
EXAMPLE_SECRET="6789" # Overwrites the .env one

Install

# Use whatever package manager you prefer
pnpm add -D dotenv-cli tsx glob

# For a single app in a monorepo
# pnpm add -F your-app -D dotenv-cli tsx glob

# For the global package.json in a monorepo
# pnpm add -wD dotenv-cli tsx glob

For a detailed guide, check my article (100% free, forever)

"scripts": {
//...
"test": "dotenv -e .env -e env.test -- glob -c \"tsx --test --test-reporter spec \" \"./test/**/*.test.ts\"",
"build": "tsc -p tsconfig.build.json",
//...
}
{
"extends": "./tsconfig.json", // The file above. You can also just extend the base `tsconfig.json` (in the root of the monorepo) instead, adding `include: [ "./src/**/*"]`, or write the `compilerOptions` here
"compilerOptions": {
"noEmit": false,
"outDir": "./dist",
},
"exclude": [ // This way we avoid compiling tests into JS
"**/*.spec.ts",
"**/*.test.ts"
]
}
{
"extends": "../../tsconfig.json", // Since I work in a monorepo, this one contains all generic compiler options. You can write them in this file if you prefer
"compilerOptions": { // These are for shorter, non-relative imports in tests. Completely optional
"baseUrl": "./",
"paths": {
"@src/*": ["./src/*"],
},
"noEmit": true, // Avoid compiling by mistake
},
"include": ["./src/**/*", "./test/**/*"], // Important so tests also benefit from TS support
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment