-
Notifications
You must be signed in to change notification settings - Fork 68
test: add health endpoint smoke test and make redis test-safe #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,57 @@ | ||
| const Redis = require("ioredis"); | ||
| const dotenv = require("dotenv"); | ||
| dotenv.config() | ||
| dotenv.config(); | ||
|
|
||
| if (!process.env.REDIS_URL) { | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| console.log("DEBUG: ENV KEYS:", Object.keys(process.env)); | ||
| /** | ||
| * TEST MODE → fake redis | ||
| */ | ||
| if (process.env.NODE_ENV === "test") { | ||
| console.warn("⚠️ Redis disabled in test environment"); | ||
|
|
||
| const dummyRedis = { | ||
| status: "ready", | ||
| get: async () => null, | ||
| set: async () => null, | ||
| del: async () => null, | ||
| quit: async () => null, | ||
| on: () => {}, // some modules attach listeners | ||
| }; | ||
|
|
||
| module.exports = dummyRedis; | ||
|
|
||
| } else { | ||
|
|
||
| /** | ||
| * DEV / PROD → real redis | ||
| */ | ||
| if (!process.env.REDIS_URL) { | ||
| if (process.env.NODE_ENV !== "production") { | ||
| console.log("DEBUG: ENV KEYS:", Object.keys(process.env)); | ||
| } | ||
| throw new Error("REDIS_URL is not defined in .env"); | ||
| } | ||
| } | ||
|
|
||
| const redis = new Redis(process.env.REDIS_URL, { | ||
| const redis = new Redis(process.env.REDIS_URL, { | ||
| retryStrategy(times) { | ||
| const delay = Math.min(times * 50, 2000); | ||
| if (times > 3) { | ||
| console.warn("⚠️ Redis: Max retries reached. Caching will be disabled."); | ||
| return null; // Stop retrying | ||
| } | ||
| return delay; | ||
| const delay = Math.min(times * 50, 2000); | ||
| if (times > 3) { | ||
| console.warn("⚠️ Redis: Max retries reached. Caching will be disabled."); | ||
| return null; | ||
| } | ||
| return delay; | ||
| }, | ||
| maxRetriesPerRequest: null // Allow requests to fail if not connected | ||
| }); | ||
| maxRetriesPerRequest: null, | ||
| }); | ||
|
|
||
| redis.on('ready', () => { | ||
| console.log('ioredis client is connected and ready.'); | ||
| }); | ||
| redis.on("ready", () => { | ||
| console.log("ioredis client is connected and ready."); | ||
| }); | ||
|
|
||
| redis.on('error', (err) => { | ||
| console.error('❌ Redis Connection Error:', err.message); | ||
| console.error(' -> Ensure Redis is running on localhost:6379'); | ||
| console.error(' -> Windows: Use WSL or a Memurai/Redis port.'); | ||
| // Do not throw; lets the app continue without caching if needed | ||
| }); | ||
| redis.on("error", (err) => { | ||
| console.error("❌ Redis Connection Error:", err.message); | ||
| console.error(" -> Ensure Redis is running on localhost:6379"); | ||
| console.error(" -> Windows: Use WSL or a Memurai/Redis port."); | ||
| }); | ||
|
|
||
| module.exports = redis; | ||
| module.exports = redis; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const request = require("supertest"); | ||
| const app = require("../app"); | ||
|
|
||
| describe("Health Check API", () => { | ||
| it("should return 200 and success message", async () => { | ||
| const res = await request(app).get("/"); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.body.status).toBe("success"); | ||
| }); | ||
| }); | ||
|
Comment on lines
+1
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find all test files in backend/tests
fd '.test.js' --base-directory backend/tests 2>/dev/null || find backend/tests -name '*.test.js' 2>/dev/null
# Check app.js for app.listen and exports
echo "=== Checking app.js for listen/exports ==="
rg -n 'app\.listen|module\.exports\s*=|exports\s*=' backend/app.js
# Look for auth-related test files
echo "=== Looking for auth test files ==="
fd -e js --base-directory backend/tests | grep -i authRepository: yash-pouranik/urBackend Length of output: 260 🏁 Script executed: # Check the context around app.listen() call
sed -n '125,145p' backend/app.js
# Check if there's any conditional wrapping around listen
sed -n '120,170p' backend/app.js | head -50
# Read auth.test.js to see what it tests
cat backend/tests/auth.test.jsRepository: yash-pouranik/urBackend Length of output: 5194 Add The health check test is correct, but it leaves the server running. Since Add an Required addition const request = require("supertest");
const app = require("../app");
describe("Health Check API", () => {
+ let server;
+ afterAll((done) => {
+ if (server) server.close(done);
+ else done();
+ });
+
it("should return 200 and success message", async () => {
- const res = await request(app).get("/");
+ server = app._server || app;
+ const res = await request(app).get("/");
expect(res.statusCode).toBe(200);
expect(res.body.status).toBe("success");
});
});Alternatively, refactor 🧰 Tools🪛 GitHub Actions: CI (Continuous Integration)[error] 1-1: Test suite failed: Auth API login expected 200 but received 500 during CI run. Command: 'cd backend && npm test'. 🤖 Prompt for AI Agents
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alok-kumar0421 resolve this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| const requiredEnvVars = ["MONGO_URL", "PORT", "REDIS_URL"]; | ||
|
|
||
| function validateEnv() { | ||
| if (process.env.NODE_ENV === "test") return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alok-kumar0421 look into ./backend/app.js i prevented the validateEnv function call if env is test |
||
| const missing = requiredEnvVars.filter((key) => !process.env[key]); | ||
|
|
||
| if (missing.length) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.