-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Add function expect.addPrettyFormatPlugin(plugin); #7702
Comments
@DanielSWolf you can implement class Animal {
constructor(name) {
this.implementationDetail1 = 42;
this.implementationDetail2 = 'foobar';
this.nameBuffer = Buffer.from(name, 'utf8');
}
get name() {
return this.nameBuffer.toString();
}
toJSON() {
return `[Animal ${this.name}]`;
}
}
function greet(animal) {
return `Hello ${animal.name}!`;
}
describe('greet', () => {
it.each([
['Hello Pooh!', new Animal('Pooh')],
['Hello Simba!', new Animal('Simba')],
])('should return %s for input %p', (expected, animal) => {
expect(greet(animal)).toBe(expected);
});
}); |
@mattphillips @pedrottimark thoughts on this one? |
@SimenB Thanks for the suggestion. That's what I've been doing so far. However, I see two downsides to this approach:
|
Application-specific serializers for test names is a new thought to me. I have wondered whether they should apply to reports when other matchers like |
@SimenB Note that one can only add toJSON to one’s own classes. It is sometimes useful to have custom pretty-printing for third-party objects, which cannot be modified. For instance, I have tests in which some of the objects contain AbortController & AbortSignal instances. Jest tries to pretty print their internal properties, which doesn’t actually work (it reaches some internal node object and crashes). Even if it worked, I don’t actually care about the internals, I just need to know if the signal is aborted or not. |
Yeah, that's a good point. We should definitely not crash though, that's separate from a way of getting some custom serialization. Could you open up a new bug report? |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
How does one remove the stale label, though? |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
Add a function
expect.addPrettyFormatPlugin(plugin)
. This should work similar toexpect.addSnapshotSerializer(serializer)
, only not for snapshots, but for test names when usingit.each()
.Motivation
Consider the following unit test:
The test is successful, but the output is not helpful:
Whenever Jest prints an animal,
pretty-format
includes a lot of implementation details, but no useful information (in this case, because the relevant information is in a buffer). It would be great if the developer had a way of telling Jest how to pretty-format certain types.Example
In the above example, we could implement a simple plugin that knows how to properly format an animal:
With this code in place, the output from Jest would be much more helpful:
Pitch
This feature doesn't introduce specific new rules for pretty-formatting values. Instead, it allows developers to add their own formatting rules. They get readable test output that contains all the information they need and no textual clutter.
The text was updated successfully, but these errors were encountered: