diff --git a/test/integration/bundle/bundle.test.ts b/test/integration/bundle/bundle.test.ts index 5f1af2d866f..bfad421e368 100644 --- a/test/integration/bundle/bundle.test.ts +++ b/test/integration/bundle/bundle.test.ts @@ -3,27 +3,31 @@ import fs from 'fs'; import path from 'path'; import { fileCleanup } from '../../helpers'; -const spec = fs.readFileSync('./test/integration/bundle/final-asyncapi.yaml', {encoding: 'utf-8'}); +const spec = fs.readFileSync('./test/integration/bundle/final-asyncapi.yaml', { encoding: 'utf-8' }); -function validateGeneratedSpec(filePath: string, spec: string) { - const generatedSPec = fs.readFileSync(path.resolve(filePath), { encoding: 'utf-8' }); - return generatedSPec === spec; +function validateGeneratedSpec(filePath: string, expectedSpec: string) { + const generatedSpec = fs.readFileSync(path.resolve(filePath), { encoding: 'utf-8' }); + if (generatedSpec !== expectedSpec) { + throw new Error(`Generated spec does not match expected spec at ${filePath}`); + } } -describe('bundle', () => { +describe('bundle', function () { + this.timeout(200000); // Global timeout for all tests in this suite + describe('bundle successful', () => { test .stdout() .command([ - 'bundle', './test/integration/bundle/first-asyncapi.yaml', + 'bundle', + './test/integration/bundle/first-asyncapi.yaml', '--output=./test/integration/bundle/final.yaml', ]) - .it('should successfully bundle specification', (ctx, done) => { + .it('should successfully bundle specification', async (ctx) => { expect(ctx.stdout).to.contain( 'Check out your shiny new bundled files at ./test/integration/bundle/final.yaml' ); fileCleanup('./test/integration/bundle/final.yaml'); - done(); }); }); @@ -31,27 +35,24 @@ describe('bundle', () => { test .stdout() .command([ - 'bundle', './test/integration/bundle/first-asyncapi.yaml', - '--output=./test/integration/bundle/final.json' + 'bundle', + './test/integration/bundle/first-asyncapi.yaml', + '--output=./test/integration/bundle/final.json', ]) - .it('should successfully bundle specification into json file', (ctx, done) => { + .it('should successfully bundle specification into json file', async (ctx) => { expect(ctx.stdout).to.contain( 'Check out your shiny new bundled files at ./test/integration/bundle/final.json' ); fileCleanup('./test/integration/bundle/final.json'); - done(); }); }); describe('when file path is wrong', () => { test .stderr() - .command([ - 'bundle', './test/integration/bundle/asyncapi.yml' - ]) - .it('should throw error message if the file path is wrong', (ctx, done) => { + .command(['bundle', './test/integration/bundle/asyncapi.yml']) + .it('should throw error message if the file path is wrong', async (ctx) => { expect(ctx.stderr).to.contain('Error: ENOENT: no such file or directory'); - done(); }); }); @@ -59,13 +60,17 @@ describe('bundle', () => { test .stdout() .command([ - 'bundle', './test/integration/bundle/first-asyncapi.yaml', './test/integration/bundle/feature.yaml', '--output=test/integration/bundle/final.yaml' + 'bundle', + './test/integration/bundle/first-asyncapi.yaml', + './test/integration/bundle/feature.yaml', + '--output=test/integration/bundle/final.yaml', ]) - .it('should be able to bundle multiple specs along with custom reference', (ctx, done) => { - expect(ctx.stdout).to.contain('Check out your shiny new bundled files at test/integration/bundle/final.yaml\n'); - expect(validateGeneratedSpec('test/integration/bundle/final.yaml', spec)); + .it('should be able to bundle multiple specs along with custom reference', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Check out your shiny new bundled files at test/integration/bundle/final.yaml\n' + ); + validateGeneratedSpec('test/integration/bundle/final.yaml', spec); fileCleanup('./test/integration/bundle/final.yaml'); - done(); }); }); @@ -73,27 +78,36 @@ describe('bundle', () => { test .stdout() .command([ - 'bundle', './test/integration/bundle/first-asyncapi.yaml', './test/integration/bundle/feature.yaml', '--output=test/integration/bundle/final.yaml', '--base=./test/integration/bundle/first-asyncapi.yaml' + 'bundle', + './test/integration/bundle/first-asyncapi.yaml', + './test/integration/bundle/feature.yaml', + '--output=test/integration/bundle/final.yaml', + '--base=./test/integration/bundle/first-asyncapi.yaml', ]) - .it('should be able to bundle correctly with overwriting base file', (ctx, done) => { - expect(ctx.stdout).to.contain('Check out your shiny new bundled files at test/integration/bundle/final.yaml\n'); - expect(validateGeneratedSpec('test/integration/bundle/final-asyncapi.yaml', spec)); + .it('should be able to bundle correctly with overwriting base file', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Check out your shiny new bundled files at test/integration/bundle/final.yaml\n' + ); + validateGeneratedSpec('test/integration/bundle/final-asyncapi.yaml', spec); fileCleanup('./test/integration/bundle/final.yaml'); - done(); }); }); }); -describe('bundle, with spec v3', () => { +describe('bundle, with spec v3', function () { + this.timeout(200000); // Set timeout for this specific describe block + test .stdout() .command([ - 'bundle', './test/integration/bundle/first-asyncapiv3.yaml', + 'bundle', + './test/integration/bundle/first-asyncapiv3.yaml', '--output=test/integration/bundle/final.yaml', - ]).it('should be able to bundle v3 spec correctly', (ctx, done) => { - expect(ctx.stdout).to.contain('Check out your shiny new bundled files at test/integration/bundle/final.yaml\n'); + ]) + .it('should be able to bundle v3 spec correctly', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Check out your shiny new bundled files at test/integration/bundle/final.yaml\n' + ); fileCleanup('./test/integration/bundle/final.yaml'); - done(); }); }); - diff --git a/test/integration/generate/fromTemplate.test.ts b/test/integration/generate/fromTemplate.test.ts index dd3535d1696..bc1795137f8 100644 --- a/test/integration/generate/fromTemplate.test.ts +++ b/test/integration/generate/fromTemplate.test.ts @@ -14,23 +14,29 @@ const generalOptions = [ const asyncapiv3 = './test/fixtures/specification-v3.yml'; function cleanup(filepath: string) { - rimraf.sync(filepath); + try { + rimraf.sync(filepath); + } catch (error) { + console.error(`Failed to clean up ${filepath}:`, error); + } } -describe('template', () => { +describe('template', function () { + this.timeout(200000); // Global timeout for all tests in this suite + after(() => { cleanup('./test/docs'); }); + test .stdout() .command([...generalOptions, '--output=./test/docs/1', '--force-write', '--no-interactive']) - .it('should generate minimal template', (ctx, done) => { + .it('should generate minimal template', async (ctx) => { console.log(ctx.stdout); expect(ctx.stdout).to.contain( 'Check out your shiny new generated files at ./test/docs/1.\n\n' ); cleanup('./test/docs/1'); - done(); }); describe('should handle AsyncAPI v3 document correctly', () => { @@ -43,12 +49,13 @@ describe('template', () => { '@asyncapi/minimaltemplate', nonInteractive, ]) - .it('give error on disabled template', (ctx, done) => { - expect(ctx.stderr).to.equal('Error: @asyncapi/minimaltemplate template does not support AsyncAPI v3 documents, please checkout some link\n'); + .it('give error on disabled template', async (ctx) => { + expect(ctx.stderr).to.equal( + 'Error: @asyncapi/minimaltemplate template does not support AsyncAPI v3 documents, please checkout some link\n' + ); expect(ctx.stdout).to.equal(''); - done(); }); - }).timeout(200000); + }); describe('should be able to use the new generator', () => { test @@ -62,10 +69,11 @@ describe('template', () => { '--force-write', '--use-new-generator', ]) - .it('should be able to generate using newer version of generator', (ctx, done) => { - expect(ctx.stdout).to.contain('Check out your shiny new generated files at ./test/docs/2.\n\n'); + .it('should be able to generate using newer version of generator', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Check out your shiny new generated files at ./test/docs/2.\n\n' + ); cleanup('./test/docs/2'); - done(); }); }); @@ -73,22 +81,18 @@ describe('template', () => { const pathToOutput = './test/docs/2'; before(() => { fs.mkdirSync(pathToOutput, { recursive: true }); - // Write a random file to trigger that dir has unstaged changes. - fs.writeFileSync(path.join(pathToOutput, 'random.md'), ''); + fs.writeFileSync(path.join(pathToOutput, 'random.md'), ''); // Trigger unstaged changes }); + test .stderr() .command([...generalOptions, `--output=${pathToOutput}`, nonInteractive]) - .it( - 'should throw error if output folder is in a git repository', - (ctx, done) => { - expect(ctx.stderr).to.contain( - `Error: "${pathToOutput}" is in a git repository with unstaged changes.` - ); - cleanup(pathToOutput); - done(); - } - ); + .it('should throw error if output folder is in a git repository', async (ctx) => { + expect(ctx.stderr).to.contain( + `Error: "${pathToOutput}" is in a git repository with unstaged changes.` + ); + cleanup(pathToOutput); + }); }); describe('custom params', () => { @@ -99,14 +103,13 @@ describe('template', () => { '-p=version=1.0.0 mode=development', '--output=./test/docs/3', '--force-write', - nonInteractive + nonInteractive, ]) - .it('should pass custom param in the template', (ctx, done) => { + .it('should pass custom param in the template', async (ctx) => { expect(ctx.stdout).to.contain( 'Check out your shiny new generated files at ./test/docs/3.\n\n' ); cleanup('./test/docs/3'); - done(); }); }); @@ -118,13 +121,12 @@ describe('template', () => { '--output=./test/docs/4', '--force-write', '-d=generate:after', - nonInteractive + nonInteractive, ]) - .it('should not create asyncapi.yaml file', async (_, done) => { - const exits = fs.existsSync(path.resolve('./docs/asyncapi.yaml')); - expect(!!exits).to.equal(false); + .it('should not create asyncapi.yaml file', async () => { + const exists = fs.existsSync(path.resolve('./docs/asyncapi.yaml')); + expect(!!exists).to.equal(false); cleanup('./test/docs/4'); - done(); }); }); @@ -136,16 +138,13 @@ describe('template', () => { '--output=./test/docs/5', '--force-write', '--debug', - nonInteractive + nonInteractive, ]) - .it('should print debug logs', (ctx, done) => { + .it('should print debug logs', async (ctx) => { expect(ctx.stdout).to.contain( - `Template sources taken from ${path.resolve( - './test/fixtures/minimaltemplate' - )}.` + `Template sources taken from ${path.resolve('./test/fixtures/minimaltemplate')}.` ); cleanup('./test/docs/5'); - done(); }); }); @@ -157,14 +156,13 @@ describe('template', () => { '--output=./test/docs/6', '--force-write', '--no-overwrite=./test/docs/asyncapi.md', - nonInteractive + nonInteractive, ]) - .it('should skip the filepath and generate normally', (ctx, done) => { + .it('should skip the filepath and generate normally', async (ctx) => { expect(ctx.stdout).to.contain( 'Check out your shiny new generated files at ./test/docs/6.\n\n' ); cleanup('./test/docs/6'); - done(); }); }); @@ -177,14 +175,15 @@ describe('template', () => { './test/fixtures/minimaltemplate', '--install', '--force-write', - '--output=./test/docs/7' + '--output=./test/docs/7', ]) - .it('should install template', (ctx, done) => { - expect(ctx.stdout).to.contain('Template installation started because you passed --install flag.'); + .it('should install template', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Template installation started because you passed --install flag.' + ); cleanup('./test/docs/7'); - done(); }); - }).timeout(1000000); + }); describe('map-base-url', () => { test @@ -197,15 +196,11 @@ describe('template', () => { '--force-write', '--map-base-url=https://schema.example.com/crm/:./test/fixtures/dummyspec', ]) - .it( - 'should resolve reference and generate from template', - (ctx, done) => { - expect(ctx.stdout).to.contain( - 'Check out your shiny new generated files at ./test/docs/8.\n\n' - ); - cleanup('./test/docs/8'); - done(); - } - ); + .it('should resolve reference and generate from template', async (ctx) => { + expect(ctx.stdout).to.contain( + 'Check out your shiny new generated files at ./test/docs/8.\n\n' + ); + cleanup('./test/docs/8'); + }); }); });