Skip to content

Commit f05380f

Browse files
Mac Minimichaelbe812
authored andcommitted
chore(hey-openapi): make generator and tests lint-compliant and rely on virtual mock for @hey-api/openapi-ts
1 parent c55282c commit f05380f

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { HeyOpenApiGenerator } from './hey-openapi-generator';
22
import { GeneratorContext } from '@nx-plugin-openapi/core';
33

4-
jest.mock('@hey-api/openapi-ts', () => ({
5-
generate: jest.fn(async () => undefined),
6-
}));
4+
jest.mock(
5+
'@hey-api/openapi-ts',
6+
() => ({
7+
generate: jest.fn(async () => undefined),
8+
}),
9+
{ virtual: true }
10+
);
711

812
describe('HeyOpenApiGenerator', () => {
913
let generator: HeyOpenApiGenerator;
@@ -28,19 +32,21 @@ describe('HeyOpenApiGenerator', () => {
2832
});
2933

3034
it('should call openapi-ts generate for single spec', async () => {
31-
const { generate } = (await import('@hey-api/openapi-ts')) as any;
35+
const mod = (await import('@hey-api/openapi-ts')) as unknown as {
36+
generate: jest.Mock;
37+
};
3238

3339
await generator.generate(
3440
{
3541
inputSpec: 'api.yaml',
3642
outputPath: 'src/generated',
3743
generatorOptions: { client: 'fetch' },
38-
} as any,
44+
} as unknown as Parameters<HeyOpenApiGenerator['generate']>[0],
3945
mockContext
4046
);
4147

4248
expect(cleanOutputSpy).toHaveBeenCalledWith(mockContext, 'src/generated');
43-
expect(generate).toHaveBeenCalledWith(
49+
expect(mod.generate).toHaveBeenCalledWith(
4450
expect.objectContaining({
4551
input: 'api.yaml',
4652
output: '/workspace/src/generated',
@@ -50,25 +56,27 @@ describe('HeyOpenApiGenerator', () => {
5056
});
5157

5258
it('should call openapi-ts generate for multiple specs', async () => {
53-
const { generate } = (await import('@hey-api/openapi-ts')) as any;
59+
const mod = (await import('@hey-api/openapi-ts')) as unknown as {
60+
generate: jest.Mock;
61+
};
5462

5563
await generator.generate(
5664
{
5765
inputSpec: { users: 'users.yaml', products: 'products.yaml' },
5866
outputPath: 'src/api',
59-
} as any,
67+
} as unknown as Parameters<HeyOpenApiGenerator['generate']>[0],
6068
mockContext
6169
);
6270

6371
expect(cleanOutputSpy).toHaveBeenCalledTimes(2);
64-
expect(generate).toHaveBeenCalledTimes(2);
65-
expect(generate).toHaveBeenCalledWith(
72+
expect(mod.generate).toHaveBeenCalledTimes(2);
73+
expect(mod.generate).toHaveBeenCalledWith(
6674
expect.objectContaining({
6775
input: 'users.yaml',
6876
output: '/workspace/src/api/users',
6977
})
7078
);
71-
expect(generate).toHaveBeenCalledWith(
79+
expect(mod.generate).toHaveBeenCalledWith(
7280
expect.objectContaining({
7381
input: 'products.yaml',
7482
output: '/workspace/src/api/products',

packages/plugin-hey-openapi/src/lib/hey-openapi-generator.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ export class HeyOpenApiGenerator
3131

3232
if (typeof inputSpec === 'string') {
3333
this.cleanOutput(ctx, outputPath);
34-
await this.invokeOpenApiTs(
35-
{
36-
input: inputSpec,
37-
output: join(ctx.root, outputPath),
38-
...generatorOptions,
39-
},
40-
ctx
41-
);
34+
await this.invokeOpenApiTs({
35+
input: inputSpec,
36+
output: join(ctx.root, outputPath),
37+
...generatorOptions,
38+
});
4239
} else {
4340
const entries = Object.entries(inputSpec as Record<string, string>) as [
4441
string,
@@ -51,23 +48,19 @@ export class HeyOpenApiGenerator
5148
logger.info(`Generating service: ${serviceName}`);
5249
const serviceOutputPath = join(outputPath, serviceName);
5350
this.cleanOutput(ctx, serviceOutputPath);
54-
await this.invokeOpenApiTs(
55-
{
56-
input: specPath,
57-
output: join(ctx.root, serviceOutputPath),
58-
...generatorOptions,
59-
},
60-
ctx
61-
);
51+
await this.invokeOpenApiTs({
52+
input: specPath,
53+
output: join(ctx.root, serviceOutputPath),
54+
...generatorOptions,
55+
});
6256
}
6357
}
6458

6559
logger.info(`hey-openapi code generation completed successfully`);
6660
}
6761

6862
private async invokeOpenApiTs(
69-
config: { input: string; output: string } & Record<string, unknown>,
70-
_ctx: GeneratorContext
63+
config: { input: string; output: string } & Record<string, unknown>
7164
): Promise<void> {
7265
let mod: Record<string, unknown>;
7366
try {
@@ -79,11 +72,16 @@ export class HeyOpenApiGenerator
7972
);
8073
}
8174

75+
const generateExport = (mod as Record<string, unknown>)['generate'];
76+
const createClientExport = (mod as Record<string, unknown>)['createClient'];
77+
8278
const fn =
83-
typeof (mod as any).generate === 'function'
84-
? ((mod as any).generate as (cfg: unknown) => Promise<unknown>)
85-
: typeof (mod as any).createClient === 'function'
86-
? ((mod as any).createClient as (cfg: unknown) => Promise<unknown>)
79+
typeof generateExport === 'function'
80+
? (generateExport as (cfg: Record<string, unknown>) => Promise<unknown>)
81+
: typeof createClientExport === 'function'
82+
? (createClientExport as (
83+
cfg: Record<string, unknown>
84+
) => Promise<unknown>)
8785
: undefined;
8886

8987
if (!fn) {
@@ -95,7 +93,7 @@ export class HeyOpenApiGenerator
9593
);
9694
}
9795

98-
await fn(config);
96+
await fn(config as Record<string, unknown>);
9997
}
10098
}
10199

0 commit comments

Comments
 (0)