Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

test: introduces custom chai assertions #204

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module.exports = {
'no-plusplus': 'off',
'func-names': 'off',

// Disabled to allow "expect()" assertions with parameters:
// expect(foo).to.be.valid
'no-unused-expressions': 'off',

// Temporary overrides. Logic to be rewritten.
// TODO https://github.com/apiaryio/gavel.js/issues/150
'no-param-reassign': 'off'
Expand Down
107 changes: 107 additions & 0 deletions test/chai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-disable no-underscore-dangle */
const chai = require('chai');

const stringify = (obj) => {
return JSON.stringify(obj, null, 2);
};

chai.use(({ Assertion }, utils) => {
utils.addProperty(Assertion.prototype, 'valid', function() {
const { isValid } = this._obj;
const stringifiedObj = stringify(this._obj);

this.assert(
isValid === true,
`
Expected the following HTTP message field:

${stringifiedObj}

to have "isValid" equal #{exp}, but got #{act}'.
`,
`
Expected the following HTTP message field:

${stringifiedObj}

to be invalid, but it is actually valid.`
);
});

Assertion.addMethod('validator', function(expectedValue) {
const { validator: actualValue } = this._obj;
const stringifiedObj = stringify(this._obj);

this.assert(
actualValue === expectedValue,
`
Expected the following HTTP message field:

${stringifiedObj}

to have "${expectedValue}" validator, but got "${actualValue}".
`,
`
Expected the following HTTP message field:

${stringifiedObj}

to not have validator equal to "${expectedValue}".
`,
expectedValue,
actualValue
);
});

Assertion.addMethod('expectedType', function(expectedValue) {
const { expectedType: actualValue } = this._obj;
const stringifiedObj = stringify(this._obj);

this.assert(
actualValue === expectedValue,
`
Expected the following HTTP message field:

${stringifiedObj}

to have an "expectedType" equal to "${expectedValue}", but got "${actualValue}".
`,
`
Expected the following HTTP message field:

${stringifiedObj}

to not have an "expectedType" of "${expectedValue}".
`,
expectedValue,
actualValue
);
});

Assertion.addMethod('realType', function(expectedValue) {
const { realType: actualValue } = this._obj;
const stringifiedObj = stringify(this._obj);

this.assert(
actualValue === expectedValue,
`
Expected the following HTTP message field:

${stringifiedObj}

to have an "realType" equal to "${expectedValue}", but got "${actualValue}".
`,
`
Expected the following HTTP message field:

${stringifiedObj}

to not have an "realType" of "${expectedValue}".
`,
expectedValue,
actualValue
);
});
});

module.exports = chai;
Loading