File tree Expand file tree Collapse file tree 3 files changed +32
-22
lines changed
Expand file tree Collapse file tree 3 files changed +32
-22
lines changed Original file line number Diff line number Diff line change 1- // error.test.js
2- const { handler } = require ( '../index' ) ; // go up one level
3-
1+ // __tests__/error.test.js
2+ const { handler } = require ( '../index' ) ;
43
54describe ( 'Error handling tests' , ( ) => {
6-
75 test ( 'GET /users/:id returns 404 if user does not exist' , async ( ) => {
86 const event = {
97 httpMethod : 'GET' ,
10- path : '/users/9999'
8+ path : '/users/9999' ,
119 } ;
1210
11+ // Set RDS credentials
12+ process . env . DB_HOST = process . env . DB_HOST ;
13+ process . env . DB_USER = process . env . DB_USER ;
14+ process . env . DB_PASSWORD = process . env . DB_PASSWORD ;
15+ process . env . DB_NAME = process . env . DB_NAME ;
16+ process . env . DB_PORT = process . env . DB_PORT || 5432 ;
17+
1318 const response = await handler ( event ) ;
1419
1520 expect ( response . statusCode ) . toBe ( 404 ) ;
1621
1722 const body = JSON . parse ( response . body ) ;
1823 expect ( body . message ) . toBe ( 'User not found' ) ;
1924 } ) ;
20-
2125} ) ;
26+
Original file line number Diff line number Diff line change 1- // lambda.test.js
2- const { handler } = require ( '../index' ) ; // go up one level
3-
1+ // __tests__/example.test.js
2+ const { handler } = require ( '../index' ) ;
43
54describe ( 'Lambda API tests' , ( ) => {
6-
75 test ( 'GET /users should return status 200 and a list of users' , async ( ) => {
86 const event = {
97 httpMethod : 'GET' ,
108 path : '/users' ,
119 } ;
1210
11+ // Set RDS credentials from environment variables
12+ process . env . DB_HOST = process . env . DB_HOST ;
13+ process . env . DB_USER = process . env . DB_USER ;
14+ process . env . DB_PASSWORD = process . env . DB_PASSWORD ;
15+ process . env . DB_NAME = process . env . DB_NAME ;
16+ process . env . DB_PORT = process . env . DB_PORT || 5432 ;
17+
1318 const response = await handler ( event ) ;
1419
15- // Check HTTP status
1620 expect ( response . statusCode ) . toBe ( 200 ) ;
1721
18- // Check response body is an array (JSON parsed)
1922 const body = JSON . parse ( response . body ) ;
2023 expect ( Array . isArray ( body ) ) . toBe ( true ) ;
21-
22- // Optional: check there is at least one user
23- expect ( body . length ) . toBeGreaterThan ( 0 ) ;
24+ expect ( body . length ) . toBeGreaterThanOrEqual ( 0 ) ;
2425 } ) ;
25-
2626} ) ;
27+
Original file line number Diff line number Diff line change 1- // users.test.js
2- const { handler } = require ( '../index' ) ; // go up one level
3-
1+ // __tests__/users.test.js
2+ const { handler } = require ( '../index' ) ;
43
54describe ( 'POST /users endpoint' , ( ) => {
6-
75 test ( 'should create a new user and return status 201' , async ( ) => {
86 const event = {
97 httpMethod : 'POST' ,
108 path : '/users' ,
11- body : JSON . stringify ( { name : 'John Doe' , email : 'john@example.com' } )
9+ body : JSON . stringify ( { name : 'John Doe' , email : 'john@example.com' } ) ,
1210 } ;
1311
12+ // Set RDS credentials
13+ process . env . DB_HOST = process . env . DB_HOST ;
14+ process . env . DB_USER = process . env . DB_USER ;
15+ process . env . DB_PASSWORD = process . env . DB_PASSWORD ;
16+ process . env . DB_NAME = process . env . DB_NAME ;
17+ process . env . DB_PORT = process . env . DB_PORT || 5432 ;
18+
1419 const response = await handler ( event ) ;
1520
1621 expect ( response . statusCode ) . toBe ( 201 ) ;
@@ -20,5 +25,4 @@ describe('POST /users endpoint', () => {
2025 expect ( body . email ) . toBe ( 'john@example.com' ) ;
2126 expect ( body . id ) . toBeDefined ( ) ;
2227 } ) ;
23-
2428} ) ;
You can’t perform that action at this time.
0 commit comments