Add test helpers for new backend system

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-08-11 16:19:58 +02:00
parent 3d5ab6cb3c
commit 56e1b4b89c
13 changed files with 208 additions and 26 deletions
+1
View File
@@ -22,4 +22,5 @@
export * from './database';
export * from './msw';
export * from './next';
export * from './util';
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './wiring';
@@ -0,0 +1,76 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
createBackendModule,
createServiceFactory,
createServiceRef,
} from '@backstage/backend-plugin-api';
import { createTestBackend, startTestBackend } from './TestBackend';
describe('TestBackend', () => {
it('should get a type error if service implementation does not match', () => {
const serviceRef = createServiceRef<{ a: string; b: string }>({ id: 'a' });
const backend = createTestBackend({
services: [
[serviceRef, { a: 'a' }],
[serviceRef, { a: 'a', b: 'b' }],
// @ts-expect-error
[serviceRef, { c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', c: 'c' }],
// @ts-expect-error
[serviceRef, { a: 'a', b: 'b', c: 'c' }],
],
});
expect(backend).toBeDefined();
});
it('should start the test backend', async () => {
const testRef = createServiceRef<(v: string) => void>({ id: 'test' });
const testFn = jest.fn();
const sf = createServiceFactory({
deps: {},
service: testRef,
factory: async () => {
return async () => testFn;
},
});
const testModule = createBackendModule({
moduleId: 'test.module',
pluginId: 'test',
register(env) {
env.registerInit({
deps: {
test: testRef,
},
async init({ test }) {
test('winning');
},
});
},
});
await startTestBackend({
services: [sf],
registrables: [testModule({})],
});
expect(testFn).toBeCalledWith('winning');
});
});
@@ -0,0 +1,66 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Backend, createSpecializedBackend } from '@backstage/backend-app-api';
import {
AnyServiceFactory,
ServiceRef,
createServiceFactory,
BackendRegistrable,
} from '@backstage/backend-plugin-api';
/** @alpha */
export interface TestBackendOptions<TServices extends any[]> {
services: readonly [
...{
[index in keyof TServices]:
| AnyServiceFactory
| [ServiceRef<TServices[index]>, Partial<TServices[index]>];
},
];
}
/** @alpha */
export function createTestBackend<TServices extends any[]>(
options: TestBackendOptions<TServices>,
): Backend {
const factories =
options.services?.map(serviceDef => {
if (Array.isArray(serviceDef)) {
return createServiceFactory({
service: serviceDef[0],
deps: {},
factory: async () => async () => serviceDef[1],
});
}
return serviceDef as AnyServiceFactory;
}) ?? [];
return createSpecializedBackend({ services: factories });
}
/** @alpha */
export async function startTestBackend<TServices extends any[]>(
options: TestBackendOptions<TServices> & {
registrables?: BackendRegistrable[];
},
): Promise<void> {
const { registrables = [], ...otherOptions } = options;
const backend = createTestBackend(otherOptions);
for (const reg of registrables) {
backend.add(reg);
}
return backend.start();
}
@@ -0,0 +1,18 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { createTestBackend, startTestBackend } from './TestBackend';
export type { TestBackendOptions } from './TestBackend';