-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathgenerate-arazzo.test.ts
More file actions
95 lines (83 loc) · 2.75 KB
/
generate-arazzo.test.ts
File metadata and controls
95 lines (83 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { GenerateArazzoCommandArgv, handleGenerateArazzo } from '../../commands/generate-arazzo.js';
import { generate } from '@redocly/respect-core';
import { vi, describe, it, expect, beforeEach } from 'vitest';
import * as openapiCore from '@redocly/openapi-core';
vi.mock('@redocly/respect-core', async () => {
const actual = await vi.importActual<typeof import('@redocly/respect-core')>(
'@redocly/respect-core'
);
return {
...actual,
generate: vi.fn(),
};
});
vi.mock('@redocly/openapi-core', async () => {
const actual = await vi.importActual('@redocly/openapi-core');
return {
...actual,
logger: {
info: vi.fn(),
},
stringifyYaml: vi.fn(() => 'mocked yaml'),
};
});
describe('handleGenerateArazzo', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(generate).mockResolvedValue('{"mocked": "arazzo"}');
});
it('should call generate with the correct arguments', async () => {
const mockConfig = await openapiCore.createConfig({});
const commandArgs = {
argv: {
descriptionPath: 'openapi.yaml',
} as GenerateArazzoCommandArgv,
config: mockConfig,
version: '1.0.0',
collectSpecData: vi.fn(),
};
await handleGenerateArazzo(commandArgs);
expect(generate).toHaveBeenCalledWith({
outputFile: 'auto-generated.arazzo.yaml',
descriptionPath: 'openapi.yaml',
collectSpecData: commandArgs.collectSpecData,
version: '1.0.0',
config: mockConfig,
});
});
it('should use custom output file when provided', async () => {
const mockConfig = await openapiCore.createConfig({});
const commandArgs = {
argv: {
descriptionPath: 'openapi.yaml',
'output-file': 'custom.arazzo.yaml',
} as GenerateArazzoCommandArgv,
config: mockConfig,
version: '1.0.0',
collectSpecData: vi.fn(),
};
await handleGenerateArazzo(commandArgs);
expect(generate).toHaveBeenCalledWith({
outputFile: 'custom.arazzo.yaml',
descriptionPath: 'openapi.yaml',
collectSpecData: commandArgs.collectSpecData,
version: '1.0.0',
config: mockConfig,
});
});
it('should throw an error if the openapi file is not valid', async () => {
const mockConfig = await openapiCore.createConfig({});
const commandArgs = {
argv: {
descriptionPath: 'openapi.yaml',
} as GenerateArazzoCommandArgv,
config: mockConfig,
version: '1.0.0',
collectSpecData: vi.fn(),
};
vi.mocked(generate).mockRejectedValueOnce(new Error('Invalid OpenAPI file'));
await expect(handleGenerateArazzo(commandArgs)).rejects.toThrow(
'❌ Failed to generate Arazzo description. Check the output file path you provided, or the OpenAPI file content.'
);
});
});