Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows writing lint-friendly tests #297

Merged
merged 7 commits into from
Nov 10, 2014
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 lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ module.exports = function (_chai, util) {
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
};

Assertion.addChainableNoop = function(name, fn) {
util.addChainableMethod(this.prototype, name, function() { }, fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create the NOOP function as a file-wide variable and only reference it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree... The only thing I'm wondering is, do we want to document this method for use by plugin authors? If so, perhaps we should just make a more idiomatic function for chainable assert terminators. addChainableAssertion maybe?

};

Assertion.overwriteProperty = function (name, fn) {
util.overwriteProperty(this.prototype, name, fn);
};
Expand Down
18 changes: 9 additions & 9 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('ok', function () {
Assertion.addChainableNoop('ok', function () {
this.assert(
flag(this, 'object')
, 'expected #{this} to be truthy'
Expand All @@ -209,7 +209,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('true', function () {
Assertion.addChainableNoop('true', function () {
this.assert(
true === flag(this, 'object')
, 'expected #{this} to be true'
Expand All @@ -230,7 +230,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('false', function () {
Assertion.addChainableNoop('false', function () {
this.assert(
false === flag(this, 'object')
, 'expected #{this} to be false'
Expand All @@ -251,7 +251,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('null', function () {
Assertion.addChainableNoop('null', function () {
this.assert(
null === flag(this, 'object')
, 'expected #{this} to be null'
Expand All @@ -271,7 +271,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('undefined', function () {
Assertion.addChainableNoop('undefined', function () {
this.assert(
undefined === flag(this, 'object')
, 'expected #{this} to be undefined'
Expand All @@ -296,7 +296,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('exist', function () {
Assertion.addChainableNoop('exist', function () {
this.assert(
null != flag(this, 'object')
, 'expected #{this} to exist'
Expand All @@ -320,7 +320,7 @@ module.exports = function (chai, _) {
* @api public
*/

Assertion.addProperty('empty', function () {
Assertion.addChainableNoop('empty', function () {
var obj = flag(this, 'object')
, expected = obj;

Expand Down Expand Up @@ -361,8 +361,8 @@ module.exports = function (chai, _) {
);
}

Assertion.addProperty('arguments', checkArguments);
Assertion.addProperty('Arguments', checkArguments);
Assertion.addChainableNoop('arguments', checkArguments);
Assertion.addChainableNoop('Arguments', checkArguments);

/**
* ### .equal(value)
Expand Down
24 changes: 24 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ describe('expect', function () {
expect(false).to.not.be.true;
expect(1).to.not.be.true;

expect(true).to.be.true();
expect(true).to.be.true().and.not.false;

err(function(){
expect('test').to.be.true;
}, "expected 'test' to be true")
Expand All @@ -26,6 +29,9 @@ describe('expect', function () {
expect(1).to.be.ok;
expect(0).to.not.be.ok;

expect(true).to.be.ok();
expect(true).to.be.ok().and.not.false;

err(function(){
expect('').to.be.ok;
}, "expected '' to be truthy");
Expand All @@ -40,6 +46,9 @@ describe('expect', function () {
expect(true).to.not.be.false;
expect(0).to.not.be.false;

expect(false).to.be.false();
expect(false).to.be.false().and.not.true;

err(function(){
expect('').to.be.false;
}, "expected '' to be false")
Expand All @@ -49,6 +58,9 @@ describe('expect', function () {
expect(null).to.be.null;
expect(false).to.not.be.null;

expect(null).to.be.null();
expect(null).to.be.null().and.not.ok();

err(function(){
expect('').to.be.null;
}, "expected '' to be null")
Expand All @@ -59,6 +71,9 @@ describe('expect', function () {
expect(undefined).to.be.undefined;
expect(null).to.not.be.undefined;

expect(undefined).to.be.undefined();
expect(undefined).to.be.undefined().and.not.ok();

err(function(){
expect('').to.be.undefined;
}, "expected '' to be undefined")
Expand All @@ -69,6 +84,9 @@ describe('expect', function () {
, bar;
expect(foo).to.exist;
expect(bar).to.not.exist;

expect(foo).to.exist();
expect(foo).to.exist().and.contain('bar');
});

it('arguments', function(){
Expand All @@ -77,6 +95,9 @@ describe('expect', function () {
expect([]).to.not.be.arguments;
expect(args).to.be.an('arguments').and.be.arguments;
expect([]).to.be.an('array').and.not.be.Arguments;

expect(args).to.be.Arguments();
expect(args).to.be.arguments().and.be.ok;
});

it('.equal()', function(){
Expand Down Expand Up @@ -355,6 +376,9 @@ describe('expect', function () {
expect({}).to.be.empty;
expect({foo: 'bar'}).not.to.be.empty;

expect('').to.be.empty();
expect('').to.be.empty().and.exist;

err(function(){
expect('').not.to.be.empty;
}, "expected \'\' not to be empty");
Expand Down