fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Abstract repeated snapshot code into helper function

Created expectBundledSchemaToMatchSnapshot() helper to eliminate repeated code in all 5 bundle tests. The helper handles:
- Creating output and snapshot paths
- Ensuring directory exists
- Writing bundled result to temp file
- Comparing with snapshot

This reduces duplication and makes tests more maintainable.

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>

+27 -60
+27 -60
packages/json-schema-ref-parser/src/__tests__/bundle.test.ts
··· 11 11 const getSnapshotsPath = () => path.join(__dirname, '__snapshots__'); 12 12 const getTempSnapshotsPath = () => path.join(__dirname, '.gen', 'snapshots'); 13 13 14 + /** 15 + * Helper function to compare a bundled schema with a snapshot file. 16 + * Handles writing the schema to a temp file and comparing with the snapshot. 17 + * 18 + * @param schema - The bundled schema to compare 19 + * @param snapshotName - The name of the snapshot file (e.g., 'circular-ref-with-description.json') 20 + */ 21 + const expectBundledSchemaToMatchSnapshot = async (schema: unknown, snapshotName: string) => { 22 + const outputPath = path.join(getTempSnapshotsPath(), snapshotName); 23 + const snapshotPath = path.join(getSnapshotsPath(), snapshotName); 24 + 25 + // Ensure directory exists 26 + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 27 + 28 + // Write the bundled result 29 + const content = JSON.stringify(schema, null, 2); 30 + fs.writeFileSync(outputPath, content); 31 + 32 + // Compare with snapshot 33 + await expect(content).toMatchFileSnapshot(snapshotPath); 34 + }; 35 + 14 36 describe('bundle', () => { 15 37 it('handles circular reference with description', async () => { 16 38 const refParser = new $RefParser(); ··· 21 43 ); 22 44 const schema = await refParser.bundle({ pathOrUrlOrSchema }); 23 45 24 - const outputPath = path.join(getTempSnapshotsPath(), 'circular-ref-with-description.json'); 25 - const snapshotPath = path.join(getSnapshotsPath(), 'circular-ref-with-description.json'); 26 - 27 - // Ensure directory exists 28 - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 29 - 30 - // Write the bundled result 31 - const content = JSON.stringify(schema, null, 2); 32 - fs.writeFileSync(outputPath, content); 33 - 34 - // Compare with snapshot 35 - await expect(content).toMatchFileSnapshot(snapshotPath); 46 + await expectBundledSchemaToMatchSnapshot(schema, 'circular-ref-with-description.json'); 36 47 }); 37 48 38 49 it('bundles multiple references to the same file correctly', async () => { ··· 44 55 ); 45 56 const schema = await refParser.bundle({ pathOrUrlOrSchema }); 46 57 47 - const outputPath = path.join(getTempSnapshotsPath(), 'multiple-refs.json'); 48 - const snapshotPath = path.join(getSnapshotsPath(), 'multiple-refs.json'); 49 - 50 - // Ensure directory exists 51 - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 52 - 53 - // Write the bundled result 54 - const content = JSON.stringify(schema, null, 2); 55 - fs.writeFileSync(outputPath, content); 56 - 57 - // Compare with snapshot 58 - await expect(content).toMatchFileSnapshot(snapshotPath); 58 + await expectBundledSchemaToMatchSnapshot(schema, 'multiple-refs.json'); 59 59 }); 60 60 61 61 it('hoists sibling schemas from external files', async () => { ··· 67 67 ); 68 68 const schema = await refParser.bundle({ pathOrUrlOrSchema }); 69 69 70 - const outputPath = path.join(getTempSnapshotsPath(), 'main-with-external-siblings.json'); 71 - const snapshotPath = path.join(getSnapshotsPath(), 'main-with-external-siblings.json'); 72 - 73 - // Ensure directory exists 74 - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 75 - 76 - // Write the bundled result 77 - const content = JSON.stringify(schema, null, 2); 78 - fs.writeFileSync(outputPath, content); 79 - 80 - // Compare with snapshot 81 - await expect(content).toMatchFileSnapshot(snapshotPath); 70 + await expectBundledSchemaToMatchSnapshot(schema, 'main-with-external-siblings.json'); 82 71 }); 83 72 84 73 it('hoists sibling schemas from YAML files with versioned names (Redfish-like)', async () => { ··· 90 79 ); 91 80 const schema = await refParser.bundle({ pathOrUrlOrSchema }); 92 81 93 - const outputPath = path.join(getTempSnapshotsPath(), 'redfish-like.json'); 94 - const snapshotPath = path.join(getSnapshotsPath(), 'redfish-like.json'); 95 - 96 - // Ensure directory exists 97 - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 98 - 99 - // Write the bundled result 100 - const content = JSON.stringify(schema, null, 2); 101 - fs.writeFileSync(outputPath, content); 102 - 103 - // Compare with snapshot 104 - await expect(content).toMatchFileSnapshot(snapshotPath); 82 + await expectBundledSchemaToMatchSnapshot(schema, 'redfish-like.json'); 105 83 }); 106 84 107 85 it('fixes cross-file references (schemas in different external files)', async () => { ··· 113 91 ); 114 92 const schema = await refParser.bundle({ pathOrUrlOrSchema }); 115 93 116 - const outputPath = path.join(getTempSnapshotsPath(), 'cross-file-ref-main.json'); 117 - const snapshotPath = path.join(getSnapshotsPath(), 'cross-file-ref-main.json'); 118 - 119 - // Ensure directory exists 120 - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); 121 - 122 - // Write the bundled result 123 - const content = JSON.stringify(schema, null, 2); 124 - fs.writeFileSync(outputPath, content); 125 - 126 - // Compare with snapshot 127 - await expect(content).toMatchFileSnapshot(snapshotPath); 94 + await expectBundledSchemaToMatchSnapshot(schema, 'cross-file-ref-main.json'); 128 95 }); 129 96 });