-
-
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
Ability to show only changes and deletions in diffs, not additions #1120
Comments
Just thinking outloud here: One option would be a modification to this line in |
Unfotinatly this is not an option because Unfortunately it is not option as |
@Mingun The Of course, first class support could be added in Chai by updating the @boneskull what are your thoughts on this? |
Yes, it can be option, many thanks! Maybe it should be added the last optional |
@meeber My thoughts are that I don't want to add more random properties that Mocha does stuff with. Mocha's handling of diffs and whatnot can be avoided by an There's really no end to Mocha adding features to handle properties on @Mingun You should be able to do what you want within the context of an assertion library, but I can't tell you whether or not Chai allows this level of customization; you would need to remove the |
@boneskull how would you feel about chai's |
@keithamus Ehh, I don't see that as necessarily any better than the current state of things. Even if Chai did that, Mocha would still need to retain its own diffing code, because other libs don't. |
either way, assertion libraries can use the built-in mocha capabilities, or just go their own way, which I think is fine. |
If I understand what has been said so far, currently there's two options: 1) an Assertion library can delegate both the diffing of the The way I interpret @keithamus' last question is asking if it may be possible to support a third option where the assertion library performs the diffing of the Did I get all of that right or am I misunderstanding how it works currently or what's being proposed? |
I'm probably misunderstanding something... |
Well I've been looking into chai supporting a |
@boneskull Not necessarily :D. It could be me misunderstanding; I don't have a firm mental picture on the diffing stuff yet. @keithamus Was my last post accurate? If not, can you correct my misunderstanding? |
@meeber your explanation was correct, yes. |
Of course, it is possible, but how in that case to guarantee uniformity of diff? I like the idea with @keithamus with
Actually there only two use-cases: full equality and similarity so it is enough to support only these two cases. diff with full equality is already supported, there was a case with similarity. Originally I stated a sentence for a solution with the minimum expenses, but the decision with |
If mocha could agree to a standard of the following I would be happy to begin work on the changes for chai:
In other words a string For --- <optional filename> <optional ISO date>
+++ <optional filename> <optional ISO date>
@@ -1,3 +1,3 @@ <optional block header>
-removed text
+added text
unchanged text (The How mocha interprets that diff is up to their implementation (I would imagine terminals could colorise the ouput with ansi escape codes, while html reporter might use CSS). |
With a unified diff, we will lose context; it's for text files. But this would solve the use case by adding some flag to Chai which suppresses the additions from the diff and then Mocha has to add a special hook for it? (It's "special" because Chai supporting such a property is not a standard 😉) If the idea is for Mocha to simply print the contents of the |
In other words, this is what I said originally; either an assertion lib can use's Mocha stuff, or it can do whatever other thing it wants to and just use |
Just before I respond to your points, I'll make an additional point. We get a regular stream of issues about mocha's diffing, and how it relates to chais error messages, and how they can become better representations and more consistent for each failure. We need to work on our error messages to make them better, so it makes sense for us to also own the diff output.
As I allude to over in qunitjs/js-reporters#106; the assertion library (chai) can provide much nuanced context about the
To give a concrete example, currently Buffers look like Arrays with mocha's diff. I'm hoping with work Im doing around Chai's inspection engine we can produce diffs which get much closer to what the user cares about, so they can see what's failing much easier. Sure, mocha could pick this up, but chai already has to have this logic for the
Which standard is this? We mostly adhere to CommonJS Unit Testing 1.0 but this doesn't cover anything about reporter errors, so properties like
That would not be what I want. I would want mocha to parse the contents of a diff and represent it in a way which is useful for the chosen reporter. Taking the diff and colorising it would be a useful step.
We definitely could do this, but ultimately it's not that usable. The reason an explicit Ultimately if we (chai) can't get |
@keithamus I agree with @boneskull that |
@Mingun I don't understand how JSON patch could work. What would be the JSON patch for @@ -1,1 +1,1 @@
- 1
+ '1' A more complex example, what would be the JSON patch for |
I think I was replying to you here, so I probably just misunderstood:
I'll revisit "interop" below... If it seems like I'm being stubborn, I just want to be clear: I'm trying to avoid adding and maintaining support for "random" properties coming out of 3p libraries. As originally proposed, a flag to suppress portions of a diff, requested by a single individual (sorry @Mingun; don't take it personally), isn't something I'm willing to entertain. ...just so you know where I'm coming from.
So, a couple things:
Granted, it's kind of lame to just attach the diff to INTEROP Interop is tricky, because we're not just talking about Chai and Mocha, we're talking about Chai and n other assertion libraries and m other testing frameworks. The chances we'll get strong cooperation--especially in a timely manner--are near-nil (e.g. the non-adoption of the js-reporters spec, but it could still have some life in it). That being said, we could certainly ask around, but I would prefer action over consensus. As the respectively most popular testing framework and assertion lib, we have a chance to lead "by example" or otherwise bilaterally throw our weight around. I don't claim to know what other things Chai's users want out of diffs--besides what @Mingun has proposed. I do know some requests Mocha would have of these libraries, however! Please let me know what you want to do here. You can just use |
FWIW here is an (incomplete) list of issues we have relating to diffs and Mocha: #246, #249, #270, #304, #363, #469, #654, #678, #703, #1055, #1060, #1103 You can see these issues have been surfaced with a fairly regular cadence since 2014 which is somewhat shortly after when mocha started supporting We can see the same thing in Mocha's issue tracker: mochajs/mocha#1832, mochajs/mocha#1348, mochajs/mocha#1071 ... I won't go on, you know enough about mocha's issues. All of these issues are roughly duplicates of eachother - the general theme is that the error output isn't sufficient.
FWIW @Mingun is looking for the same answer all the above issues are. I'm derailing this issue with my own agenda, which is something I've been perculating on for a long time. I've been hoping to add
That is exactly what I'm after. Given mocha+chai is the most used combo, like you say, we could easily throw a bit of weight around and end up with everyone happy. IMO this is where the ProblemBoth mocha and chai need to improve diff output, to stop the influx off issues we both receive. How do we do this in an interoperable way? Requirements (chai)
What are Mocha's requirements for this interop here? |
@keithamus { op: "replace", path: [], oldValue: 1, newValue: "1" }
{ op: "replace", path: [], oldValue: <ref to Buffer.from([1,2,3])>, newValue: <ref to new Set(['a', 'b', 'c'])> } It is not necessary to consider JSON Patch as a direct guide to action, however the resultant format can be its superset. For example, it is in addition possible to support a possibility of representation of diff of strings/Buffer/Set/etc. collections. This format is not obliged to be serialized in JSON, it may contain references to any objects. Studying the list of diff libraries, I managed to select 2 approaches in representation of diff:
// patch, that if applied to expected produce actual
diff(expected, actual) ->
// changed all object
{
// added key "type" with value "grammar"
type: { op: 'add', value: 'grammar' },
// changed key "rules"
rules: {
// changed key 0
0: {
// added key "type" with value "rule"
type: { op: 'add', value: 'rule' },
// value of the key "name" changed from "rule" to "rule1"
name: { op: 'replace', oldValue: 'rule', newValue: 'rule1', diff: [...string diff...] },
},
// changed key 1
1: {
// added key "type" with value "rule"
type: { op: 'add', value: 'rule' },
},
},
} Maybe second approach will be better. Each object represent a changeset. If |
I'm not completely averse to implementing JSON Diff is thats how we want to go, but it looks more complex to generate and consume than a unified diff. |
brain dump follows Where Do I File A Bug?What sucks for users, I've noticed, is that they don't know which lib is responsible for the diff. Is it Chai? Is it Mocha? Sometimes we don't even know! I'm open to suggestions on how to attack that. Baseline Value RepresentationWhat also sucks--and could suck more, if we don't do this right--is inconsistent diffs from one lib to the next. How to represent a numeric primitive is not especially controversial. But ...
Mocha has its own ideas about many of these, but not necessarily the "best" or "most correct" ones. Some of these may be best left out of the "core"--and/or we can choose to delay decisions until they become necessary.
Extensibility
Metadata, StatisticsOne thing Mocha can't do, because of a lack of a built-in assertion library, is set an expectation about how many assertions were expected to be made. Mocha doesn't know what an "assertion" is. Some may find this capability more useful than others, but it is another tool to avoid false positives. But I don't know if the user should touch the assertion lib API or Mocha's API to use it. (This is also not currently possible from within a standalone assertion framework, because it doesn't know when a "test" or "suite" finishes. Or what a "test" or "suite" is.) Open to ideas. First thing that comes to mind is some sort of If there are numbers that Chai keeps that may be useful to Mocha, let's expose those. Backwards CompatMocha must continue to work fine with other assertion libraries. That said, Mocha's diff output will necessarily change, since I don't want to maintain two separate algorithms for this. Modules
JustificationIf we're going to disrupt users by screwing with their diffs, the new diffs should be better than the old ones. They should be so freaking awesome that users of other frameworks & libs create issues demanding adoption. 😉 |
I ask all interested to review mochajs/mocha#3251 |
This discussion was hugely productive and it's made us think a lot about what we can implement in chai 5! We're probably going to support diffs in chai 5, we've added this to our project board over on https://github.com/chaijs/chai/projects/2. I'll close this issue because I think it's taken its logical conclusion, and nothing is specifically actionable here. |
I use
chai
assertion library andchai-like
library for the partial comparing of objects: I am interested in existence and compliance only of those properties that are listed in anexpected
object. Existence of additional properties in anactual
object permissibly. Unfortunately, when diff is created it is not considered in any way and those properties, existence which for the test it is indifferent get to diff. It would be desirable to have a way to suppress their generation in a diff. Example (runned with mocha):This is current actual output of mocha which is too large in some cases and contains unnecessary details
But I want that diff looked here so:
Now there is no opportunity to show diff in that look in what it is required as
AssertionError
has no signs that it is required. Mocha developers advised to create at first the task here before they are able to support this functionality (mochajs/mocha#3193).I suppose add to an
AssertionError
object new property which could control such behavior of diff.If the idea is supported, I can make PRs for its implementation in all necessary libraries.
The text was updated successfully, but these errors were encountered: