Skip to content

Commit 8b52a19

Browse files
committed
test(clerk-js): Add basic errorsToParsedErrors test
1 parent 3b8c675 commit 8b52a19

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { ClerkError } from '@clerk/shared/error';
2+
import { ClerkAPIResponseError } from '@clerk/shared/error';
3+
import { describe, expect, it } from 'vitest';
4+
5+
import { errorsToParsedErrors } from '../signals';
6+
7+
describe('errorsToParsedErrors', () => {
8+
it('returns empty errors object when error is null', () => {
9+
const initialFields = { emailAddress: null, password: null };
10+
const result = errorsToParsedErrors(null, initialFields);
11+
12+
expect(result).toEqual({
13+
fields: { emailAddress: null, password: null },
14+
raw: null,
15+
global: null,
16+
});
17+
});
18+
19+
it('handles non-API errors by putting them in raw and global arrays', () => {
20+
const initialFields = { emailAddress: null, password: null };
21+
// Use a plain Error cast as ClerkError to test non-API error handling
22+
const error = new Error('Something went wrong') as unknown as ClerkError;
23+
const result = errorsToParsedErrors(error, initialFields);
24+
25+
expect(result.fields).toEqual({ emailAddress: null, password: null });
26+
expect(result.raw).toEqual([error]);
27+
expect(result.global).toBeTruthy();
28+
expect(result.global?.length).toBe(1);
29+
});
30+
31+
it('handles API errors with field errors', () => {
32+
const initialFields = { emailAddress: null, password: null };
33+
const error = new ClerkAPIResponseError('Validation failed', {
34+
data: [
35+
{
36+
code: 'form_identifier_not_found',
37+
message: 'emailAddress not found',
38+
meta: { param_name: 'emailAddress' },
39+
},
40+
],
41+
status: 400,
42+
});
43+
const result = errorsToParsedErrors(error, initialFields);
44+
45+
expect(result.fields.emailAddress).toBeTruthy();
46+
expect(result.fields.password).toBeNull();
47+
expect(result.raw).toEqual([error.errors[0]]);
48+
expect(result.global).toBeNull();
49+
});
50+
51+
it('handles API errors without field errors', () => {
52+
const initialFields = { emailAddress: null, password: null };
53+
const error = new ClerkAPIResponseError('Server error', {
54+
data: [
55+
{
56+
code: 'internal_error',
57+
message: 'Something went wrong on the server',
58+
},
59+
],
60+
status: 500,
61+
});
62+
const result = errorsToParsedErrors(error, initialFields);
63+
64+
expect(result.fields).toEqual({ emailAddress: null, password: null });
65+
// When there are no field errors, individual ClerkAPIError instances are put in raw
66+
expect(result.raw).toEqual([error.errors[0]]);
67+
// Note: global is null when errors are processed individually without field errors
68+
expect(result.global).toBeNull();
69+
});
70+
});

packages/clerk-js/src/core/signals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const signUpComputedSignal: SignUpSignal = computed(() => {
3838
* Converts an error to a parsed errors object that reports the specific fields that the error pertains to. Will put
3939
* generic non-API errors into the global array.
4040
*/
41-
function errorsToParsedErrors<T extends Record<string, unknown>>(
41+
export function errorsToParsedErrors<T extends Record<string, unknown>>(
4242
error: ClerkError | null,
4343
initialFields: T,
4444
): Errors<T> {

0 commit comments

Comments
 (0)