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

Enable chaining of chai plugins #1617

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions lib/chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {Assertion} from './chai/assertion.js';
import * as should from './chai/interface/should.js';
import {assert} from './chai/interface/assert.js';

const used = [];

// Assertion Error
export {AssertionError};

Expand All @@ -35,16 +33,14 @@ export function use(fn) {
expect,
assert,
Assertion,
...should
use,
...should,
};

if (!~used.indexOf(fn)) {
fn(exports, util);
used.push(fn);
}
fn(exports, util);

return exports;
};
}

// Utility Functions
export {util};
Expand Down
124 changes: 96 additions & 28 deletions test/plugins.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,110 @@
import * as chai from '../index.js';
import {use, assert, expect, Should} from '../index.js';

/**
* A chai plugin that adds the `testing` property on chai assertions.
*
* @param {unknown} chai
*/
function plugin(chai) {
if (chai.Assertion.prototype.testing) return;

chai.assert.testing = 'successful';

Object.defineProperty(chai.Assertion.prototype, 'testing', {
get: function () {
return 'successful';
},
});
Comment on lines +11 to +17
Copy link
Member Author

Choose a reason for hiding this comment

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

I thought it was surprising I had to do this. Unless I'm misunderstanding how this is supposed to work.

If it's correct that we need to define a property on both chai.assert and chai.Assertion.prototype I was thinking we could think about a more user friendly way to write plugins for the next breaking version.

Copy link
Contributor

Choose a reason for hiding this comment

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

you would normally use util.addProperty at least, like:

util.addProperty(Assertion.prototype, 'testing', function () {
  this.assert(this._obj === 'foo');
});

so expect and should will inherit that

but you're right you'd still have to manually extend the assert interface i think, something like:

assert.isTesting = function() {
  new Assertion(val).testing;
}

}

/**
* A chai plugin that adds the `moreTesting` property on chai assertions.
*
* @param {unknown} chai
*/
function anotherPlugin(chai) {
if (chai.Assertion.prototype.moreTesting) return;

chai.assert.moreTesting = 'more success';

Object.defineProperty(chai.Assertion.prototype, 'moreTesting', {
get: function () {
return 'more success';
},
});
}

/**
* A exmple of a "bad" plugin for chai that overwrites the `equal` property.
*
* @param {unknown} chai
*/
function brokenPlugin(chai) {
chai.overwriteProperty('equal', function (_super) {
if (something) {
return _super.call(this);
}
return someOtherThing();
});
}

describe('plugins', function () {
// Plugins are not applied "immutably" on chai so we want to just apply them
// here globally and then run all the tests.
use(plugin).use(anotherPlugin);

it("doesn't crash when there's a bad plugin", function () {
expect(() => {
use(brokenPlugin).use(brokenPlugin).use(brokenPlugin);
}).to.not.throw;
});

function plugin (chai) {
if (chai.Assertion.prototype.testing) return;
describe('should', () => {
before(() => {
Should();
});

it('basic usage', function () {
expect((42).should.testing).to.equal('successful');
});

Object.defineProperty(chai.Assertion.prototype, 'testing', {
get: function () {
return 'successful';
}
it('multiple plugins apply all changes', function () {
expect((42).should.testing).to.equal('successful');
expect((42).should.moreTesting).to.equal('more success');
});
}

it('basic usage', function () {
chai.use(plugin);
var expect = chai.expect;
expect(expect('').testing).to.equal('successful');
it('.use detached from chai object', function () {
expect((42).should.moreTesting).to.equal('more success');
});
});

it('double plugin', function () {
chai.expect(function () {
chai.use(plugin);
}).to.not.throw();
describe('expect', () => {
it('basic usage', function () {
expect(expect('').testing).to.equal('successful');
});

it('multiple plugins apply all changes', function () {
expect(expect('').testing).to.equal('successful');
expect(expect('').moreTesting).to.equal('more success');
});

it('.use detached from chai object', function () {
expect(expect('').moreTesting).to.equal('more success');
});
});

it('.use detached from chai object', function () {
function anotherPlugin (chai) {
Object.defineProperty(chai.Assertion.prototype, 'moreTesting', {
get: function () {
return 'more success';
}
});
}
describe('assert', () => {
it('basic usage', function () {
expect(assert.testing).to.equal('successful');
});

var use = chai.use;
use(anotherPlugin);
it('multiple plugins apply all changes', function () {
expect(assert.testing).to.equal('successful');
expect(assert.moreTesting).to.equal('more success');
});

var expect = chai.expect;
expect(expect('').moreTesting).to.equal('more success');
it('.use detached from chai object', function () {
expect(assert.moreTesting).to.equal('more success');
});
});
});