1010
1111import { describe , it , expect , beforeAll , afterAll , beforeEach } from 'vitest' ;
1212import { setupServer } from 'msw/node' ;
13- import { createAuthHandlers } from '../mocks/authHandlers' ;
13+ import { createAuthHandlers , resetAuthState } from '../mocks/authHandlers' ;
1414
1515const BASE_URL = 'http://localhost/api/v1/auth' ;
1616const handlers = createAuthHandlers ( '/api/v1/auth' ) ;
1717const server = setupServer ( ...handlers ) ;
1818
1919beforeAll ( ( ) => server . listen ( { onUnhandledRequest : 'bypass' } ) ) ;
2020afterAll ( ( ) => server . close ( ) ) ;
21-
22- /**
23- * Reset the in-memory user store between tests by importing a
24- * fresh set of handlers. Because the module-level Maps/state
25- * persist across tests within the same module, we accept that
26- * state accumulates during a single describe block and structure
27- * tests as a sequential flow: sign-up → sign-in → session → sign-out.
28- */
21+ beforeEach ( ( ) => resetAuthState ( ) ) ;
2922
3023describe ( 'Mock Auth Handlers' , ( ) => {
3124 it ( 'should register a new user via sign-up' , async ( ) => {
@@ -51,6 +44,13 @@ describe('Mock Auth Handlers', () => {
5144 } ) ;
5245
5346 it ( 'should reject duplicate sign-up' , async ( ) => {
47+ // Register a user first
48+ await fetch ( `${ BASE_URL } /sign-up/email` , {
49+ method : 'POST' ,
50+ headers : { 'Content-Type' : 'application/json' } ,
51+ body : JSON . stringify ( { name : 'Alice' , email : 'alice@example.com' , password : 'secret123' } ) ,
52+ } ) ;
53+
5454 const res = await fetch ( `${ BASE_URL } /sign-up/email` , {
5555 method : 'POST' ,
5656 headers : { 'Content-Type' : 'application/json' } ,
@@ -77,6 +77,13 @@ describe('Mock Auth Handlers', () => {
7777 } ) ;
7878
7979 it ( 'should sign in with correct credentials' , async ( ) => {
80+ // Register user first
81+ await fetch ( `${ BASE_URL } /sign-up/email` , {
82+ method : 'POST' ,
83+ headers : { 'Content-Type' : 'application/json' } ,
84+ body : JSON . stringify ( { name : 'Alice' , email : 'alice@example.com' , password : 'secret123' } ) ,
85+ } ) ;
86+
8087 const res = await fetch ( `${ BASE_URL } /sign-in/email` , {
8188 method : 'POST' ,
8289 headers : { 'Content-Type' : 'application/json' } ,
@@ -106,7 +113,12 @@ describe('Mock Auth Handlers', () => {
106113 } ) ;
107114
108115 it ( 'should return current session after sign-in' , async ( ) => {
109- // First sign in to establish a session
116+ // Register and sign in
117+ await fetch ( `${ BASE_URL } /sign-up/email` , {
118+ method : 'POST' ,
119+ headers : { 'Content-Type' : 'application/json' } ,
120+ body : JSON . stringify ( { name : 'Alice' , email : 'alice@example.com' , password : 'secret123' } ) ,
121+ } ) ;
110122 await fetch ( `${ BASE_URL } /sign-in/email` , {
111123 method : 'POST' ,
112124 headers : { 'Content-Type' : 'application/json' } ,
@@ -150,7 +162,12 @@ describe('Mock Auth Handlers', () => {
150162 } ) ;
151163
152164 it ( 'should update user when authenticated' , async ( ) => {
153- // Sign in first
165+ // Register and sign in first
166+ await fetch ( `${ BASE_URL } /sign-up/email` , {
167+ method : 'POST' ,
168+ headers : { 'Content-Type' : 'application/json' } ,
169+ body : JSON . stringify ( { name : 'Alice' , email : 'alice@example.com' , password : 'secret123' } ) ,
170+ } ) ;
154171 await fetch ( `${ BASE_URL } /sign-in/email` , {
155172 method : 'POST' ,
156173 headers : { 'Content-Type' : 'application/json' } ,
@@ -171,9 +188,6 @@ describe('Mock Auth Handlers', () => {
171188 } ) ;
172189
173190 it ( 'should reject update-user when not authenticated' , async ( ) => {
174- // Sign out first
175- await fetch ( `${ BASE_URL } /sign-out` , { method : 'POST' } ) ;
176-
177191 const res = await fetch ( `${ BASE_URL } /update-user` , {
178192 method : 'POST' ,
179193 headers : { 'Content-Type' : 'application/json' } ,
0 commit comments