Skip to content

Commit

Permalink
test: local server for mocking tests (#625)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Gornicki <[email protected]>
  • Loading branch information
chinma-yyy and derberg authored Jul 19, 2023
1 parent 80ec15b commit 631b4b1
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 36 deletions.
19 changes: 13 additions & 6 deletions test/commands/convert.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import path from 'path';
import { test } from '@oclif/test';
import { NO_CONTEXTS_SAVED } from '../../src/errors/context-error';
import TestHelper from '../testHelper';
import TestHelper, { createMockServer, stopMockServer } from '../testHelper';
import fs from 'fs-extra';
import { unlink, unlinkSync } from 'fs';

const testHelper = new TestHelper();
const filePath = './test/specification.yml';
Expand All @@ -19,6 +18,14 @@ describe('convert', () => {
testHelper.deleteDummyContextFile();
});

beforeAll(() => {
createMockServer();
});

afterAll(() => {
stopMockServer();
});

test
.stderr()
.stdout()
Expand All @@ -42,9 +49,9 @@ describe('convert', () => {
test
.stderr()
.stdout()
.command(['convert', 'https://bit.ly/asyncapi'])
.command(['convert', 'http://localhost:8080/dummySpec.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).toContain('URL https://bit.ly/asyncapi successfully converted!\n');
expect(ctx.stdout).toContain('URL http://localhost:8080/dummySpec.yml successfully converted!\n');
expect(ctx.stderr).toEqual('');
done();
});
Expand Down Expand Up @@ -95,7 +102,7 @@ describe('convert', () => {
}
}
});

test
.stderr()
.stdout()
Expand Down Expand Up @@ -170,4 +177,4 @@ describe('convert', () => {
done();
});
});
});
});
13 changes: 10 additions & 3 deletions test/commands/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
/* eslint-disable sonarjs/no-identical-functions */
import path from 'path';
import { test } from '@oclif/test';
import { createMockServer, stopMockServer } from '../../testHelper';
const generalOptions = ['generate:models'];
const outputDir = './test/commands/generate/models';

describe('models', () => {
beforeAll(() => {
createMockServer();
});
afterAll(() => {
stopMockServer();
});
test
.stderr()
.stdout()
.command([...generalOptions, 'typescript', 'http://bit.ly/asyncapi'])
.command([...generalOptions, 'typescript', 'http://localhost:8080/dummySpec.yml'])
.it('works with remote AsyncAPI files', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toMatchSnapshot();
Expand Down Expand Up @@ -355,10 +362,10 @@ describe('models', () => {
test
.stderr()
.stdout()
.command([...generalOptions, 'typescript', 'http://bit.ly/asyncapi', '--log-diagnostics'])
.command([...generalOptions, 'typescript', 'http://localhost:8080/dummySpec.yml', '--log-diagnostics'])
.it('works with remote AsyncAPI files', (ctx, done) => {
expect(ctx.stderr).toEqual('');
expect(ctx.stdout).toMatch('URL http://bit.ly/asyncapi is valid but has (itself and/or referenced documents) governance issues.');
expect(ctx.stdout).toMatch('URL http://localhost:8080/dummySpec.yml is valid but has (itself and/or referenced documents) governance issues.');
done();
});
});
Expand Down
15 changes: 11 additions & 4 deletions test/commands/optimize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path';
import { test } from '@oclif/test';
import { NO_CONTEXTS_SAVED } from '../../src/errors/context-error';
import TestHelper from '../testHelper';
import TestHelper, { createMockServer, stopMockServer } from '../testHelper';
import inquirer from 'inquirer';
import {Optimizations, Outputs} from '../../src/commands/optimize';

Expand All @@ -20,6 +19,14 @@ describe('optimize', () => {
testHelper.deleteDummyContextFile();
});

beforeAll(() => {
createMockServer();
});

afterAll(() => {
stopMockServer();
});

test
.stderr()
.stdout()
Expand All @@ -43,9 +50,9 @@ describe('optimize', () => {
test
.stderr()
.stdout()
.command(['optimize', 'https://bit.ly/asyncapi'])
.command(['optimize', 'http://localhost:8080/dummySpec.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).toContain('No optimization has been applied since https://bit.ly/asyncapi looks optimized!');
expect(ctx.stdout).toContain('No optimization has been applied since http://localhost:8080/dummySpec.yml looks optimized!');
expect(ctx.stderr).toEqual('');
done();
});
Expand Down
46 changes: 27 additions & 19 deletions test/commands/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import path from 'path';
import { test } from '@oclif/test';
import { NO_CONTEXTS_SAVED } from '../../src/errors/context-error';
import TestHelper from '../testHelper';
import TestHelper, { createMockServer, stopMockServer } from '../testHelper';

const testHelper = new TestHelper();

Expand All @@ -12,11 +12,19 @@ describe('validate', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

beforeAll(() => {
createMockServer();
});

afterAll(() => {
stopMockServer();
});

test
.stderr()
.stdout()
Expand All @@ -26,7 +34,7 @@ describe('validate', () => {
expect(ctx.stderr).toEqual('');
done();
});

test
.stderr()
.stdout()
Expand All @@ -52,9 +60,9 @@ describe('validate', () => {
test
.stderr()
.stdout()
.command(['validate', 'https://bit.ly/asyncapi'])
.command(['validate', 'http://localhost:8080/dummySpec.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).toMatch('URL https://bit.ly/asyncapi is valid but has (itself and/or referenced documents) governance issues.\n\nhttps://bit.ly/asyncapi');
expect(ctx.stdout).toMatch('URL http://localhost:8080/dummySpec.yml is valid but has (itself and/or referenced documents) governance issues.\n\nhttp://localhost:8080/dummySpec.yml');
expect(ctx.stderr).toEqual('');
done();
});
Expand All @@ -69,16 +77,16 @@ describe('validate', () => {
done();
});
});

describe('with context names', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
Expand All @@ -89,7 +97,7 @@ describe('validate', () => {
expect(ctx.stderr).toEqual('');
done();
});

test
.stderr()
.stdout()
Expand All @@ -100,7 +108,7 @@ describe('validate', () => {
done();
});
});

describe('with no arguments', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
Expand All @@ -110,7 +118,7 @@ describe('validate', () => {
testHelper.setCurrentContext('home');
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
Expand All @@ -121,7 +129,7 @@ describe('validate', () => {
expect(ctx.stderr).toEqual('');
done();
});

test
.stderr()
.stdout()
Expand All @@ -147,7 +155,7 @@ describe('validate', () => {
}
}
});

test
.stderr()
.stdout()
Expand All @@ -163,11 +171,11 @@ describe('validate', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
Expand All @@ -193,11 +201,11 @@ describe('validate', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
Expand All @@ -223,11 +231,11 @@ describe('validate', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
Expand Down
Loading

0 comments on commit 631b4b1

Please sign in to comment.