|
| 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