Skip to content
Snippets Groups Projects
Unverified Commit 3b7c4207 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

test: Refactor memory cache mocking (#26630)

parent d16e0f23
No related branches found
No related tags found
No related merge requests found
import { logger, mocked } from '../../../test/util'; import { logger } from '../../../test/util';
import type { Logger } from '../../logger/types'; import type { Logger } from '../../logger/types';
import * as _memCache from '../../util/cache/memory'; import * as memCache from '../../util/cache/memory';
import type { LookupStats } from '../../util/cache/memory/types'; import type { LookupStats } from '../../util/cache/memory/types';
import type { RequestStats } from '../../util/http/types';
import { printLookupStats, printRequestStats } from './stats'; import { printLookupStats, printRequestStats } from './stats';
jest.mock('../../util/cache/memory');
const memCache = mocked(_memCache);
const log = logger.logger as jest.Mocked<Logger>; const log = logger.logger as jest.Mocked<Logger>;
describe('workers/repository/stats', () => { describe('workers/repository/stats', () => {
beforeEach(() => {
memCache.init();
});
describe('printLookupStats()', () => { describe('printLookupStats()', () => {
it('runs', () => { it('runs', () => {
const stats: LookupStats[] = [ const stats: LookupStats[] = [
...@@ -27,7 +27,7 @@ describe('workers/repository/stats', () => { ...@@ -27,7 +27,7 @@ describe('workers/repository/stats', () => {
duration: 1000, duration: 1000,
}, },
]; ];
memCache.get.mockImplementationOnce(() => stats as any); memCache.set('lookup-stats', stats);
expect(printLookupStats()).toBeUndefined(); expect(printLookupStats()).toBeUndefined();
expect(log.debug).toHaveBeenCalledTimes(1); expect(log.debug).toHaveBeenCalledTimes(1);
expect(log.debug.mock.calls[0][0]).toMatchInlineSnapshot(` expect(log.debug.mock.calls[0][0]).toMatchInlineSnapshot(`
...@@ -51,13 +51,9 @@ describe('workers/repository/stats', () => { ...@@ -51,13 +51,9 @@ describe('workers/repository/stats', () => {
describe('printRequestStats()', () => { describe('printRequestStats()', () => {
it('runs', () => { it('runs', () => {
const getStats: number[] = [30, 100, 10, 20]; memCache.set('package-cache-gets', [30, 100, 10, 20]);
// TODO: fix types, jest is using wrong overload (#22198) memCache.set('package-cache-sets', [110, 80, 20]);
memCache.get.mockImplementationOnce(() => getStats as any); memCache.set('http-requests', [
const setStats: number[] = [110, 80, 20];
// TODO: fix types, jest is using wrong overload (#22198)
memCache.get.mockImplementationOnce(() => setStats as any);
const httpStats: RequestStats[] = [
{ {
method: 'get', method: 'get',
url: 'https://api.github.com/api/v3/user', url: 'https://api.github.com/api/v3/user',
...@@ -100,9 +96,7 @@ describe('workers/repository/stats', () => { ...@@ -100,9 +96,7 @@ describe('workers/repository/stats', () => {
queueDuration: 0, queueDuration: 0,
statusCode: 401, statusCode: 401,
}, },
]; ]);
// TODO: fix types, jest is using wrong overload (#22198)
memCache.get.mockImplementationOnce(() => httpStats as any);
expect(printRequestStats()).toBeUndefined(); expect(printRequestStats()).toBeUndefined();
expect(log.trace).toHaveBeenCalledOnce(); expect(log.trace).toHaveBeenCalledOnce();
expect(log.debug).toHaveBeenCalledTimes(2); expect(log.debug).toHaveBeenCalledTimes(2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment