Skip to content

Commit fc598ab

Browse files
authored
test: add support for loading test environment variables (#39)
* refactor: move sanity test to src * chore: update test scripts in package.json to streamline testing process * feat: add support for loading test environment variables in loadEnvFile.ts * chore: update GitHub Actions test workflow to use npm run test:ci instead of npm run test:build * chore: update GitHub Actions workflow to separate build and test steps for clarity * chore: add .env.test file for local environment variable configuration and update .gitignore to include it * refactor: reorganize loadEnvFile function to ensure environment-specific configurations are loaded correctly * refactor: streamline loadEnvFile function to utilize a mapping for environment-specific files
1 parent ac10bce commit fc598ab

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

.env.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Local Environment Variables (Secrets)
2+
# Copy this file to .env and fill in your actual values
3+
# .env is gitignored and should NEVER be committed
4+
5+
# Discord Bot Token & Application ID (REQUIRED)
6+
# Get this from: https://discord.com/developers/applications
7+
DISCORD_TOKEN=your-bot-token-here
8+
CLIENT_ID=your-bot-application-id
9+
10+
# Override any public config values for local testing
11+
12+
# Discord Server ID (your dev server)
13+
SERVER_ID=your-server-id
14+
15+
# Channel IDs (from your dev server)
16+
GUIDES_CHANNEL_ID=your-guide-channel-id
17+
REPEL_LOG_CHANNEL_ID=your-repel-log-channel-id
18+
19+
# Role IDs (from your dev server)
20+
REPEL_ROLE_ID=your-repel-role-id
21+
MODERATORS_ROLE_IDS=your-moderator-role-id
22+
23+
# Other
24+
GUIDES_TRACKER_PATH=guides-tracker.json

.github/workflows/test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ jobs:
3535
- name: Lint
3636
run: npm run lint
3737

38-
- name: Build and test
39-
run: npm run test:build
38+
- name: Build
39+
run: npm run build:ci
40+
41+
- name: Test
42+
run: npm run test:ci
4043

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ yarn-error.log*
1818
# Public config (committed to repo)
1919
!.env.production
2020
!.env.example
21+
!.env.test
2122

2223
# guides tracker
2324
guides-tracker.json

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build:ci": "npm run build:ts && npm run build:copy",
88
"build:dev": "pnpm run build:ts && pnpm run build:copy",
99
"build:ts": "tsup",
10-
"build:test": "tsup --entry 'src/**/*.ts' --entry 'test/**/*.ts'",
1110
"build:copy": "node scripts/copy-assets.js",
1211
"start": "node dist/index.js",
1312
"dev": "tsx watch src/index.ts",
@@ -23,8 +22,7 @@
2322
"check:fix": "biome check --write .",
2423
"typecheck": "tsc --noEmit",
2524
"test": "pnpm run build:dev && node --test dist/**/*.test.js",
26-
"test:ci": "node --test $(find dist -name '*.test.js')",
27-
"test:build": "npm run build:test && npm run test:ci",
25+
"test:ci": "NODE_ENV=test node --test dist/**/*.test.js",
2826
"prepare": "husky",
2927
"pre-commit": "lint-staged",
3028
"sync-guides": "tsx scripts/sync-guides.js",

src/loadEnvFile.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ function loadEnvFile(filePath: string) {
3636
const nodeEnv = process.env.NODE_ENV || 'development';
3737
console.log(`🌍 Environment: ${nodeEnv}`);
3838

39-
// Load environment-specific config first (public values, production only)
40-
if (nodeEnv === 'production') {
41-
const envFile = join(process.cwd(), '.env.production');
39+
const ENV_FILES = {
40+
test: join(process.cwd(), '.env.test'),
41+
production: join(process.cwd(), '.env.production'),
42+
development: join(process.cwd(), '.env'),
43+
};
44+
45+
const envFile = ENV_FILES[nodeEnv as keyof typeof ENV_FILES];
46+
if (envFile) {
4247
loadEnvFile(envFile);
48+
} else {
49+
console.error(`❌ Environment file ${nodeEnv} not found`);
50+
process.exit(1);
4351
}
44-
45-
// Load .env file with secrets and local config (overrides public config if any)
46-
// Required for DISCORD_TOKEN and other secrets
47-
const localEnvFile = join(process.cwd(), '.env');
48-
loadEnvFile(localEnvFile);
File renamed without changes.

0 commit comments

Comments
 (0)