0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

test(ut): add error type ut (#229)

add error type ut
This commit is contained in:
simeng-li 2022-02-15 18:11:23 +08:00 committed by GitHub
parent 34e540d3ed
commit 108705076f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,76 @@
import { LogtoErrorI18nKey } from '@logto/phrases';
import i18next from 'i18next';
import initI18n from '@/i18n/init';
import RequestError from '.';
describe('RequestError', () => {
beforeAll(async () => {
await initI18n();
});
it('standard RequestError use LogtoErrorCode', () => {
const errorCode = 'auth.unauthorized';
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`);
const data = { foo: 'foo' };
const newRequestError = new RequestError(errorCode, data);
expect(newRequestError.status).toEqual(400);
expect(newRequestError.code).toEqual(errorCode);
expect(newRequestError.expose).toEqual(true);
expect(newRequestError.message).toEqual(expectMessage);
expect(newRequestError.body).toEqual({
code: errorCode,
data,
message: expectMessage,
});
});
it('standard RequestError use RequestErrorMetadata', () => {
const errorCode = 'auth.unauthorized';
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`);
const data = { foo: 'foo' };
const newRequestError = new RequestError(
{
code: errorCode,
status: 500,
expose: false,
},
data
);
expect(newRequestError.status).toEqual(500);
expect(newRequestError.code).toEqual(errorCode);
expect(newRequestError.expose).toEqual(false);
expect(newRequestError.message).toEqual(expectMessage);
expect(newRequestError.body).toEqual({
code: errorCode,
data,
message: expectMessage,
});
});
it('RequestError with interpolation error message', () => {
const errorCode = 'entity.create_failed';
const entityName = 'mockEntity';
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`, {
name: entityName,
});
const newRequestError = new RequestError({
code: errorCode,
status: 500,
expose: false,
name: entityName,
});
expect(newRequestError.status).toEqual(500);
expect(newRequestError.code).toEqual(errorCode);
expect(newRequestError.expose).toEqual(false);
expect(newRequestError.message).toEqual(expectMessage);
expect(newRequestError.message).toContain('mockEntity');
});
});

View file

@ -0,0 +1,12 @@
import { SlonikError } from 'slonik';
import { DeletionError } from '.';
describe('SlonikError', () => {
it('DeletionError', () => {
const error = new DeletionError('user', 'foo');
expect(error instanceof SlonikError).toEqual(true);
expect(error.table).toEqual('user');
expect(error.id).toEqual('foo');
});
});