-
-
Notifications
You must be signed in to change notification settings - Fork 696
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
Can I refactor chai messages? #286
Comments
That makes sense to me, @logicalparadox? |
I also have a question. Why, in some cases, chai doesn't show the actual value? e.g. function an (type, msg) {
if (msg) flag(this, 'message', msg);
type = type.toLowerCase();
var obj = flag(this, 'object')
, article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a ';
this.assert(
type === _.type(obj)
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
} While refactoring messages I could also make sure this.assert(
type === _.type(obj)
, msg[article]
, msg.not[article]
, type
, _.type(obj)
); Is there a reason for not doing this? |
Yeah, go for it. The reason this hasn't been done yet is probably because nobody offered to do it ;) |
Hey guys, I'm almost done implementing the new messages structure by I'm facing a dilema: to have more, simpler messages or to have fewer, more complex messages. I started off with a structure like this: {
a: "expected #{this} to be a #{exp}",
an: "expected #{this} to be an #{exp}",
above: 'expected #{this} to be above #{exp}',
. . .
not: {
a: "expected #{this} not to be a #{exp}",
an: "expected #{this} not to be an #{exp}",
above: 'expected #{this} to be at most #{exp}'
. . .
}
} That's the simpler messages option. You'd use it like Then I though the messages had too little variation, specially between the normal and the negate messages of an assertion, so I came up with this: {
a: 'expected #{this} #{not}to be #{article:#{exp}}',
above: 'expected #{this} to be #{+:above}#{-:at most} #{exp}',
} And then I updated the
This, in fact, eliminates the need for a |
Very cool! I have a couple of thoughts before we get to removing
Now, for My guess for the easiest way is that if the negation flag is present but no negation message was provided, assume that the normal message can be composed into a negated message. Otherwise, looking good! |
Good. Since I sent that message I went ahead with the "smarter" messages approach (it would be easy to revert anyway). I like the idea of differentiating placeholders from other template structures. Here's a proposal based on what I felt is needed and your considerations:
Most of these is somewhat implemented (I'll have to update the syntax). In favor of metadata and to be able to control how data is displayed from a single place, I've created some "meta" classes in the namespace
All this types implement the Do you guys think there is any demand to translate chai messages? If so, I'll take that into account as I implement this. |
This all looks good to me; looking forward to seeing the code :) I think that if this much work is being put into a new message manager, having multi-lingual support is a nice bit of icing on the cake. @vesln Any thoughts? |
@svallory I'd love to see your progress on this. Would you like to put it in a PR so we can all look at it, and maybe continue on from your great sounding work 😄 |
Hey Guys, I'm sorry to leave this hanging for so long. I'll take a look at the code I've done, rebase it and make a PR as soon as possible. |
Hey @svallory, just checking in to see if you made any progress with this? |
@keithamus and guys sorry for The reeeeealy huge delay on this. But I come bearing good news! I've got some time to work on it this week. I'll make a PR as soon as I get it to pass all the tests (hopefully today!). I have 18 to go. |
@svallory really looking forward to this. Can't wait 😄 |
Closing this alongside #393:
|
As of now Chai error messages are embedded into code and don't follow a strict rule. Take, for example, the
assertAbove
method...when flag
doLength
is true, Chai usesotherwise it uses
This makes it harder to improve messages by overwriting the
getMessages
method, which was my approach to modify the message before it was sent away.My idea is to extract all messages to a module which would return a hash so messages would be referenced by a string id. Something like...
lib/chai/core/messages.js
This would allow plugins to translate, improve, add color or even replace all messages to the liking of the author.
I'm volunteering to code it and make a pull request. Should I proceed?
The text was updated successfully, but these errors were encountered: