diff --git a/packages/config-loader/src/sources/MutableConfigSource.test.ts b/packages/config-loader/src/sources/MutableConfigSource.test.ts index eebd276c4d..98c78005e4 100644 --- a/packages/config-loader/src/sources/MutableConfigSource.test.ts +++ b/packages/config-loader/src/sources/MutableConfigSource.test.ts @@ -16,24 +16,7 @@ import { ConfigSources } from './ConfigSources'; import { MutableConfigSource } from './MutableConfigSource'; -import { ConfigSource, ConfigSourceData } from './types'; - -function isResolved(promise: Promise): Promise { - return Promise.race([promise.then(() => true), Promise.resolve(false)]); -} - -async function readAll( - source: ConfigSource, - signal?: AbortSignal, -): Promise { - const results: ConfigSourceData[][] = []; - - for await (const { data } of source.readConfigData({ signal })) { - results.push(data); - } - - return results; -} +import { isResolved, readAll } from './__testUtils__/testUtils'; describe('MutableConfigSource', () => { it('should be initialized with data', async () => { diff --git a/packages/config-loader/src/sources/__testUtils__/testUtils.ts b/packages/config-loader/src/sources/__testUtils__/testUtils.ts new file mode 100644 index 0000000000..6b954a8489 --- /dev/null +++ b/packages/config-loader/src/sources/__testUtils__/testUtils.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2023 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 { ConfigSource, ConfigSourceData } from '../types'; + +export function isResolved( + promise: Promise, + { wait }: { wait?: number | boolean } = {}, +): Promise { + return Promise.race([ + promise.then(() => true), + typeof wait !== 'undefined' + ? new Promise(resolve => + setTimeout( + () => resolve(false), + typeof wait === 'number' ? wait : 10, + ), + ) + : Promise.resolve().then(() => false), + ]); +} + +export async function readAll( + source: ConfigSource, + signal?: AbortSignal, +): Promise { + const results: ConfigSourceData[][] = []; + + try { + for await (const { data } of source.readConfigData({ signal })) { + results.push(data); + } + } catch (error) { + throw error; + } + + return results; +}