-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ErrorFormatterService and corresponding tests
- Loading branch information
mrkeksz
committed
Dec 9, 2023
1 parent
776752a
commit c86e3f4
Showing
3 changed files
with
42 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
import {Test, TestingModule} from '@nestjs/testing' | ||
import {ReasonPhrases, StatusCodes} from 'http-status-codes' | ||
import {ErrorFormatterService} from './error-formatter.service' | ||
import {ReasonPhrases} from 'http-status-codes' | ||
|
||
describe('ErrorFormatterService', () => { | ||
let errorFormatterService: ErrorFormatterService | ||
describe(ErrorFormatterService.name, () => { | ||
let service: ErrorFormatterService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ErrorFormatterService], | ||
}).compile() | ||
|
||
errorFormatterService = module.get(ErrorFormatterService) | ||
service = module.get(ErrorFormatterService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
describe('formatHttpErrorCode', () => { | ||
it('string: should return IM_A_TEAPOT', () => { | ||
const result = errorFormatterService.formatHttpErrorCode(ReasonPhrases.IM_A_TEAPOT) | ||
expect(result).toBe('IM_A_TEAPOT') | ||
}) | ||
it('should return formatted error if http error code is known', () => { | ||
const httpErrorCode = StatusCodes.NOT_FOUND | ||
const expectedResult = service.formatHttpErrorCode(ReasonPhrases.NOT_FOUND) | ||
|
||
it('number: should return IM_A_TEAPOT', () => { | ||
const result = errorFormatterService.formatHttpErrorCode(418) | ||
expect(result).toBe('IM_A_TEAPOT') | ||
const result = service.formatHttpErrorCode(httpErrorCode) | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
|
||
it('unknown string: should return INTERNAL_SERVER_ERROR', () => { | ||
const result = errorFormatterService.formatHttpErrorCode('unknown') | ||
expect(result).toBe('INTERNAL_SERVER_ERROR') | ||
it('should return formatted INTERNAL_SERVER_ERROR if http error code is unknown', () => { | ||
const httpErrorCode = 999 | ||
const expectedResult = service.formatHttpErrorCode(ReasonPhrases.INTERNAL_SERVER_ERROR) | ||
|
||
const result = service.formatHttpErrorCode(httpErrorCode) | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) | ||
|
||
describe('convertPhraseToHttpErrorCodeFormat', () => { | ||
it('should convert phrase to http error code format', () => { | ||
const text = 'Not Found' | ||
const expectedResult = 'NOT_FOUND' | ||
|
||
// convertPhraseToHttpErrorCodeFormat is a private method, so a direct test cannot be written | ||
// we need to use formatHttpErrorCode for testing the functionality of convertPhraseToHttpErrorCodeFormat indirectly | ||
const result = service.formatHttpErrorCode(text) | ||
|
||
it('unknown number: should return INTERNAL_SERVER_ERROR', () => { | ||
const result = errorFormatterService.formatHttpErrorCode(999) | ||
expect(result).toBe('INTERNAL_SERVER_ERROR') | ||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters