Skip to content

Commit c07487c

Browse files
committed
- Added test to check if errors are thrown correct
1 parent bbaa039 commit c07487c

File tree

14 files changed

+543
-19
lines changed

14 files changed

+543
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.12.0-alpha",
3+
"version": "0.12.0-beta",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

test/__snapshots__/index.spec.js.snap

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ export { CollectionFormatService } from './services/CollectionFormatService';
571571
export { ComplexService } from './services/ComplexService';
572572
export { DefaultsService } from './services/DefaultsService';
573573
export { DuplicateService } from './services/DuplicateService';
574+
export { ErrorService } from './services/ErrorService';
574575
export { HeaderService } from './services/HeaderService';
575576
export { MultipleTags1Service } from './services/MultipleTags1Service';
576577
export { MultipleTags2Service } from './services/MultipleTags2Service';
@@ -2179,6 +2180,42 @@ export class DuplicateService {
21792180
}"
21802181
`;
21812182

2183+
exports[`v2 should generate: ./test/generated/v2/services/ErrorService.ts 1`] = `
2184+
"/* istanbul ignore file */
2185+
/* tslint:disable */
2186+
/* eslint-disable */
2187+
import type { CancelablePromise } from '../core/CancelablePromise';
2188+
import { request as __request } from '../core/request';
2189+
import { OpenAPI } from '../core/OpenAPI';
2190+
2191+
export class ErrorService {
2192+
2193+
/**
2194+
* @param status Status code to return
2195+
* @returns any Custom message: Successful response
2196+
* @throws ApiError
2197+
*/
2198+
public static testErrorCode(
2199+
status: string,
2200+
): CancelablePromise<any> {
2201+
return __request({
2202+
method: 'POST',
2203+
path: \`/api/v\${OpenAPI.VERSION}/error\`,
2204+
query: {
2205+
'status': status,
2206+
},
2207+
errors: {
2208+
500: \`Custom message: Internal Server Error\`,
2209+
501: \`Custom message: Not Implemented\`,
2210+
502: \`Custom message: Bad Gateway\`,
2211+
503: \`Custom message: Service Unavailable\`,
2212+
},
2213+
});
2214+
}
2215+
2216+
}"
2217+
`;
2218+
21822219
exports[`v2 should generate: ./test/generated/v2/services/HeaderService.ts 1`] = `
21832220
"/* istanbul ignore file */
21842221
/* tslint:disable */
@@ -3197,6 +3234,7 @@ export { CollectionFormatService } from './services/CollectionFormatService';
31973234
export { ComplexService } from './services/ComplexService';
31983235
export { DefaultsService } from './services/DefaultsService';
31993236
export { DuplicateService } from './services/DuplicateService';
3237+
export { ErrorService } from './services/ErrorService';
32003238
export { FormDataService } from './services/FormDataService';
32013239
export { HeaderService } from './services/HeaderService';
32023240
export { MultipartService } from './services/MultipartService';
@@ -5227,6 +5265,42 @@ export class DuplicateService {
52275265
}"
52285266
`;
52295267

5268+
exports[`v3 should generate: ./test/generated/v3/services/ErrorService.ts 1`] = `
5269+
"/* istanbul ignore file */
5270+
/* tslint:disable */
5271+
/* eslint-disable */
5272+
import type { CancelablePromise } from '../core/CancelablePromise';
5273+
import { request as __request } from '../core/request';
5274+
import { OpenAPI } from '../core/OpenAPI';
5275+
5276+
export class ErrorService {
5277+
5278+
/**
5279+
* @param status Status code to return
5280+
* @returns any Custom message: Successful response
5281+
* @throws ApiError
5282+
*/
5283+
public static testErrorCode(
5284+
status: number,
5285+
): CancelablePromise<any> {
5286+
return __request({
5287+
method: 'POST',
5288+
path: \`/api/v\${OpenAPI.VERSION}/error\`,
5289+
query: {
5290+
'status': status,
5291+
},
5292+
errors: {
5293+
500: \`Custom message: Internal Server Error\`,
5294+
501: \`Custom message: Not Implemented\`,
5295+
502: \`Custom message: Bad Gateway\`,
5296+
503: \`Custom message: Service Unavailable\`,
5297+
},
5298+
});
5299+
}
5300+
5301+
}"
5302+
`;
5303+
52305304
exports[`v3 should generate: ./test/generated/v3/services/FormDataService.ts 1`] = `
52315305
"/* istanbul ignore file */
52325306
/* tslint:disable */

test/e2e/scripts/browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function start() {
1313
args: ['--no-sandbox', '--disable-setuid-sandbox'],
1414
});
1515
page = await browser.newPage();
16-
page.on('console', msg => console.log(msg.text()));
16+
// page.on('console', msg => console.log(msg.text()));
1717
await page.goto(`http://localhost:3000/`, {
1818
waitUntil: 'networkidle0',
1919
});

test/e2e/scripts/server.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ async function start(dir) {
2727
res.send('<script src="js/script.js"></script>');
2828
});
2929

30+
// Register an 'echo' server for testing error codes. This will just grab the
31+
// status code from the query and return the default response (and text) from Express.
32+
// See the spec files for more information.
33+
app.all('/base/api/v1.0/error', (req, res) => {
34+
const status = parseInt(req.query.status);
35+
res.sendStatus(status);
36+
});
37+
3038
// Register an 'echo' server that just returns all data from the API calls.
3139
// Although this might not be a 'correct' response, we can use this to test
3240
// the majority of API calls.
33-
app.all('/base/api/*', (req, res) => {
41+
app.all('/base/api/v1.0/*', (req, res) => {
3442
setTimeout(() => {
3543
res.json({
3644
method: req.method,
@@ -45,6 +53,7 @@ async function start(dir) {
4553
}, 100);
4654
});
4755

56+
4857
server = app.listen(3000, resolve);
4958
});
5059
}

test/e2e/v2.axios.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('v2.node', () => {
3737
});
3838

3939
it('can abort the request', async () => {
40+
let error;
4041
try {
4142
const { SimpleService } = require('./generated/v2/axios/index.js');
4243
const promise = SimpleService.getCallWithoutParametersAndResponse();
@@ -45,7 +46,8 @@ describe('v2.node', () => {
4546
}, 10);
4647
await promise;
4748
} catch (e) {
48-
expect(e.message).toContain('The user aborted a request.');
49+
error = e.message;
4950
}
51+
expect(error).toContain('The user aborted a request.');
5052
});
5153
});

test/e2e/v2.fetch.spec.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('v2.fetch', () => {
4545
});
4646

4747
it('can abort the request', async () => {
48+
let error;
4849
try {
4950
await browser.evaluate(async () => {
5051
const { SimpleService } = window.api;
@@ -55,7 +56,65 @@ describe('v2.fetch', () => {
5556
await promise;
5657
});
5758
} catch (e) {
58-
expect(e.message).toContain('The user aborted a request.');
59+
error = e.message;
5960
}
61+
expect(error).toContain('The user aborted a request.');
62+
});
63+
64+
it('should throw known error (500)', async () => {
65+
const error = await browser.evaluate(async () => {
66+
try {
67+
const { ErrorService } = window.api;
68+
await ErrorService.testErrorCode(500);
69+
} catch (e) {
70+
return JSON.stringify({
71+
name: e.name,
72+
message: e.message,
73+
url: e.url,
74+
status: e.status,
75+
statusText: e.statusText,
76+
body: e.body,
77+
});
78+
}
79+
});
80+
81+
expect(error).toBe(
82+
JSON.stringify({
83+
name: 'ApiError',
84+
message: 'Custom message: Internal Server Error',
85+
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
86+
status: 500,
87+
statusText: 'Internal Server Error',
88+
body: 'Internal Server Error',
89+
})
90+
);
91+
});
92+
93+
it('should throw unknown error (409)', async () => {
94+
const error = await browser.evaluate(async () => {
95+
try {
96+
const { ErrorService } = window.api;
97+
await ErrorService.testErrorCode(409);
98+
} catch (e) {
99+
return JSON.stringify({
100+
name: e.name,
101+
message: e.message,
102+
url: e.url,
103+
status: e.status,
104+
statusText: e.statusText,
105+
body: e.body,
106+
});
107+
}
108+
});
109+
expect(error).toBe(
110+
JSON.stringify({
111+
name: 'ApiError',
112+
message: 'Generic Error',
113+
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
114+
status: 409,
115+
statusText: 'Conflict',
116+
body: 'Conflict',
117+
})
118+
);
60119
});
61120
});

test/e2e/v2.node.spec.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('v2.node', () => {
3737
});
3838

3939
it('can abort the request', async () => {
40+
let error;
4041
try {
4142
const { SimpleService } = require('./generated/v2/node/index.js');
4243
const promise = SimpleService.getCallWithoutParametersAndResponse();
@@ -45,7 +46,62 @@ describe('v2.node', () => {
4546
}, 10);
4647
await promise;
4748
} catch (e) {
48-
expect(e.message).toContain('The user aborted a request.');
49+
error = e.message;
4950
}
51+
expect(error).toContain('The user aborted a request.');
52+
});
53+
54+
it('should throw known error (500)', async () => {
55+
let error;
56+
try {
57+
const { ErrorService } = require('./generated/v2/node/index.js');
58+
await ErrorService.testErrorCode(500);
59+
} catch (e) {
60+
error = JSON.stringify({
61+
name: e.name,
62+
message: e.message,
63+
url: e.url,
64+
status: e.status,
65+
statusText: e.statusText,
66+
body: e.body,
67+
});
68+
}
69+
expect(error).toBe(
70+
JSON.stringify({
71+
name: 'ApiError',
72+
message: 'Custom message: Internal Server Error',
73+
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
74+
status: 500,
75+
statusText: 'Internal Server Error',
76+
body: 'Internal Server Error',
77+
})
78+
);
79+
});
80+
81+
it('should throw unknown error (409)', async () => {
82+
let error;
83+
try {
84+
const { ErrorService } = require('./generated/v2/node/index.js');
85+
await ErrorService.testErrorCode(409);
86+
} catch (e) {
87+
error = JSON.stringify({
88+
name: e.name,
89+
message: e.message,
90+
url: e.url,
91+
status: e.status,
92+
statusText: e.statusText,
93+
body: e.body,
94+
});
95+
}
96+
expect(error).toBe(
97+
JSON.stringify({
98+
name: 'ApiError',
99+
message: 'Generic Error',
100+
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
101+
status: 409,
102+
statusText: 'Conflict',
103+
body: 'Conflict',
104+
})
105+
);
50106
});
51107
});

test/e2e/v2.xhr.spec.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const copy = require('./scripts/copy');
55
const compileWithTypescript = require('./scripts/compileWithTypescript');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
8+
const {ErrorService} = require("./generated/v3/node/index.js");
89

910
describe('v2.xhr', () => {
1011
beforeAll(async () => {
@@ -45,6 +46,7 @@ describe('v2.xhr', () => {
4546
});
4647

4748
it('can abort the request', async () => {
49+
let error;
4850
try {
4951
await browser.evaluate(async () => {
5052
const { SimpleService } = window.api;
@@ -55,7 +57,65 @@ describe('v2.xhr', () => {
5557
await promise;
5658
});
5759
} catch (e) {
58-
expect(e.message).toContain('The user aborted a request.');
60+
error = e.message;
5961
}
62+
expect(error).toContain('The user aborted a request.');
63+
});
64+
65+
it('should throw known error (500)', async () => {
66+
const error = await browser.evaluate(async () => {
67+
try {
68+
const { ErrorService } = window.api;
69+
await ErrorService.testErrorCode(500);
70+
} catch (e) {
71+
return JSON.stringify({
72+
name: e.name,
73+
message: e.message,
74+
url: e.url,
75+
status: e.status,
76+
statusText: e.statusText,
77+
body: e.body,
78+
});
79+
}
80+
});
81+
82+
expect(error).toBe(
83+
JSON.stringify({
84+
name: 'ApiError',
85+
message: 'Custom message: Internal Server Error',
86+
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
87+
status: 500,
88+
statusText: 'Internal Server Error',
89+
body: 'Internal Server Error',
90+
})
91+
);
92+
});
93+
94+
it('should throw unknown error (409)', async () => {
95+
const error = await browser.evaluate(async () => {
96+
try {
97+
const { ErrorService } = window.api;
98+
await ErrorService.testErrorCode(409);
99+
} catch (e) {
100+
return JSON.stringify({
101+
name: e.name,
102+
message: e.message,
103+
url: e.url,
104+
status: e.status,
105+
statusText: e.statusText,
106+
body: e.body,
107+
});
108+
}
109+
});
110+
expect(error).toBe(
111+
JSON.stringify({
112+
name: 'ApiError',
113+
message: 'Generic Error',
114+
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
115+
status: 409,
116+
statusText: 'Conflict',
117+
body: 'Conflict',
118+
})
119+
);
60120
});
61121
});

0 commit comments

Comments
 (0)