diff options
| author | Bertrand Yuan <189593334+bertyuan@users.noreply.github.com> | 2026-03-26 00:19:31 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-26 00:19:31 +0800 |
| commit | f247a8c4a863ec430f4a705b5c493d652c8429bd (patch) | |
| tree | 71d0985970984c105582f6e3c370b254f38e9bbe /src/lib/safe-action.test.ts | |
| parent | f7a02fe0e112cf108fc5f22872f1efc077e99fe8 (diff) | |
| parent | cd3c4bc89c169616b38bdb7443bb4eb7571b020c (diff) | |
Merge pull request #12 from bertyuan/fix-vitestv1.1
Fix vitest
Diffstat (limited to 'src/lib/safe-action.test.ts')
| -rw-r--r-- | src/lib/safe-action.test.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/safe-action.test.ts b/src/lib/safe-action.test.ts new file mode 100644 index 0000000..eee5679 --- /dev/null +++ b/src/lib/safe-action.test.ts @@ -0,0 +1,42 @@ +import { afterEach, describe, expect, test, vi } from 'vitest'; + +vi.mock('next-safe-action', () => ({ + DEFAULT_SERVER_ERROR_MESSAGE: 'Internal server error', + createSafeActionClient: vi.fn((options: unknown) => options), +})); + +describe('safe-action', () => { + afterEach(() => { + vi.restoreAllMocks(); + vi.resetModules(); + }); + + test('returns custom message for ActionError', async () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const { ActionError, actionClient } = await import('./safe-action'); + const client = actionClient as unknown as { + handleServerError: (e: Error) => string; + }; + + const message = client.handleServerError(new ActionError('Custom error')); + + expect(message).toBe('Custom error'); + expect(errorSpy).toHaveBeenCalledWith( + 'Failed to execute action:', + 'Custom error', + ); + }); + + test('returns default message for unknown errors', async () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const { actionClient } = await import('./safe-action'); + const client = actionClient as unknown as { + handleServerError: (e: Error) => string; + }; + + const message = client.handleServerError(new Error('Unexpected')); + + expect(message).toBe('Internal server error'); + expect(errorSpy).toHaveBeenCalledWith('Failed to execute action:', 'Unexpected'); + }); +}); |
