Skip to content

Commit

Permalink
test: increase jest-openapi test coverage to 100% (#91)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rwalle61 authored May 31, 2020
1 parent 86de0a6 commit cf58b30
Show file tree
Hide file tree
Showing 17 changed files with 897 additions and 134 deletions.
7 changes: 7 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ assignees: ''

---

<!--- If you can, please recreate the bug in a quick test:
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)
or jest-openapi (https://github.com/RuntimeTools/OpenAPIValidators/blob/master/packages/jest-openapi/__test__/bugRecreationTemplate.test.js)
-->

**Which package are you using**?
`chai-openapi-response-validator` / `jest-openapi`

Expand Down
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ A bug is a **recreatable** problem that is caused by the code in the repository.

Guidelines for bug reports:

1. **[Check if the issue has already been reported](https://github.com/RuntimeTools/OpenAPIValidators/issues)**
2. **Check if the issue has been fixed** &mdash; try to reproduce it using the latest `master` or development branch in the repository.
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).
1. **Check if the [issue has already been reported](https://github.com/RuntimeTools/OpenAPIValidators/issues)**
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).


### Feature Requests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
openapi: 3.0.0
info:
title: Example OpenApi 3 spec
description: Has various paths with responses to use in testing
description: Use to recreate a bug
version: 0.1.0
paths:
/test:
/recreate/bug:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
allOf:
- type: object
properties:
expectedProperty1:
type: string
- type: object
properties:
expectedProperty2:
type: string
# additionalProperties: false # Uncommenting this line exposes the bug
type: object
properties:
expectedProperty1:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class RequestPromiseResponse extends AbstractResponse {
// needs parsing into a JSON object, so just move to the next
// block and return the body
}

return this.body;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ describe('Parsing responses from different request modules', function () {
});
it('fails when using .not', function () {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw('expected res not to satisfy');
expect(assertion).to.throw(str({
body: {},
text: 'res.body is a string',
}));
expect(assertion).to.throw(
AssertionError,
str({
body: {},
text: 'res.body is a string',
}),
);
});
});

Expand All @@ -146,11 +148,13 @@ describe('Parsing responses from different request modules', function () {
});
it('fails when using .not', function () {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw('expected res not to satisfy');
expect(assertion).to.throw(str({
body: {},
text: '',
}));
expect(assertion).to.throw(
AssertionError,
str({
body: {},
text: '',
}),
);
});
});
});
Expand Down Expand Up @@ -204,11 +208,13 @@ describe('Parsing responses from different request modules', function () {
});
it('fails when using .not', function () {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw('expected res not to satisfy');
expect(assertion).to.throw(str({
body: {},
text: 'res.body is a string',
}));
expect(assertion).to.throw(
AssertionError,
str({
body: {},
text: 'res.body is a string',
}),
);
});
});

Expand Down Expand Up @@ -241,11 +247,13 @@ describe('Parsing responses from different request modules', function () {
});
it('fails when using .not', function () {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw('expected res not to satisfy');
expect(assertion).to.throw(str({
body: {},
text: '',
}));
expect(assertion).to.throw(
AssertionError,
str({
body: {},
text: '',
}),
);
});
});
});
Expand Down Expand Up @@ -360,6 +368,7 @@ describe('Parsing responses from different request modules', function () {
after(function () {
app.server.close();
});

describe('json is set to true, res header is application/json, and res.body is a string', function () {
let res;
before(async function () {
Expand All @@ -383,6 +392,7 @@ describe('Parsing responses from different request modules', function () {
);
});
});

describe('res header is application/json, and res.body is a string', function () {
let res;
before(async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ openApiSpecs.forEach((spec) => {
before(function () {
chai.use(chaiResponseValidator(pathToApiSpec));
});

describe('when \'res\' is not a valid HTTP response object', function () {
const res = {
status: 204,
Expand Down Expand Up @@ -187,7 +188,7 @@ openApiSpecs.forEach((spec) => {
});
});

describe('be a object with depth of over 2', function () {
describe('be an object with depth of over 2', function () {
const nestedObject = {
a: {
b: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const chai = require('chai');
const path = require('path');

const chaiResponseValidator = require('..');

const dirContainingApiSpec = path.resolve('../../commonTestResources/exampleOpenApiFiles/valid/bugRecreationTemplate');
const { expect } = chai;

describe('Recreate bug (issue #XX)', function () {
before(function () {
const pathToApiSpec = path.join(dirContainingApiSpec, 'openapi.yml');
chai.use(chaiResponseValidator(pathToApiSpec));
});

const res = {
status: 200,
req: {
method: 'GET',
path: '/recreate/bug',
},
body: {
expectedProperty1: 'foo',
},
};

it('passes', function () {
expect(res).to.satisfyApiSpec;
});

it('fails when using .not', function () {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw();
});
});
6 changes: 5 additions & 1 deletion packages/jest-openapi/__test__/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ env:
rules:
jest/prefer-expect-assertions: off
jest/no-disabled-tests: warn
jest/lowercase-name: off
jest/lowercase-name:
- error
- ignore:
- describe
jest/no-hooks: off
39 changes: 39 additions & 0 deletions packages/jest-openapi/__test__/bugRecreationTemplate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require('path');
const { inspect } = require('util');

const jestOpenAPI = require('..');

const dirContainingApiSpec = path.resolve('../../commonTestResources/exampleOpenApiFiles/valid/bugRecreationTemplate');

describe('Recreate bug (issue #XX)', () => {
beforeAll(() => {
const pathToApiSpec = path.join(dirContainingApiSpec, 'openapi.yml');
jestOpenAPI(pathToApiSpec);
});

const res = {
status: 200,
req: {
method: 'GET',
path: '/recreate/bug',
},
body: {
expectedProperty1: 'foo',
},
};

it('passes', () => {
expect(res).toSatisfyApiSpec();
});

it('fails when using .not', () => {
const assertion = () => expect(res).not.toSatisfyApiSpec();
expect(assertion).toThrow(
inspect({
body: {
expectedProperty1: 'foo',
},
}),
);
});
});
3 changes: 0 additions & 3 deletions packages/jest-openapi/__test__/jest.config.js

This file was deleted.

Loading

0 comments on commit cf58b30

Please sign in to comment.