Skip to content

Instantly share code, notes, and snippets.

@BoscoDomingo
Last active July 2, 2024 10:47
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 tsx glob

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

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

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

"scripts": {
//...
"test": "glob -c \"tsx --env-file .env --env-file .env.test --test --test-reporter spec \" \"./test/**/*.test.ts\"",
"build": "tsc -p tsconfig.build.json",
//...
}
// Rename to test/tsconfig.json - Gists don't allow nesting
// Used for QoL improvements in tests
{
// Base monorepo tsconfig
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@src/*": ["../src/*"]
},
"noEmit": true,
"allowImportingTsExtensions": true
},
"include": ["./**/*"]
}
// Needed because VS Code and other IDEs
// use it to decide which files to
// apply the base config's rules
{
// Since I work in a monorepo, this one contains all generic compiler options.
// You can write them in this file if you prefer
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
},
"include": ["./src/**/*"],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment