@yoroi/exchange
Version:
The Exchange package of Yoroi SDK
41 lines (40 loc) • 1.25 kB
JavaScript
import { Exchange } from '@yoroi/types';
import { z } from 'zod';
import { getValidationError } from './get-validation-error';
describe('getValidationError', () => {
const testSchema = z.object({
name: z.string(),
age: z.number()
});
test('should convert a ZodError into a ValidationError', () => {
const invalidData = {
name: 123,
age: 'John Doe'
};
try {
testSchema.parse(invalidData);
} catch (error) {
expect(error).toBeInstanceOf(z.ZodError);
// Handle the error
let handledError;
try {
throw getValidationError(error);
} catch (e) {
handledError = e;
expect(handledError).toBeInstanceOf(Exchange.Errors.Validation);
expect(handledError?.message).toBe('Invalid data: name: Expected string, received number, age: Expected number, received string');
}
}
});
test('should re-throw an error that is not a ZodError', () => {
const someOtherError = new Error('Some other error');
let handledError;
try {
throw getValidationError(someOtherError);
} catch (e) {
handledError = e;
}
expect(handledError).toBeInstanceOf(Error);
});
});
//# sourceMappingURL=get-validation-error.test.js.map