Skip to content

Commit e326e4f

Browse files
committed
Test improvements
- Replace many "any" types with narrower types (this isn't comprehensive, but is an improvement) - Fix some asynchronous calls being invoked as "void" in test blocks - Use same server start/stop routine in all tests
1 parent b685f58 commit e326e4f

File tree

80 files changed

+768
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+768
-678
lines changed

test/356.campaign.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import path from 'path';
22
import express from 'express';
33
import request from 'supertest';
4-
import { createApp } from './common/app';
4+
import { createApp, ExpressWithServer } from './common/app';
55
import * as packageJson from '../package.json';
66
import { expect } from 'chai';
77

88
describe(packageJson.name, () => {
9-
let app = null;
9+
let app: ExpressWithServer;
1010

1111
before(async () => {
1212
// Set up the express app
@@ -27,8 +27,8 @@ describe(packageJson.name, () => {
2727
);
2828
});
2929

30-
after(() => {
31-
app.server.close();
30+
after(async () => {
31+
await app.closeServer();
3232
});
3333

3434
it('create campaign should return 201', async () =>

test/440.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import express from 'express';
22
import request from 'supertest';
3-
import { createApp } from './common/app';
3+
import { createApp, ExpressWithServer } from './common/app';
44
import * as packageJson from '../package.json';
55
import { OpenAPIV3 } from '../src/framework/types';
66

77
describe(packageJson.name, () => {
8-
let app = null;
8+
let app: ExpressWithServer;
99

1010
before(async () => {
1111
// Set up the express app
@@ -61,8 +61,8 @@ describe(packageJson.name, () => {
6161
);
6262
});
6363

64-
after(() => {
65-
app.server.close();
64+
after(async () => {
65+
await app.closeServer();
6666
});
6767

6868
it('create campaign should return 200', async () =>

test/478.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import request from 'supertest';
22
import { OpenAPIV3 } from '../src/framework/types';
3-
import { createApp } from './common/app';
3+
import { createApp, ExpressWithServer } from './common/app';
44

55
describe('issue #478', () => {
6-
let app = null;
6+
let app: ExpressWithServer;
77

88
before(async () => {
99
// set up express app
@@ -38,8 +38,8 @@ describe('issue #478', () => {
3838
return app;
3939
});
4040

41-
after(() => {
42-
app.server.close();
41+
after(async () => {
42+
await app.closeServer();
4343
});
4444

4545
it('should return 200', async () =>

test/509.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import request from 'supertest';
2-
import { createApp } from './common/app';
2+
import { createApp, ExpressWithServer } from './common/app';
3+
import { OpenAPIV3 } from '../src/framework/types';
34

45
describe('509 schema.preprocessor', () => {
5-
let app = null;
6+
let app: ExpressWithServer;
67

78
before(async () => {
89
// set up express app
@@ -24,18 +25,17 @@ describe('509 schema.preprocessor', () => {
2425
},
2526
false,
2627
);
27-
return app;
2828
});
2929

30-
after(() => {
31-
app.server.close();
30+
after(async () => {
31+
await app.closeServer();
3232
});
3333

3434
it('should return 200', async () =>
3535
request(app).get(`${app.basePath}/users/aafasdf`).expect(200));
3636
});
3737

38-
function apiSpec(): any {
38+
function apiSpec(): OpenAPIV3.Document {
3939
return {
4040
openapi: '3.0.1',
4141
info: {

test/511.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import request from 'supertest';
2-
import { createApp } from './common/app';
2+
import { createApp, ExpressWithServer } from './common/app';
3+
import { OpenAPIV3 } from '../src/framework/types';
34

45
describe('511 schema.preprocessor inheritance', () => {
5-
let app = null;
6+
let app: ExpressWithServer;
67

78
before(async () => {
89
// set up express app
@@ -27,8 +28,8 @@ describe('511 schema.preprocessor inheritance', () => {
2728
return app;
2829
});
2930

30-
after(() => {
31-
app.server.close();
31+
after(async () => {
32+
await app.closeServer();
3233
});
3334

3435
it('should return 201', async () =>
@@ -43,7 +44,7 @@ describe('511 schema.preprocessor inheritance', () => {
4344
.expect(201));
4445
});
4546

46-
function apiSpec(): any {
47+
function apiSpec(): OpenAPIV3.Document {
4748
return {
4849
openapi: '3.0.0',
4950
info: {
@@ -86,7 +87,6 @@ function apiSpec(): any {
8687
components: {
8788
schemas: {
8889
PolyObject: {
89-
type: 'object',
9090
discriminator: {
9191
propertyName: 'object_type',
9292
mapping: {

test/535.spec.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
import express from 'express';
2-
import { Server } from 'http';
32
import request from 'supertest';
43
import * as OpenApiValidator from '../src';
54
import { OpenAPIV3 } from '../src/framework/types';
6-
import { startServer } from './common/app.common';
5+
import { ExpressWithServer, startServer } from './common/app.common';
76
import { deepStrictEqual } from 'assert';
87

98
describe('#535 - calling `middleware()` multiple times', () => {
10-
it('does not mutate the API specification', async () => {
11-
const apiSpec = createApiSpec();
9+
let apiSpec: OpenAPIV3.Document;
10+
let app: ExpressWithServer;
1211

13-
const app = await createApp(apiSpec);
14-
await request(app).get('/ping/GNU Sir Terry').expect(200, 'GNU Sir Terry');
15-
app.server.close();
12+
before(async () => {
13+
apiSpec = createApiSpec();
14+
app = await createApp(apiSpec);
15+
});
1616

17+
after(async () => {
18+
await app.closeServer();
19+
});
20+
21+
it('does not mutate the API specification', async () => {
22+
await request(app).get('/ping/GNU Sir Terry').expect(200, 'GNU Sir Terry');
1723
deepStrictEqual(apiSpec, createApiSpec());
1824
});
1925
});
2026

2127
async function createApp(
2228
apiSpec: OpenAPIV3.Document,
23-
): Promise<express.Express & { server?: Server }> {
24-
const app = express();
29+
): Promise<ExpressWithServer> {
30+
const app = express() as ExpressWithServer;
31+
app.basePath = '';
2532

2633
app.use(
2734
OpenApiValidator.middleware({

test/577.spec.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import express from 'express';
2-
import { Server } from 'http';
32
import request from 'supertest';
43
import * as OpenApiValidator from '../src';
54
import { OpenAPIV3 } from '../src/framework/types';
6-
import { startServer } from './common/app.common';
5+
import { ExpressWithServer, startServer } from './common/app.common';
76
import { deepStrictEqual } from 'assert';
87

98
describe('#577 - Exclude response validation that is not in api spec', () => {
10-
it('does not validate responses which are not present in the spec', async () => {
11-
const apiSpec = createApiSpec();
9+
let apiSpec: OpenAPIV3.Document;
10+
let app: ExpressWithServer;
11+
12+
before(async () => {
13+
apiSpec = createApiSpec();
14+
app = await createApp(apiSpec);
15+
});
1216

13-
const app = await createApp(apiSpec);
17+
after(async () => {
18+
await app.closeServer();
19+
});
20+
21+
it('does not validate responses which are not present in the spec', async () => {
1422
await request(app).get('/users').expect(200, 'some users');
1523
await request(app).post('/users').expect(201, 'Created!');
1624
await request(app).get('/example').expect(200, 'Example indeed');
17-
app.server.close();
18-
1925
deepStrictEqual(apiSpec, createApiSpec());
2026
});
2127
});
2228

2329
async function createApp(
2430
apiSpec: OpenAPIV3.Document,
25-
): Promise<express.Express & { server?: Server }> {
26-
const app = express();
31+
): Promise<ExpressWithServer> {
32+
const app = express() as ExpressWithServer;
33+
app.basePath = '';
2734

2835
app.use(
2936
OpenApiValidator.middleware({

test/699.spec.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'path';
22
import { expect } from 'chai';
33
import request from 'supertest';
4-
import { createApp } from './common/app';
5-
4+
import { createApp, ExpressWithServer } from './common/app';
5+
import { EovErrorHandler } from './common/app.common';
66
import { date, dateTime } from '../src/framework/base.serdes';
77

88
const apiSpecPath = path.join('test', 'resources', '699.yaml');
@@ -26,7 +26,7 @@ class BadDate extends Date {
2626
}
2727

2828
describe('699', () => {
29-
let app = null;
29+
let app: ExpressWithServer;
3030

3131
before(async () => {
3232
// set up express app
@@ -56,7 +56,7 @@ describe('699', () => {
5656
if (typeof req.params.id !== 'object') {
5757
throw new Error('Should be deserialized to ObjectId object');
5858
}
59-
let date = new Date('2020-12-20T07:28:19.213Z');
59+
const date = new Date('2020-12-20T07:28:19.213Z');
6060
res.json({
6161
id: req.params.id,
6262
creationDateTime: date,
@@ -82,20 +82,20 @@ describe('699', () => {
8282
}
8383
res.json(req.body);
8484
});
85-
app.use((err, req, res, next) => {
85+
app.use(<EovErrorHandler>((err, req, res, next) => {
8686
res.status(err.status ?? 500).json({
8787
message: err.message,
8888
code: err.status ?? 500,
8989
});
90-
});
90+
}));
9191
},
9292
false,
9393
);
9494
return app;
9595
});
9696

97-
after(() => {
98-
app.server.close();
97+
after(async () => {
98+
await app.closeServer();
9999
});
100100

101101
it('should control GOOD id format and get a response in expected format', async () =>
@@ -167,7 +167,7 @@ describe('699', () => {
167167
});
168168

169169
describe('699 serialize response components only', () => {
170-
let app = null;
170+
let app: ExpressWithServer;
171171

172172
before(async () => {
173173
// set up express app
@@ -193,18 +193,17 @@ describe('699 serialize response components only', () => {
193193
3005,
194194
(app) => {
195195
app.get([`${app.basePath}/users/:id?`], (req, res) => {
196-
debugger;
197196
if (typeof req.params.id !== 'string') {
198197
throw new Error('Should be not be deserialized to ObjectId object');
199198
}
200-
let date = new Date('2020-12-20T07:28:19.213Z');
201-
let result = {
199+
const date = new Date('2020-12-20T07:28:19.213Z');
200+
const result = {
202201
id: new ObjectID(req.params.id),
203202
creationDateTime: date,
204203
creationDate: date,
205204
shortOrLong: 'a',
206-
history: [{ modificationDate: undefined }],
207-
historyWithoutRef: [{ modificationDate: undefined }],
205+
history: [{} as { modificationDate?: ObjectID | Date }],
206+
historyWithoutRef: [{} as { modificationDate?: ObjectID | Date }],
208207
};
209208
if (req.query.baddateresponse === 'functionNotExists') {
210209
result.history[0].modificationDate = new ObjectID();
@@ -251,20 +250,20 @@ describe('699 serialize response components only', () => {
251250
// We let creationDate et al as String and it should also work (either in Date Object ou String 'date' format)
252251
res.json(req.body);
253252
});
254-
app.use((err, req, res, next) => {
253+
app.use(<EovErrorHandler>((err, req, res, next) => {
255254
res.status(err.status ?? 500).json({
256255
message: err.message,
257256
code: err.status ?? 500,
258257
});
259-
});
258+
}));
260259
},
261260
false,
262261
);
263262
return app;
264263
});
265264

266-
after(() => {
267-
app.server.close();
265+
after(async () => {
266+
await app.closeServer();
268267
});
269268

270269
it('should control GOOD id format and get a response in expected format', async () =>

0 commit comments

Comments
 (0)