Skip to content

Commit cf58b30

Browse files
authored
test: increase jest-openapi test coverage to 100% (#91)
* chore: update bug recreation template * test: tidy existing tests * test: sync tests for chai and jest plugins * tidy: sync chai and jest plugins * test: fix jest config
1 parent 86de0a6 commit cf58b30

File tree

17 files changed

+897
-134
lines changed

17 files changed

+897
-134
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ assignees: ''
77

88
---
99

10+
<!--- If you can, please recreate the bug in a quick test:
11+
12+
Simply clone master and use our bug recreation template for chai-openapi-response-validator (https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/chai-openapi-response-validator/test/bugRecreationTemplate.test.js)
13+
14+
or jest-openapi (https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/jest-openapi/__test__/bugRecreationTemplate.test.js)
15+
-->
16+
1017
**Which package are you using**?
1118
`chai-openapi-response-validator` / `jest-openapi`
1219

CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ A bug is a **recreatable** problem that is caused by the code in the repository.
1919

2020
Guidelines for bug reports:
2121

22-
1. **[Check if the issue has already been reported](https://github.com/RuntimeTools/OpenAPIValidators/issues)**
23-
2. **Check if the issue has been fixed** &mdash; try to reproduce it using the latest `master` or development branch in the repository.
24-
3. **Write a test recreating the bug** &mdash; [use this template to get started quickly](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/chai-openapi-response-validator/test/bug-recreation-template.test.js).
22+
1. **Check if the [issue has already been reported](https://github.com/RuntimeTools/OpenAPIValidators/issues)**
23+
2. **Recreate the bug** &mdash; clone `master` and write a quick test. You can use our bug recreation template for [`chai-openapi-response-validator`](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/chai-openapi-response-validator/test/bugRecreationTemplate.test.js) or [`jest-openapi`](https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/jest-openapi/__test__/bugRecreationTemplate.test.js).
2524

2625

2726
### Feature Requests
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
openapi: 3.0.0
22
info:
33
title: Example OpenApi 3 spec
4-
description: Has various paths with responses to use in testing
4+
description: Use to recreate a bug
55
version: 0.1.0
66
paths:
7-
/test:
7+
/recreate/bug:
88
get:
99
responses:
1010
'200':
1111
description: OK
1212
content:
1313
application/json:
1414
schema:
15-
allOf:
16-
- type: object
17-
properties:
18-
expectedProperty1:
19-
type: string
20-
- type: object
21-
properties:
22-
expectedProperty2:
23-
type: string
24-
# additionalProperties: false # Uncommenting this line exposes the bug
15+
type: object
16+
properties:
17+
expectedProperty1:
18+
type: string

packages/chai-openapi-response-validator/lib/openapi-validator/lib/classes/RequestPromiseResponse.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class RequestPromiseResponse extends AbstractResponse {
2222
// needs parsing into a JSON object, so just move to the next
2323
// block and return the body
2424
}
25-
2625
return this.body;
2726
}
2827
}

packages/chai-openapi-response-validator/test/assertions/satisfyApiSpec/differentRequestModules.test.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ describe('Parsing responses from different request modules', function () {
128128
});
129129
it('fails when using .not', function () {
130130
const assertion = () => expect(res).to.not.satisfyApiSpec;
131-
expect(assertion).to.throw('expected res not to satisfy');
132-
expect(assertion).to.throw(str({
133-
body: {},
134-
text: 'res.body is a string',
135-
}));
131+
expect(assertion).to.throw(
132+
AssertionError,
133+
str({
134+
body: {},
135+
text: 'res.body is a string',
136+
}),
137+
);
136138
});
137139
});
138140

@@ -146,11 +148,13 @@ describe('Parsing responses from different request modules', function () {
146148
});
147149
it('fails when using .not', function () {
148150
const assertion = () => expect(res).to.not.satisfyApiSpec;
149-
expect(assertion).to.throw('expected res not to satisfy');
150-
expect(assertion).to.throw(str({
151-
body: {},
152-
text: '',
153-
}));
151+
expect(assertion).to.throw(
152+
AssertionError,
153+
str({
154+
body: {},
155+
text: '',
156+
}),
157+
);
154158
});
155159
});
156160
});
@@ -204,11 +208,13 @@ describe('Parsing responses from different request modules', function () {
204208
});
205209
it('fails when using .not', function () {
206210
const assertion = () => expect(res).to.not.satisfyApiSpec;
207-
expect(assertion).to.throw('expected res not to satisfy');
208-
expect(assertion).to.throw(str({
209-
body: {},
210-
text: 'res.body is a string',
211-
}));
211+
expect(assertion).to.throw(
212+
AssertionError,
213+
str({
214+
body: {},
215+
text: 'res.body is a string',
216+
}),
217+
);
212218
});
213219
});
214220

@@ -241,11 +247,13 @@ describe('Parsing responses from different request modules', function () {
241247
});
242248
it('fails when using .not', function () {
243249
const assertion = () => expect(res).to.not.satisfyApiSpec;
244-
expect(assertion).to.throw('expected res not to satisfy');
245-
expect(assertion).to.throw(str({
246-
body: {},
247-
text: '',
248-
}));
250+
expect(assertion).to.throw(
251+
AssertionError,
252+
str({
253+
body: {},
254+
text: '',
255+
}),
256+
);
249257
});
250258
});
251259
});
@@ -360,6 +368,7 @@ describe('Parsing responses from different request modules', function () {
360368
after(function () {
361369
app.server.close();
362370
});
371+
363372
describe('json is set to true, res header is application/json, and res.body is a string', function () {
364373
let res;
365374
before(async function () {
@@ -383,6 +392,7 @@ describe('Parsing responses from different request modules', function () {
383392
);
384393
});
385394
});
395+
386396
describe('res header is application/json, and res.body is a string', function () {
387397
let res;
388398
before(async function () {

packages/chai-openapi-response-validator/test/assertions/satisfyApiSpec/satisfyApiSpec.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ openApiSpecs.forEach((spec) => {
4242
before(function () {
4343
chai.use(chaiResponseValidator(pathToApiSpec));
4444
});
45+
4546
describe('when \'res\' is not a valid HTTP response object', function () {
4647
const res = {
4748
status: 204,
@@ -187,7 +188,7 @@ openApiSpecs.forEach((spec) => {
187188
});
188189
});
189190

190-
describe('be a object with depth of over 2', function () {
191+
describe('be an object with depth of over 2', function () {
191192
const nestedObject = {
192193
a: {
193194
b: {

packages/chai-openapi-response-validator/test/bug-recreation-template.test.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const chai = require('chai');
2+
const path = require('path');
3+
4+
const chaiResponseValidator = require('..');
5+
6+
const dirContainingApiSpec = path.resolve('../../commonTestResources/exampleOpenApiFiles/valid/bugRecreationTemplate');
7+
const { expect } = chai;
8+
9+
describe('Recreate bug (issue #XX)', function () {
10+
before(function () {
11+
const pathToApiSpec = path.join(dirContainingApiSpec, 'openapi.yml');
12+
chai.use(chaiResponseValidator(pathToApiSpec));
13+
});
14+
15+
const res = {
16+
status: 200,
17+
req: {
18+
method: 'GET',
19+
path: '/recreate/bug',
20+
},
21+
body: {
22+
expectedProperty1: 'foo',
23+
},
24+
};
25+
26+
it('passes', function () {
27+
expect(res).to.satisfyApiSpec;
28+
});
29+
30+
it('fails when using .not', function () {
31+
const assertion = () => expect(res).to.not.satisfyApiSpec;
32+
expect(assertion).to.throw();
33+
});
34+
});

packages/jest-openapi/__test__/.eslintrc.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ env:
77
rules:
88
jest/prefer-expect-assertions: off
99
jest/no-disabled-tests: warn
10-
jest/lowercase-name: off
10+
jest/lowercase-name:
11+
- error
12+
- ignore:
13+
- describe
14+
jest/no-hooks: off
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('path');
2+
const { inspect } = require('util');
3+
4+
const jestOpenAPI = require('..');
5+
6+
const dirContainingApiSpec = path.resolve('../../commonTestResources/exampleOpenApiFiles/valid/bugRecreationTemplate');
7+
8+
describe('Recreate bug (issue #XX)', () => {
9+
beforeAll(() => {
10+
const pathToApiSpec = path.join(dirContainingApiSpec, 'openapi.yml');
11+
jestOpenAPI(pathToApiSpec);
12+
});
13+
14+
const res = {
15+
status: 200,
16+
req: {
17+
method: 'GET',
18+
path: '/recreate/bug',
19+
},
20+
body: {
21+
expectedProperty1: 'foo',
22+
},
23+
};
24+
25+
it('passes', () => {
26+
expect(res).toSatisfyApiSpec();
27+
});
28+
29+
it('fails when using .not', () => {
30+
const assertion = () => expect(res).not.toSatisfyApiSpec();
31+
expect(assertion).toThrow(
32+
inspect({
33+
body: {
34+
expectedProperty1: 'foo',
35+
},
36+
}),
37+
);
38+
});
39+
});

0 commit comments

Comments
 (0)