backend-test-utils: more strict test to ensure mockServices support + fixes

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-29 10:14:23 +02:00
parent cad7b53fc2
commit 710f621de2
4 changed files with 39 additions and 14 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-test-utils': patch
---
Added missing service mock for `mockServices.rootConfig.mock`, and fixed the definition of `mockServices.rootHttpRouter.factory` to not have a duplicate callback.
+5 -5
View File
@@ -36,7 +36,6 @@ import { ParsedQs } from 'qs';
import { PermissionsService } from '@backstage/backend-plugin-api';
import { RootConfigService } from '@backstage/backend-plugin-api';
import { RootHealthService } from '@backstage/backend-plugin-api';
import { RootHttpRouterFactoryOptions } from '@backstage/backend-defaults/rootHttpRouter';
import { RootHttpRouterService } from '@backstage/backend-plugin-api';
import { RootLifecycleService } from '@backstage/backend-plugin-api';
import { RootLoggerService } from '@backstage/backend-plugin-api';
@@ -267,6 +266,10 @@ export namespace mockServices {
factory: (
options?: Options | undefined,
) => ServiceFactory<RootConfigService, 'root', 'singleton' | 'multiton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<RootConfigService> | undefined,
) => ServiceMock<RootConfigService>;
}
// (undocumented)
export namespace rootHealth {
@@ -280,10 +283,7 @@ export namespace mockServices {
// (undocumented)
export namespace rootHttpRouter {
const // (undocumented)
factory: () => ((
options?: RootHttpRouterFactoryOptions | undefined,
) => ServiceFactory<RootHttpRouterService, 'root', 'singleton'>) &
ServiceFactory<RootHttpRouterService, 'root', 'singleton'>;
factory: () => ServiceFactory<RootHttpRouterService, 'root', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<RootHttpRouterService> | undefined,
@@ -18,13 +18,15 @@ import { coreServices } from '@backstage/backend-plugin-api';
import { mockServices } from './mockServices';
describe('mockServices', () => {
it('should have mock implementations for all core services', () => {
const mockServiceKeys = Object.keys(mockServices);
for (const key of Object.keys(coreServices)) {
if (key === 'pluginMetadata') {
continue;
}
expect(mockServiceKeys).toContain(key);
}
const coreServiceKeys = Object.keys(coreServices).filter(
key => key !== 'pluginMetadata',
) as Array<keyof typeof mockServices>;
it.each(coreServiceKeys)('should have mock implementations for %s', key => {
expect(mockServices[key]).toBeDefined();
expect(mockServices[key].mock).toEqual(expect.any(Function));
expect(mockServices[key].mock()).toEqual(expect.any(Object));
expect(mockServices[key].factory).toEqual(expect.any(Function));
expect(mockServices[key].factory()).toEqual(expect.any(Object));
});
});
@@ -141,6 +141,24 @@ export namespace mockServices {
coreServices.rootConfig,
rootConfig,
);
export const mock = simpleMock(coreServices.rootConfig, () => ({
get: jest.fn(),
getBoolean: jest.fn(),
getConfig: jest.fn(),
getConfigArray: jest.fn(),
getNumber: jest.fn(),
getOptional: jest.fn(),
getOptionalBoolean: jest.fn(),
getOptionalConfig: jest.fn(),
getOptionalConfigArray: jest.fn(),
getOptionalNumber: jest.fn(),
getOptionalString: jest.fn(),
getOptionalStringArray: jest.fn(),
getString: jest.fn(),
getStringArray: jest.fn(),
has: jest.fn(),
keys: jest.fn(),
}));
}
export function rootLogger(options?: rootLogger.Options): LoggerService {
@@ -341,7 +359,7 @@ export namespace mockServices {
}
export namespace rootHttpRouter {
export const factory = () => rootHttpRouterServiceFactory;
export const factory = () => rootHttpRouterServiceFactory();
export const mock = simpleMock(coreServices.rootHttpRouter, () => ({
use: jest.fn(),
}));