-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(testlab): add a new helper toJSON
#1733
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
Conversation
packages/testlab/src/misc.ts
Outdated
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| /** | ||
| * JSON transport does not preserve properties that are undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should say JSON format or JSON encoding instead of transport.
packages/testlab/src/misc.ts
Outdated
| * Use this function to convert a model instance into a data object | ||
| * as returned by REST API | ||
| */ | ||
| export function deserializedFromJson<T extends object>(value: T): T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name is a bit misleading. It tricks me to think about the argument is a JSON string.
What about asJSON() or toJSON()?
Do we want to restrain the argument to be an object? What about any?
We need to deal with the case where value is undefined.
JSON.parse(JSON.stringify(undefined)); // SyntaxError: Unexpected token u in JSON at position 0The return type is incorrect, for example:
// Customer is a class with `getFullName()` method.
const customer: Customer = await this.repo.findById('c001');
const json = deserializedFromJson(customer); // TypeScript infers the return type as `Customer`
json.getFullName(); // The function does not exist but TypeScript cannot catch it.I propose that we change the method to be:
export function toJSON<T extends object>(value: T): DeepPartial<T> { // ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to using either asJSON or toJSON ... with a slight preference for toJSON.
// Customer is a class with `getFullName()` method.
const customer: Customer = await this.repo.findById('c001');
const json = deserializedFromJson(customer); // TypeScript infers the return type as `Customer`
json.getFullName(); // The function does not exist but TypeScript cannot catch it.
I don't think json.getFullName(); is a valid use of this as the intention of this method is to act as a helper to allow comparison of an object to the value you would get from a rest api ... which is the use case for testing (and the inspiration for this helper method). One should just do customer.getFullName() instead of converting to JSON and then trying to treat it as a class.
Considering the above maybe a better name we can use is modelToJSON?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@virkt25 Please note by the current signature, deserializedFromJson(customer); returns a Customer, which has getFullName(). As a result, TypeScript thinks json.getFullName() is valid use but the getFullName does not exist on the object.
virkt25
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation LGTM. Just need to agree on a name as per the comments.
5bcce0c to
3d92cfa
Compare
I think this isn't going to really fix the problem, because I solved the problem by returning a generic I have also renamed the helper function to PTAL again, LGTY now? |
toJSON
|
@bajtos Please address my latest comments. |
No need to use |
jannyHou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Implement a helper function to convert an object (e.g. a Model instance) to a data object representing JSON version of the input object. Specifically, properties set to a value that cannot be represented in JSON (e.g. `undefined`) are removed from the result.
3d92cfa to
e7a9ef4
Compare
|
@raymondfeng added more tests as you requested and simplified the actual implementation by wrapping the input value in an object property. LGTY now? |
raymondfeng
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
Implement a helper function to convert an object (e.g. a Model instance) to a data object representing JSON version of the input object.
Specifically, properties set to a value that cannot be represented in
JSON (e.g.
undefined) are removed from the result.Checklist
npm testpasses on your machine/docs/sitetestlab's README was updatedpackages/cliwere updatedexamples/*were updated