Skip to content
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

fix: add toJSON to ValidationErrors #693

Merged
merged 4 commits into from
Jun 16, 2023
Merged

Conversation

brudil
Copy link
Collaborator

@brudil brudil commented Jun 15, 2023

Following on from #692.

  • ValidationErrors holds a property of entities which have circular references.
  • Jest returns the failure error of a test back to its root process by stringifying
  • Jest also clones the returned object before stringifying, destroying the prototype where a toJSON method would live

This (from my testing, Jest 29.5.0 w/ Circus) should stop issues around circular references by introducing toJSON as a property of ValidationErrors which survives the clone.

I've added in a test to attempt to represent this issue, wasn't sure the best place for it - happy to move/change.

@brudil brudil requested a review from stephenh June 15, 2023 21:25
Copy link
Collaborator

@stephenh stephenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks!

constructor(errors: ValidationError[]);
constructor(message: string);
constructor(messageOrErrors: ValidationError[] | string) {
super(typeof messageOrErrors === "string" ? messageOrErrors : errorMessage(messageOrErrors));
this.errors = typeof messageOrErrors === "string" ? [] : messageOrErrors;
// Jest clones without prototype, so explictly setting this as a property rather than a class method
// https://github.com/jestjs/jest/issues/11958
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wonder if this is what we missed, i.e. I probably tried defining toJSON as class method. Great comment!

constructor(errors: ValidationError[]);
constructor(message: string);
constructor(messageOrErrors: ValidationError[] | string) {
super(typeof messageOrErrors === "string" ? messageOrErrors : errorMessage(messageOrErrors));
this.errors = typeof messageOrErrors === "string" ? [] : messageOrErrors;
// Jest clones without prototype, so explictly setting this as a property rather than a class method
// https://github.com/jestjs/jest/issues/11958
this.toJSON = () => `<ValidationErrors> ${this.message}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind making this ValidationErrors: ${this.message}? I just realized/remembered that EntityManager.toJSONs also uses this <...> syntax but not sure why we did that...

Copy link
Collaborator Author

@brudil brudil Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! I was also debating returning { message } which would be the default response if we weren't overriding this (sans .entities obviously)

@brudil brudil merged commit b3c49bd into main Jun 16, 2023
3 checks passed
@brudil brudil deleted the brudil/validationerrors-tojson branch June 16, 2023 21:49
stephenh pushed a commit that referenced this pull request Jun 16, 2023
## [1.89.2](v1.89.1...v1.89.2) (2023-06-16)

### Bug Fixes

* add toJSON to ValidationErrors ([#693](#693)) ([b3c49bd](b3c49bd))
@stephenh
Copy link
Collaborator

🎉 This PR is included in version 1.89.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants