Skip to content

Commit 1d94319

Browse files
authored
Merge pull request #58 from berger-engineering-io/feat/plugin-openapi-lint-target
2 parents c3a10a0 + a2e7c3f commit 1d94319

4 files changed

Lines changed: 38 additions & 19 deletions

File tree

packages/plugin-openapi/project.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"packageRoot": "dist/{projectRoot}"
2929
}
3030
},
31+
"lint": {
32+
"executor": "@nx/eslint:lint"
33+
},
3134
"test": {
3235
"executor": "@nx/jest:jest",
3336
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],

packages/plugin-openapi/src/lib/openapi-tools-generator.spec.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { spawn } from 'node:child_process';
22
import { EventEmitter } from 'node:events';
33
import { OpenApiToolsGenerator } from './openapi-tools-generator';
4-
import { buildCommandArgs } from './utils/build-command';
5-
import { GeneratorContext } from '@nx-plugin-openapi/core';
4+
import {
5+
buildCommandArgs,
6+
OpenApiGeneratorOptions,
7+
} from './utils/build-command';
8+
import { GenerateOptionsBase, GeneratorContext } from '@nx-plugin-openapi/core';
69

710
// Mock node:child_process
811
jest.mock('node:child_process', () => ({
@@ -14,10 +17,17 @@ jest.mock('./utils/build-command', () => ({
1417
buildCommandArgs: jest.fn(),
1518
}));
1619

20+
type MockChildProcess = EventEmitter & { on: jest.Mock };
21+
type GeneratorWithCleanOutput = {
22+
cleanOutput: (...args: unknown[]) => unknown;
23+
};
24+
1725
describe('OpenApiToolsGenerator', () => {
1826
let generator: OpenApiToolsGenerator;
1927
let mockContext: GeneratorContext;
20-
let mockChildProcess: any;
28+
let mockChildProcess: MockChildProcess;
29+
let generatorWithCleanOutput: GeneratorWithCleanOutput;
30+
let cleanOutputSpy: jest.SpyInstance;
2131

2232
beforeEach(() => {
2333
jest.clearAllMocks();
@@ -28,7 +38,7 @@ describe('OpenApiToolsGenerator', () => {
2838
};
2939

3040
// Create a mock child process
31-
mockChildProcess = new EventEmitter();
41+
mockChildProcess = new EventEmitter() as MockChildProcess;
3242
mockChildProcess.on = jest.fn(mockChildProcess.on.bind(mockChildProcess));
3343
(spawn as jest.Mock).mockReturnValue(mockChildProcess);
3444

@@ -42,7 +52,10 @@ describe('OpenApiToolsGenerator', () => {
4252
]);
4353

4454
// Mock cleanOutput method
45-
jest.spyOn(generator as any, 'cleanOutput').mockImplementation(() => {});
55+
generatorWithCleanOutput = generator as unknown as GeneratorWithCleanOutput;
56+
cleanOutputSpy = jest
57+
.spyOn(generatorWithCleanOutput, 'cleanOutput')
58+
.mockImplementation(() => {});
4659
});
4760

4861
describe('name', () => {
@@ -69,7 +82,7 @@ describe('OpenApiToolsGenerator', () => {
6982

7083
await generatePromise;
7184

72-
expect((generator as any).cleanOutput).toHaveBeenCalledWith(
85+
expect(cleanOutputSpy).toHaveBeenCalledWith(
7386
mockContext,
7487
'src/generated'
7588
);
@@ -176,31 +189,31 @@ describe('OpenApiToolsGenerator', () => {
176189
generatorOptions: {
177190
generator: 'typescript-axios',
178191
},
179-
} as any;
192+
} as unknown as OpenApiGeneratorOptions & GenerateOptionsBase;
180193

181194
const generatePromise = generator.generate(options, mockContext);
182195

183196
// Simulate successful execution for all three
184197
const emitClose = () => mockChildProcess.emit('close', 0);
185198
await emitClose();
186-
await new Promise(resolve => setTimeout(resolve, 0));
199+
await new Promise((resolve) => setTimeout(resolve, 0));
187200
await emitClose();
188-
await new Promise(resolve => setTimeout(resolve, 0));
201+
await new Promise((resolve) => setTimeout(resolve, 0));
189202
await emitClose();
190203

191204
await generatePromise;
192205

193206
// Should clean output for each service
194-
expect((generator as any).cleanOutput).toHaveBeenCalledTimes(3);
195-
expect((generator as any).cleanOutput).toHaveBeenCalledWith(
207+
expect(cleanOutputSpy).toHaveBeenCalledTimes(3);
208+
expect(cleanOutputSpy).toHaveBeenCalledWith(
196209
mockContext,
197210
'src/api/users'
198211
);
199-
expect((generator as any).cleanOutput).toHaveBeenCalledWith(
212+
expect(cleanOutputSpy).toHaveBeenCalledWith(
200213
mockContext,
201214
'src/api/products'
202215
);
203-
expect((generator as any).cleanOutput).toHaveBeenCalledWith(
216+
expect(cleanOutputSpy).toHaveBeenCalledWith(
204217
mockContext,
205218
'src/api/orders'
206219
);
@@ -234,7 +247,7 @@ describe('OpenApiToolsGenerator', () => {
234247
products: 'products.yaml',
235248
},
236249
outputPath: 'src/api',
237-
} as any;
250+
} as unknown as OpenApiGeneratorOptions & GenerateOptionsBase;
238251

239252
const generatePromise = generator.generate(options, mockContext);
240253

@@ -254,7 +267,7 @@ describe('OpenApiToolsGenerator', () => {
254267
'product-service': 'specs/products.yaml',
255268
},
256269
outputPath: 'generated',
257-
} as any;
270+
} as unknown as OpenApiGeneratorOptions & GenerateOptionsBase;
258271

259272
const generatePromise = generator.generate(options, mockContext);
260273

packages/plugin-openapi/src/lib/utils/build-command.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ describe('buildCommandArgs', () => {
204204
const options: OpenApiGeneratorOptions = {
205205
inputSpec: 'api.yaml',
206206
outputPath: 'output',
207-
auth: null as any,
208-
packageName: null as any,
207+
auth: null as unknown as string,
208+
packageName: null as unknown as string,
209209
};
210210

211211
const result = buildCommandArgs(options);

packages/plugin-openapi/src/lib/utils/build-command.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ export function buildCommandArgs(options: OpenApiGeneratorOptions): string[] {
8181
args.push('-g', 'typescript-angular');
8282
args.push('-o', options.outputPath!);
8383

84-
for (const [optionKey, flagConfig] of Object.entries(OPTION_FLAG_MAP)) {
85-
const value = (options as any)[optionKey];
84+
for (const [optionKey, flagConfig] of Object.entries(OPTION_FLAG_MAP) as [
85+
keyof OpenApiGeneratorOptions,
86+
FlagConfig | string
87+
][]) {
88+
const value = options[optionKey] as unknown;
8689
if (
8790
value === undefined ||
8891
value === null ||

0 commit comments

Comments
 (0)