Skip to content

Commit ad0e99b

Browse files
chore: improve performance of e2e tests (#2731)
1 parent fa20dc3 commit ad0e99b

16 files changed

Lines changed: 1193 additions & 1021 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { existsSync, statSync, readFileSync } from 'node:fs';
2+
import { join, dirname } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import { getCommandOutput, getParams, cleanupOutput } from '../helpers.js';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
const indexEntryPoint = join(process.cwd(), 'packages/cli/lib/index.js');
9+
10+
describe('build-docs', () => {
11+
const folderPath = __dirname;
12+
13+
test('simple build-docs', async () => {
14+
const testPath = join(folderPath, 'simple-build-docs');
15+
const args = getParams(indexEntryPoint, ['build-docs', 'pets.yaml']);
16+
const result = getCommandOutput(args, { testPath });
17+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
18+
19+
expect(existsSync(join(testPath, 'redoc-static.html'))).toEqual(true);
20+
});
21+
22+
test('build docs with config option', async () => {
23+
const testPath = join(folderPath, 'build-docs-with-config-option');
24+
const args = getParams(indexEntryPoint, [
25+
'build-docs',
26+
'nested/openapi.yaml',
27+
'--config=nested/redocly.yaml',
28+
'-o=nested/redoc-static.html',
29+
]);
30+
const result = getCommandOutput(args, { testPath });
31+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
32+
"
33+
Found nested/redocly.yaml and using 'openapi' options
34+
Prerendering docs
35+
36+
🎉 bundled successfully in: nested/redoc-static.html (36 KiB) [⏱ <test>ms].
37+
"
38+
`);
39+
40+
expect(existsSync(join(testPath, 'nested/redoc-static.html'))).toEqual(true);
41+
expect(statSync(join(testPath, 'nested/redoc-static.html')).size).toEqual(36417);
42+
const output = readFileSync(join(testPath, 'nested/redoc-static.html'), 'utf8');
43+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
44+
});
45+
46+
describe('build docs with disableSearch', () => {
47+
test('build docs using an argv option', async () => {
48+
const testPath = join(folderPath, 'build-docs-with-disabled-search');
49+
const args = getParams(indexEntryPoint, [
50+
'build-docs',
51+
'openapi.yaml',
52+
'--theme.openapi.disableSearch',
53+
]);
54+
55+
const result = getCommandOutput(args, { testPath });
56+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
57+
"
58+
Prerendering docs
59+
60+
🎉 bundled successfully in: redoc-static.html (34 KiB) [⏱ <test>ms].
61+
"
62+
`);
63+
const output = readFileSync(join(testPath, 'redoc-static.html'), 'utf8');
64+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
65+
});
66+
67+
test('build docs using a config', async () => {
68+
const testPath = join(folderPath, 'build-docs-with-disabled-search');
69+
const args = getParams(indexEntryPoint, [
70+
'build-docs',
71+
'openapi.yaml',
72+
'--config=config.yaml',
73+
]);
74+
75+
const result = getCommandOutput(args, { testPath });
76+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
77+
"
78+
Found config.yaml and using 'openapi' options
79+
Prerendering docs
80+
81+
🎉 bundled successfully in: redoc-static.html (34 KiB) [⏱ <test>ms].
82+
"
83+
`);
84+
const output = readFileSync(join(testPath, 'redoc-static.html'), 'utf8');
85+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
86+
});
87+
88+
test('build docs using an alias', async () => {
89+
const testPath = join(folderPath, 'build-docs-with-disabled-search');
90+
const args = getParams(indexEntryPoint, [
91+
'build-docs',
92+
'alias',
93+
'--config=config-with-alias.yaml',
94+
]);
95+
const result = getCommandOutput(args, { testPath });
96+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
97+
"
98+
Found config-with-alias.yaml and using 'openapi' options
99+
Prerendering docs
100+
101+
🎉 bundled successfully in: redoc-static.html (34 KiB) [⏱ <test>ms].
102+
"
103+
`);
104+
const output = readFileSync(join(testPath, 'redoc-static.html'), 'utf8');
105+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
106+
});
107+
108+
test('build docs using the file name (should use the alias config options)', async () => {
109+
const testPath = join(folderPath, 'build-docs-with-disabled-search');
110+
const args = getParams(indexEntryPoint, [
111+
'build-docs',
112+
'openapi.yaml',
113+
'--config=config-with-alias.yaml',
114+
]);
115+
const result = getCommandOutput(args, { testPath });
116+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
117+
"
118+
Found config-with-alias.yaml and using 'openapi' options
119+
Prerendering docs
120+
121+
🎉 bundled successfully in: redoc-static.html (34 KiB) [⏱ <test>ms].
122+
"
123+
`);
124+
const output = readFileSync(join(testPath, 'redoc-static.html'), 'utf8');
125+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
126+
});
127+
128+
test('build docs using a config with apis and a root option', async () => {
129+
const testPath = join(folderPath, 'build-docs-with-disabled-search');
130+
const args = getParams(indexEntryPoint, [
131+
'build-docs',
132+
'openapi.yaml',
133+
'--config=config-with-apis-and-root-option.yaml',
134+
]);
135+
const result = getCommandOutput(args, { testPath });
136+
expect(cleanupOutput(result)).toMatchInlineSnapshot(`
137+
"
138+
Found config-with-apis-and-root-option.yaml and using 'openapi' options
139+
Prerendering docs
140+
141+
🎉 bundled successfully in: redoc-static.html (34 KiB) [⏱ <test>ms].
142+
"
143+
`);
144+
const output = readFileSync(join(testPath, 'redoc-static.html'), 'utf8');
145+
await expect(output).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
146+
});
147+
});
148+
});

tests/e2e/bundle/bundle.test.ts

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { readdirSync, statSync } from 'node:fs';
2+
import { join, dirname } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import { getCommandOutput, getEntrypoints, getParams, cleanupOutput } from '../helpers.js';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
const indexEntryPoint = join(process.cwd(), 'packages/cli/lib/index.js');
9+
10+
describe('bundle', () => {
11+
const excludeFolders = [
12+
'bundle-remove-unused-components',
13+
'bundle-remove-unused-components-from-config',
14+
'bundle-remove-unused-components-from-api-config',
15+
'bundle-arazzo-valid-test-description',
16+
'bundle-no-output-without-inline-apis',
17+
];
18+
const folderPath = __dirname;
19+
const contents = readdirSync(folderPath).filter((folder) => !excludeFolders.includes(folder));
20+
21+
for (const file of contents) {
22+
const testPath = join(folderPath, file);
23+
if (statSync(testPath).isFile()) {
24+
continue;
25+
}
26+
27+
const entryPoints = getEntrypoints(testPath);
28+
29+
const args = getParams(indexEntryPoint, ['bundle', ...entryPoints]);
30+
31+
test(file, async () => {
32+
const result = getCommandOutput(args, { testPath });
33+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
34+
});
35+
}
36+
37+
test('bundle-arazzo-valid-test-description', async () => {
38+
const testPath = join(folderPath, 'bundle-arazzo-valid-test-description');
39+
const args = getParams(indexEntryPoint, ['bundle', 'museum.yaml']);
40+
41+
const result = getCommandOutput(args, { testPath });
42+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
43+
});
44+
45+
test('bundle should NOT be invoked IF no positional apis provided AND --output specified', async () => {
46+
const testPath = join(folderPath, 'bundle-no-output-without-inline-apis');
47+
const args = getParams(indexEntryPoint, ['bundle', '--output=dist']);
48+
const result = getCommandOutput(args, { testPath });
49+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
50+
});
51+
});
52+
53+
describe('bundle with option: remove-unused-components', () => {
54+
test.each(['oas2', 'oas3'])('%s: should remove unused components', async (type) => {
55+
const testPath = join(__dirname, `bundle-remove-unused-components/${type}`);
56+
const entryPoints = getEntrypoints(testPath);
57+
const args = [indexEntryPoint, 'bundle', '--remove-unused-components', ...entryPoints];
58+
const result = getCommandOutput(args, { testPath });
59+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
60+
join(testPath, 'remove-unused-components-snapshot.txt')
61+
);
62+
});
63+
});
64+
65+
describe('bundle with option in config: remove-unused-components', () => {
66+
test.each(['oas2', 'oas3'])('%s: should remove unused components', async (type) => {
67+
const testPath = join(__dirname, `bundle-remove-unused-components-from-config/${type}`);
68+
const entryPoints = getEntrypoints(testPath);
69+
const args = [indexEntryPoint, 'bundle', ...entryPoints];
70+
const result = getCommandOutput(args, { testPath });
71+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
72+
join(testPath, 'remove-unused-components-snapshot.txt')
73+
);
74+
});
75+
76+
test.each(['oas2-without-option', 'oas3-without-option'])(
77+
"%s: shouldn't remove unused components",
78+
async (type) => {
79+
const testPath = join(__dirname, `bundle-remove-unused-components-from-config/${type}`);
80+
const entryPoints = getEntrypoints(testPath);
81+
const args = [indexEntryPoint, 'bundle', ...entryPoints];
82+
const result = getCommandOutput(args, { testPath });
83+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
84+
join(testPath, 'without-remove-unused-components-snapshot.txt')
85+
);
86+
}
87+
);
88+
});
89+
90+
describe('bundle with option in api config: remove-unused-components', () => {
91+
test('oas2: should remove unused components', async () => {
92+
const testPath = join(__dirname, 'bundle-remove-unused-components-from-api-config/oas2');
93+
const args = getParams(indexEntryPoint, ['bundle', '--config=redocly.yaml']);
94+
const result = getCommandOutput(args, { testPath });
95+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
96+
join(testPath, 'remove-unused-components-snapshot.txt')
97+
);
98+
});
99+
100+
test('oas3: should remove unused components', async () => {
101+
const testPath = join(__dirname, 'bundle-remove-unused-components-from-api-config/oas3');
102+
const args = getParams(indexEntryPoint, ['bundle', '--config=redocly.yaml']);
103+
const result = getCommandOutput(args, { testPath });
104+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
105+
join(testPath, 'remove-unused-components-snapshot.txt')
106+
);
107+
});
108+
});
109+
110+
describe('bundle without option in api config: do not remove unused components', () => {
111+
test('oas2-without-option: should not remove unused components', async () => {
112+
const testPath = join(
113+
__dirname,
114+
'bundle-remove-unused-components-from-api-config/oas2-without-option'
115+
);
116+
const args = getParams(indexEntryPoint, ['bundle', '--config=redocly.yaml']);
117+
const result = getCommandOutput(args, { testPath });
118+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
119+
join(testPath, 'keep-unused-components-snapshot.txt')
120+
);
121+
});
122+
123+
test('oas3-without-option: should not remove unused components', async () => {
124+
const testPath = join(
125+
__dirname,
126+
'bundle-remove-unused-components-from-api-config/oas3-without-option'
127+
);
128+
const args = getParams(indexEntryPoint, ['bundle', '--config=redocly.yaml']);
129+
const result = getCommandOutput(args, { testPath });
130+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
131+
join(testPath, 'keep-unused-components-snapshot.txt')
132+
);
133+
});
134+
135+
test('oas3-with-decorator-off: should not remove unused components in the first api, but remove in the second', async () => {
136+
const testPath = join(
137+
__dirname,
138+
'bundle-remove-unused-components-from-api-config/oas3-with-decorator-off'
139+
);
140+
const args = getParams(indexEntryPoint, ['bundle', '--config=redocly.yaml']);
141+
const result = getCommandOutput(args, { testPath });
142+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
143+
join(testPath, 'keep-unused-components-snapshot.txt')
144+
);
145+
});
146+
147+
test('oas3-turn-off-with-flag: should not remove unused components even if the arg passed', async () => {
148+
const testPath = join(
149+
__dirname,
150+
'bundle-remove-unused-components-from-api-config/oas3-turn-off-with-flag'
151+
);
152+
const args = getParams(indexEntryPoint, [
153+
'bundle',
154+
'--config=redocly.yaml',
155+
'--remove-unused-components',
156+
]);
157+
const result = getCommandOutput(args, { testPath });
158+
await expect(cleanupOutput(result)).toMatchFileSnapshot(
159+
join(testPath, 'keep-unused-components-snapshot.txt')
160+
);
161+
});
162+
});
163+
164+
describe('bundle with option: dereferenced', () => {
165+
it('description should not be from $ref', async () => {
166+
const testPath = join(__dirname, `bundle-description-dereferenced`);
167+
const args = getParams(indexEntryPoint, ['bundle', 'test.yaml', '--dereferenced']);
168+
169+
const result = getCommandOutput(args, { testPath });
170+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot_2.txt'));
171+
});
172+
173+
it('discriminator mapping should be replaced with correct references to components', async () => {
174+
const testPath = join(__dirname, `discriminator-mapping`);
175+
const args = getParams(indexEntryPoint, ['bundle', 'main.yaml', '--dereferenced']);
176+
177+
const result = getCommandOutput(args, { testPath });
178+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot_2.txt'));
179+
});
180+
});
181+
182+
describe('bundle with long description', () => {
183+
it('description should not be in folded mode', async () => {
184+
const testPath = join(__dirname, `bundle-description-long`);
185+
const args = getParams(indexEntryPoint, ['bundle', 'test.yaml']);
186+
187+
const result = getCommandOutput(args, { testPath });
188+
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot_2.txt'));
189+
});
190+
});

0 commit comments

Comments
 (0)