From 76ecc39d27f7c9f8a2859976a4aaa56c0927da7c Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Mon, 24 Feb 2025 17:48:22 +0530 Subject: [PATCH] testcases added for preview command Signed-off-by: Gourav --- src/commands/start/preview.ts | 40 ++++++++++++++------------------ test/integration/preview.test.ts | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 test/integration/preview.test.ts diff --git a/src/commands/start/preview.ts b/src/commands/start/preview.ts index dae176ff985..7f056f28e9f 100644 --- a/src/commands/start/preview.ts +++ b/src/commands/start/preview.ts @@ -47,8 +47,8 @@ class StartPreview extends Command { for (const [key, value] of Object.entries(obj)) { if ( key === '$ref' && - typeof value === 'string' && - (value.startsWith('.') || value.startsWith('./') || value.startsWith('../')) + typeof value === 'string' && + (value.startsWith('.') || value.startsWith('./') || value.startsWith('../')) ) { const { filePath } = this.parseRef(value); const resolvedPath = path.resolve(basePath, filePath); @@ -81,12 +81,13 @@ class StartPreview extends Command { try { const document = await bundle(AsyncAPIFile); const fileContent = - outputFormat === '.yaml' || outputFormat === '.yml' - ? document.yml() - : JSON.stringify(document.json()); + outputFormat === '.yaml' || outputFormat === '.yml' + ? document.yml() + : JSON.stringify(document.json(), null, 2); fs.writeFileSync(bundledFilePath, fileContent, { encoding: 'utf-8' }); + this.log('Bundled file updated successfully.'); } catch (error: any) { - throw new Error(`Error bundling files: ${error.message}`); + this.error(`Error bundling files: ${error.message}`); } } @@ -95,6 +96,8 @@ class StartPreview extends Command { const port = flags.port; const filePath = args['spec-file']; + this.log('Starting Studio in preview mode...'); + this.specFile = await load(filePath); this.metricsMetadata.port = port; @@ -106,9 +109,7 @@ class StartPreview extends Command { const outputFormat = path.extname(AsyncAPIFile); if (!outputFormat) { - this.error( - 'Unable to determine file format from the provided AsyncAPI file.' - ); + this.error('Unable to determine file format from the provided AsyncAPI file.'); } const bundledFilePath = `./asyncapi-bundled${outputFormat}`; @@ -121,29 +122,24 @@ class StartPreview extends Command { ); this.findLocalRefFiles(asyncapiDocument, basePath, filesToWatch); - const watcher = chokidar.watch(Array.from(filesToWatch), { - persistent: true, - }); + const watcher = chokidar.watch(Array.from(filesToWatch), { persistent: true }); - await this.updateBundledFile( - AsyncAPIFile, - outputFormat, - bundledFilePath - ); + await this.updateBundledFile(AsyncAPIFile, outputFormat, bundledFilePath); watcher.on('change', async (changedPath) => { this.log(`File changed: ${changedPath}`); try { - await this.updateBundledFile( - AsyncAPIFile, - outputFormat, - bundledFilePath - ); + await this.updateBundledFile(AsyncAPIFile, outputFormat, bundledFilePath); } catch (error: any) { this.error(`Error updating bundled file: ${error.message}`); } }); + watcher.on('error', (error) => { + this.error(`Watcher error: ${error.message}`); + }); + + this.log(`Starting Studio on port ${port}`); startStudio(bundledFilePath, port); } } diff --git a/test/integration/preview.test.ts b/test/integration/preview.test.ts new file mode 100644 index 00000000000..e2da044eeac --- /dev/null +++ b/test/integration/preview.test.ts @@ -0,0 +1,40 @@ +import { test } from '@oclif/test'; +import fs from 'fs'; +import TestHelper from '../helpers'; +import { expect } from 'chai'; + +const testHelper = new TestHelper(); +const bundledFilePath = './asyncapi-bundled.yml'; + +describe('start preview', () => { + beforeEach(() => { + testHelper.createDummyContextFile(); + }); + + afterEach(() => { + testHelper.deleteDummyContextFile(); + if (fs.existsSync(bundledFilePath)) { + fs.unlinkSync(bundledFilePath); + } + }); + + test + .stderr() + .stdout() + .command(['start:preview', './test/fixtures/specification.yml']) + .it('bundles the AsyncAPI file and starts the studio', ({ stdout, stderr }) => { + const output = stdout + stderr; + expect(output).to.include('Starting Studio in preview mode...'); + expect(output).to.include('Bundled file updated successfully.'); + expect(fs.existsSync(bundledFilePath)).to.be.true; + }); + + test + .stderr() + .stdout() + .command(['start:preview', 'non-existing-context']) + .it('throws error if context does not exist', ({ stdout, stderr }) => { + const output = stdout + stderr; + expect(output).to.include('ContextError: Context "non-existing-context" does not exist'); + }); +});