add a unit test
Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2024 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 { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
import path from 'path';
|
||||
|
||||
jest.mock(
|
||||
'lodash',
|
||||
() =>
|
||||
({
|
||||
...jest.requireActual('lodash'),
|
||||
debounce: (fn: any) => fn,
|
||||
} as any as typeof import('lodash')),
|
||||
);
|
||||
|
||||
describe('generateOpenApiSchema', () => {
|
||||
const inputDir = createMockDirectory();
|
||||
const outputDir = createMockDirectory();
|
||||
|
||||
beforeEach(() => {
|
||||
inputDir.clear();
|
||||
outputDir.clear();
|
||||
|
||||
inputDir.addContent({
|
||||
'openapi.yaml': `
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Test API
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/test:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
`,
|
||||
});
|
||||
jest.mock('../../../../../lib/openapi/helpers', () => ({
|
||||
getPathToCurrentOpenApiSpec: jest.fn(() =>
|
||||
Promise.resolve(path.join(inputDir.path, 'openapi.yaml')),
|
||||
),
|
||||
}));
|
||||
});
|
||||
it('should handle watch mode', async () => {
|
||||
const generateClientMock = jest.fn();
|
||||
const generateServerMock = jest.fn();
|
||||
jest.doMock('./client', () => ({ command: generateClientMock }));
|
||||
jest.doMock('./server', () => ({ command: generateServerMock }));
|
||||
|
||||
const mockWatch = jest.fn();
|
||||
jest.mock('chokidar', () => ({
|
||||
watch: mockWatch,
|
||||
}));
|
||||
let resolve: (val?: any) => void;
|
||||
const block = () =>
|
||||
new Promise(res => {
|
||||
resolve = res;
|
||||
});
|
||||
jest.mock('../../../../../lib/runner', () => ({
|
||||
block,
|
||||
}));
|
||||
|
||||
const mockOn: Record<string, any> = {};
|
||||
mockWatch.mockReturnValue({
|
||||
on: jest.fn((event, cb) => {
|
||||
console.log(event);
|
||||
mockOn[event] = cb;
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const { command } = await import('./index');
|
||||
|
||||
const actions = async () => {
|
||||
while (!mockOn.ready) {
|
||||
// Wait for the watcher to be registered
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
}
|
||||
|
||||
expect(generateClientMock).toHaveBeenCalledTimes(0);
|
||||
mockOn.change();
|
||||
expect(generateClientMock).toHaveBeenCalledTimes(1);
|
||||
resolve();
|
||||
};
|
||||
|
||||
await Promise.all([
|
||||
actions(),
|
||||
command({ watch: true, clientPackage: 'test123' }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -20,6 +20,7 @@ import { command as generateServer } from './server';
|
||||
import chokidar from 'chokidar';
|
||||
import { getPathToCurrentOpenApiSpec } from '../../../../../lib/openapi/helpers';
|
||||
import { debounce } from 'lodash';
|
||||
import { block } from '../../../../../lib/runner';
|
||||
|
||||
export async function command(opts: OptionValues) {
|
||||
if (!opts.clientPackage && !opts.server) {
|
||||
@@ -74,7 +75,7 @@ export async function command(opts: OptionValues) {
|
||||
'Watching for changes in OpenAPI spec. Press Ctrl+C to stop.',
|
||||
);
|
||||
});
|
||||
await new Promise(() => {});
|
||||
await block();
|
||||
} catch (err) {
|
||||
console.error(chalk.red('Error: ', err));
|
||||
process.exit(1);
|
||||
|
||||
@@ -67,3 +67,7 @@ export async function runner(
|
||||
|
||||
return resultsList;
|
||||
}
|
||||
|
||||
export async function block() {
|
||||
return new Promise(() => {});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user