1+ import { test , expect , afterAll , beforeEach } from '@jest/globals'
12import request from 'supertest'
2- import { app , prisma } from '../src/app'
3+ import { app , prisma } from '../src/app.js'
4+
5+ beforeEach ( async ( ) => {
6+ // Clean up the database before each test
7+ await prisma . user . deleteMany ( )
8+ } )
39
410afterAll ( async ( ) => {
511 await prisma . $disconnect ( )
@@ -19,23 +25,53 @@ test('a user is added successfully', async () => {
1925 . expect ( 200 )
2026
2127 expect ( response . body . id ) . toBeDefined ( )
28+ expect ( response . body . name ) . toBe ( user . name )
29+ expect ( response . body . email ) . toBe ( user . email )
2230} , 60000 )
2331
24- test ( 'a user with the same email is rejected' , ( ) => {
25- return request ( app )
32+ test ( 'a user with the same email is rejected' , async ( ) => {
33+ // First, create a user
34+ await request ( app )
2635 . post ( '/user' )
2736 . send ( user )
2837 . set ( 'Accept' , 'application/json' )
29- . expect ( 'Content-Type' , / j s o n / )
30- . expect ( 409 )
38+ . expect ( 200 )
39+
40+ // Then try to create another user with the same email
41+ const duplicateResponse = await request ( app )
42+ . post ( '/user' )
43+ . send ( user )
44+ . set ( 'Accept' , 'application/json' )
45+
46+ expect ( duplicateResponse . status ) . toBe ( 409 )
47+ expect ( duplicateResponse . body . error ) . toBe ( 'User already exists!' )
3148} , 60000 )
3249
3350test ( 'correct list of users returned' , async ( ) => {
51+ // Create multiple users
52+ const users = [
53+ { name :
'user 1' , email :
'[email protected] ' } , 54+ { name :
'user 2' , email :
'[email protected] ' } , 55+ { name :
'user 3' , email :
'[email protected] ' } 56+ ]
57+
58+ for ( const userData of users ) {
59+ await request ( app )
60+ . post ( '/user' )
61+ . send ( userData )
62+ . set ( 'Accept' , 'application/json' )
63+ . expect ( 200 )
64+ }
65+
66+ // Get all users
3467 const response = await request ( app )
3568 . get ( '/user' )
3669 . expect ( 'Content-Type' , / j s o n / )
3770 . expect ( 200 )
3871
3972 expect ( response . body ) . toBeDefined ( )
40- expect ( response . body . length ) . toBeGreaterThan ( 1 )
73+ expect ( response . body . length ) . toBe ( 3 )
74+ expect ( response . body [ 0 ] . email ) . toBeDefined ( )
75+ expect ( response . body [ 1 ] . email ) . toBeDefined ( )
76+ expect ( response . body [ 2 ] . email ) . toBeDefined ( )
4177} , 60000 )
0 commit comments