It would make writing tests easier, assertions more readable, and fail messages more meaningful.
Take https://github.com/ckeditor/ckeditor5/pull/7356/commits/8888fcebde4516d60432a17706750b7e67fa28fe#diff-74026e14057514eeea5cabde2b2f809eR16-R31 as an example
Thanks to that you can write
expect( model.document.selection ).to.have.an.attribute( 'linkHref' );
// AssertionError: expected { Object (_selection) } to have 'linkHref' attribute
instead of
expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
// AssertionError: expected false to be true
So I have two questions:
-dev-tests?If you'd like to see this improvement implemented, add a 馃憤 reaction to this post.
About Questions:
- Does anybody have anything against such an approach?
Nope :) The solution is OK for me. I think that Assertion error messages could be smarter. Something like this (or anything that is fast to write)
expect( model.document.selection ).to.have.an.attribute( 'linkHref' );
// AssertionError: expected model:documentSelection to have 'linkHref' attribute
expect( paragraph ).to.have.an.attribute( 'linkHref' );
// AssertionError: expected model:element (paragraph) to have 'linkHref' attribute
// fall-back:
expect( { foo: 'bar' } ).to.have.an.attribute( 'linkHref' );
// AssertionError: expected { Object( foo ) } to have 'linkHref' attribute
- Where to store such assertions? -
-dev-tests?
ATM: ckeditor5-core/tests/_utils/.
Desired: ckeditor5-test-utils (#6313, #6657).
I think that Assertion error messages could be smarter. [...]
What you have shown here, is not a part of the assertion, but Chai's way to display object's and their names https://github.com/chaijs/chai/blob/058ddadb8422238b418d0c3e8f92e4f757289abd/lib/chai/utils/objDisplay.js#L27-L50
The way sinon-chai works it around, their check if the object in question is a spy, and puts the name defined in the spy into the message. Nice trick, but still requires a name to be defined per-object.
BTW, I wonder why Chai is not using the obj.constructor.name here, so:
expect( model.document.selection ).to.have.an.attribute( 'linkHref' );
// AssertionError: expected { DocumentSelection(_selection) } to have 'linkHref' attribute
I made a PR for that https://github.com/chaijs/chai/pull/1351, lets see what they will tell.
Would that be any batter for you?
Would that be any batter for you? If so, I'd make a PR to chai.
I was talking aboug #this part in message strings - we can provide them ourselfs:
`expected #{this} to have '${ type }' attribute`,
`expected #{this} to not have the '${ type }' attribute`,
by some simple check, like (just a sample):
const objLabel = obj.is && obj.is( 'model:element' ) ? "model:element " + obj.name : "#{this}";
and use that:
`expected ${ objLabel } to have '${ type }' attribute`,
So the string passed to chai would already have the proper object name.
Ah, I forgot we already have that is. Thanks for the clarification, makes perfect sense to me.
BTW, I think there is also chai's way to plugin with custom obj.inspect function, but your proposal is waaay simpler :)
Sounds like a really nice idea :+1: I love anything that brings more DSL (domain specific language) into tests.
Other examples that could be addressed:
expect( range ).to.be.collapsed() / `expect( range ).not.to.be.collapsed()expect( editor ).to.be.focused()Can I already start adding assertions useful for my PR to ckeditor5-core/tests/_utils/, should I wait for ckeditor5-test-utils, and then refactor all tests altogether?
Can I already start adding assertions useful for my PR to
ckeditor5-core/tests/_utils/,
For now is IMO fine. Waiting for that repo might be long ;)
PR waiting at https://github.com/ckeditor/ckeditor5/pull/7523
As for object name display - we might consider it later: https://github.com/ckeditor/ckeditor5/pull/7523#issuecomment-652818373.