Skip to content

Commit 398924b

Browse files
committed
fix: set NODE_ENV to test for database setup and prevent server from starting in test environment
1 parent fa8cad0 commit 398924b

3 files changed

Lines changed: 5 additions & 2 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
- name: Setup Database and run Tests
4444
env:
4545
DATABASE_URL: postgres://postgres:password@localhost:5432/rest-api-db
46+
NODE_ENV: test
4647
run: |
4748
# Wait for postgres to be ready (prevents connection errors)
4849
until pg_isready -h localhost -p 5432; do

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"scripts": {
1313
"start": "nodemon server.js",
14-
"test": "jest --detectOpenHandles"
14+
"test": "NODE_ENV=test jest --runInBand"
1515
},
1616
"keywords": [],
1717
"author": "@DilshanX09",

server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ app.get('/health', (req, res) => {
3939
});
4040
});
4141

42-
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
42+
if (process.env.NODE_ENV !== 'test') {
43+
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
44+
}
4345

4446
module.exports = app;

0 commit comments

Comments
 (0)