From a77feedb39944d335180e3ee87d64f7135ba8286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Tue, 14 May 2024 10:38:00 +0200 Subject: [PATCH] enable chaining of chai plugins --- lib/chai.js | 12 ++++-------- test/plugins.js | 52 ++++++++++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/chai.js b/lib/chai.js index 507d9c76..6635c6c8 100644 --- a/lib/chai.js +++ b/lib/chai.js @@ -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}; @@ -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}; diff --git a/test/plugins.js b/test/plugins.js index 4db1f44d..4dc1ee6a 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -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'); }); });