Skip to content

Commit

Permalink
test: extract checkErrorWrapper and use it in other tests (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet authored Feb 11, 2021
1 parent d6008d5 commit f9f4a9e
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 536 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"get:name": "echo $npm_package_name",
"lint": "eslint --max-warnings 0 --config \".eslintrc\" \".\"",
"test:lib": "nyc --reporter=html --reporter=text mocha --exclude \"test/browser_test.js\" --recursive",
"test:browser": "npm run test:browser:cleanup && npm run bundle && cp \"dist/bundle.js\" \"test/sample_browser/\" && start-server-and-test \"http-server test/sample_browser --cors -s\" 8080 \"mocha --timeout 20000 test/browser_test.js\" && npm run test:browser:cleanup",
"test:browser": "npm run test:browser:cleanup && npm run bundle && shx cp \"dist/bundle.js\" \"test/sample_browser/\" && start-server-and-test \"http-server test/sample_browser --cors -s\" 8080 \"mocha --timeout 20000 test/browser_test.js\" && npm run test:browser:cleanup",
"test:browser:cleanup": "rimraf \"test/sample_browser/bundle.js\"",
"generate:readme:toc": "markdown-toc -i \"README.md\"",
"generate:assets": "npm run docs && npm run generate:readme:toc && npm run types && npm run bundle",
Expand Down Expand Up @@ -59,6 +59,7 @@
"puppeteer": "^7.0.1",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.4",
"shx": "^0.3.3",
"start-server-and-test": "^1.11.3",
"tsd-jsdoc": "^2.5.0",
"uglify-es": "^3.3.9"
Expand Down
139 changes: 69 additions & 70 deletions test/asyncapiSchemaFormatParser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,81 @@ const parser = require('../lib');
const chai = require('chai');
const fs = require('fs');
const path = require('path');
const { offset } = require('./testsUtils');
const { offset, checkErrorWrapper } = require('./testsUtils');

const expect = chai.expect;

describe('asyncapiSchemaFormatParser', function() {
it('should throw an error because of invalid schema', async function() {
const invalidAsyncapi = fs.readFileSync(path.resolve(__dirname, './wrong/invalid-payload-asyncapi-format.json'), 'utf8');
try {
await parser.parse(invalidAsyncapi);
} catch (e) {
expect(e.type).to.equal('https://github.com/asyncapi/parser-js/schema-validation-errors');
expect(e.title).to.equal('This is not a valid AsyncAPI Schema Object.');
expect(e.parsedJSON).to.deep.equal(JSON.parse(invalidAsyncapi));
expect(e.validationErrors).to.deep.equal([
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should match some schema in anyOf',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
const expectedErrorObject = {
type: 'https://github.com/asyncapi/parser-js/schema-validation-errors',
title: 'This is not a valid AsyncAPI Schema Object.',
parsedJSON: JSON.parse(invalidAsyncapi),
validationErrors: [{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
]);
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should match some schema in anyOf',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
}]
};
await checkErrorWrapper(async () => {
await parser.parse(invalidAsyncapi);
}, expectedErrorObject);
});
it('should not throw error if payload not provided', async function() {
const inputString = `{
Expand All @@ -99,4 +98,4 @@ describe('asyncapiSchemaFormatParser', function() {

expect(async () => await parser.parse(parsedInput)).to.not.throw();
});
});
});
Loading

0 comments on commit f9f4a9e

Please sign in to comment.