diff --git a/packages/fastify-core/src/__tests__/fastify-core.test.ts b/packages/fastify-core/src/__tests__/fastify-core.test.ts index 0fb60f5f..77e6233d 100644 --- a/packages/fastify-core/src/__tests__/fastify-core.test.ts +++ b/packages/fastify-core/src/__tests__/fastify-core.test.ts @@ -32,8 +32,8 @@ describe('createServiceApp', () => { expect(body.service).toBe('my-service'); expect(body.version).toBe('1.2.3'); expect(body.description).toBe('A test service'); - expect(body.timestamp).toBeTruthy(); - expect(body.requestId).toBeTruthy(); + expect(body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/); + expect(body.requestId).toBe(res.headers['x-request-id']); await app.close(); }); @@ -72,6 +72,8 @@ describe('createServiceApp', () => { expect(res.headers['x-request-id']).toMatch( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ ); + const body = JSON.parse(res.payload); + expect(body.requestId).toBe(res.headers['x-request-id']); await app.close(); }); @@ -92,7 +94,7 @@ describe('createServiceApp', () => { expect(res.statusCode).toBe(404); const body = JSON.parse(res.payload); expect(body.error).toBe('User not found'); - expect(body.requestId).toBeTruthy(); + expect(body.requestId).toBe(res.headers['x-request-id']); await app.close(); }); @@ -114,7 +116,7 @@ describe('createServiceApp', () => { const body = JSON.parse(res.payload); expect(body.error).toBe('Validation failed'); expect(body.details).toEqual({ field: 'email' }); - expect(body.requestId).toBeTruthy(); + expect(body.requestId).toBe(res.headers['x-request-id']); await app.close(); }); @@ -134,7 +136,7 @@ describe('createServiceApp', () => { expect(res.statusCode).toBe(500); const body = JSON.parse(res.payload); expect(body.error).toBe('Internal server error'); - expect(body.requestId).toBeTruthy(); + expect(body.requestId).toBe(res.headers['x-request-id']); await app.close(); }); @@ -166,6 +168,8 @@ describe('createServiceApp', () => { expect(JSON.parse(before.payload)).toMatchObject({ status: 'not_ready', service: 'ready-test', + version: '1.0.0', + requestId: before.headers['x-request-id'], }); app.setReadyState(true); @@ -175,6 +179,8 @@ describe('createServiceApp', () => { expect(JSON.parse(after.payload)).toMatchObject({ status: 'ready', service: 'ready-test', + version: '1.0.0', + requestId: after.headers['x-request-id'], }); await app.close(); diff --git a/packages/react-auth/src/__tests__/react-auth.test.tsx b/packages/react-auth/src/__tests__/react-auth.test.tsx index e3c90aea..0c080713 100644 --- a/packages/react-auth/src/__tests__/react-auth.test.tsx +++ b/packages/react-auth/src/__tests__/react-auth.test.tsx @@ -67,8 +67,7 @@ describe('createAuthProvider', () => {
Hello
); - expect(screen.getByTestId('child')).toBeDefined(); - expect(screen.getByText('Hello')).toBeDefined(); + expect(screen.getByTestId('child').textContent).toBe('Hello'); }); it('starts unauthenticated with no stored user', () => { @@ -159,6 +158,13 @@ describe('createAuthProvider', () => { expect(result).toBe(true); expect(screen.getByTestId('auth').textContent).toBe('true'); expect(screen.getByTestId('email').textContent).toBe('test@example.com'); + expect(mockFetch).toHaveBeenCalledWith( + '/api/auth/login', + expect.objectContaining({ + method: 'POST', + body: JSON.stringify({ email: 'test@example.com', password: 'pass123' }), + }) + ); expect(localStorageMock.setItem).toHaveBeenCalledWith( 'test_auth_user', expect.stringContaining('test@example.com') @@ -179,9 +185,14 @@ describe('createAuthProvider', () => { let loginFn: (email: string, password: string) => Promise; function LoginComponent() { - const { login, isAuthenticated } = useAuth(); + const { login, isAuthenticated, error } = useAuth(); loginFn = login; - return {String(isAuthenticated)}; + return ( +
+ {String(isAuthenticated)} + {error ?? 'none'} +
+ ); } render( @@ -197,6 +208,7 @@ describe('createAuthProvider', () => { expect(result).toBe(false); expect(screen.getByTestId('auth').textContent).toBe('false'); + expect(screen.getByTestId('error').textContent).toBe('Unauthorized'); }); it('logout clears user and storage', async () => {