Skip to content

Instantly share code, notes, and snippets.

@BoscoDomingo
Last active September 23, 2024 09:16
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

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

Expected folder structure

ts-project/
├── tsconfig.json
└── apps/
    └── app-1/
        ├── tsconfig.json
        ├── package.json
        └── test/
            └── tsconfig.json

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
"scripts": {
//...
"test": "glob -c \"tsx --env-file .env --env-file .env.test --test --test-reporter spec \" \"./test/**/*.test.ts\"",
//...
}
// RENAME TO `test/tsconfig.json` - Gists don't allow '/' in the filename
{
"extends": "../tsconfig.json", // Can also extend the base monorepo config
"compilerOptions": {
"noEmit": true,
"allowImportingTsExtensions": true
// These don't work with Node Test Runner, but do with Bun. Purely optional QoL
// "baseUrl": ".",
// "paths": {
// "@src/*": ["../src/*"]
// },
},
"include": ["./**/*"]
}
// Needed because VS Code and other IDEs
// use it to decide which files to
// apply the base config's rules
{
"extends": "../../tsconfig.json", // Base monorepo tsconfig
"compilerOptions": {
// If "outDir": "${configDir}/dist", not already present
// in the base config. Otherwise you can delete `compilerOptions`
"outDir": "./dist"
},
"include": ["./src/**/*"],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment