Skip to content

Commit

Permalink
Improved tests to avoid intermittent error on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemeri123 committed Jan 19, 2025
1 parent 5635245 commit ada4951
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 88 deletions.
80 changes: 47 additions & 33 deletions test/integration/bundle/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,111 @@ 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();
});
});

describe('bundle into json file', () => {
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();
});
});

describe('with custom reference', () => {
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();
});
});

describe('with base file', () => {
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();
});
});

105 changes: 50 additions & 55 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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
Expand All @@ -62,33 +69,30 @@ 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();
});
});

describe('git clash', () => {
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', () => {
Expand All @@ -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();
});
});

Expand All @@ -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();
});
});

Expand All @@ -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();
});
});

Expand All @@ -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();
});
});

Expand All @@ -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
Expand All @@ -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');
});
});
});

0 comments on commit ada4951

Please sign in to comment.