Skip to content

Commit

Permalink
enable chaining of chai plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed May 14, 2024
1 parent 5ab5b86 commit a77feed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
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
52 changes: 25 additions & 27 deletions test/plugins.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import * as chai from '../index.js';
import {use, expect} from '../index.js';

describe('plugins', function () {
function plugin(chai) {
if (chai.Assertion.prototype.testing) return;

Object.defineProperty(chai.Assertion.prototype, 'testing', {
get: function () {
return 'successful';
},
});
}

function plugin (chai) {
if (chai.Assertion.prototype.testing) return;
function anotherPlugin(chai) {
if (chai.Assertion.prototype.moreTesting) return;

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

describe('plugins', function () {
it('basic usage', function () {
chai.use(plugin);
var expect = chai.expect;
const {expect} = use(plugin);
expect(expect('').testing).to.equal('successful');
});

it('double plugin', function () {
chai.expect(function () {
chai.use(plugin);
it('multiple plugins', function () {
expect(function () {
use(plugin).use(anotherPlugin);
}).to.not.throw();
});

it('.use detached from chai object', function () {
function anotherPlugin (chai) {
Object.defineProperty(chai.Assertion.prototype, 'moreTesting', {
get: function () {
return 'more success';
}
});
}

var use = chai.use;
use(anotherPlugin);

var expect = chai.expect;
const {expect} = use(anotherPlugin);

expect(expect('').moreTesting).to.equal('more success');
});
});

0 comments on commit a77feed

Please sign in to comment.