-
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.
test: added tests for caching service
- Loading branch information
1 parent
412844a
commit ab77546
Showing
1 changed file
with
60 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Logger } from '@nestjs/common'; | ||
import { CACHE_MANAGER } from '@nestjs/cache-manager'; | ||
import { CachingService } from './cache.service'; | ||
|
||
describe('CacheService', () => { | ||
let service: CachingService; | ||
const mockCacheManager = { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CachingService, | ||
Logger, | ||
{ provide: CACHE_MANAGER, useValue: mockCacheManager }, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<CachingService>(CachingService); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should save the result to the cache', async () => { | ||
await service.store('foo', 'bar'); | ||
expect(mockCacheManager.set).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should not throw an error whenever something went wrong while saving the result to the cache', async () => { | ||
jest.spyOn(mockCacheManager, 'set').mockImplementationOnce(() => { | ||
throw Error('No way!'); | ||
}); | ||
await service.store('foo', 'bar'); | ||
expect(mockCacheManager.set).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should check the result to the cache', async () => { | ||
jest.spyOn(mockCacheManager, 'get').mockResolvedValue('bar'); | ||
expect(await service.checkCache('foo')).toEqual('bar'); | ||
expect(mockCacheManager.get).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should return undefined whenever an error occurred during cache lookup', async () => { | ||
jest.spyOn(mockCacheManager, 'get').mockImplementationOnce(() => { | ||
throw Error('No way!'); | ||
}); | ||
expect(await service.checkCache('foo')).toEqual(undefined); | ||
expect(mockCacheManager.get).toBeCalledTimes(1); | ||
}); | ||
}); |