-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a77feed
enable chaining of chai plugins
koddsson 44af64d
Add broken plugin test
koddsson 0b0e600
make multiple plugin test more concrete
koddsson 7a417be
add should plugin tests
koddsson d8815c8
add assert tests
koddsson 2e9e220
just apply plugins globally in tests
koddsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* 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'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andchai.Assertion.prototype
I was thinking we could think about a more user friendly way to write plugins for the next breaking version.There was a problem hiding this comment.
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: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: