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

Update serializer types to support enzyme-to-json #109

Conversation

alistairjcbrown
Copy link
Contributor

Current type declaration for serializer causes errors when used with
enzyme-to-json. Changes:

  • diffOptions is now an optional parameter (as it is in code).
  • Type declaration for the second serialize parameter of the print
    function has been weakened to any due to incorrect type used in
    enzyme-to-json (should be function, but has been typed serializer)
  • Return type declaration from print function has been weakened to
    any; this function is not limited to only returning strings.

Conflicting enzyme-to-json declaration:
https://github.com/adriantoine/enzyme-to-json/blob/d1f86ee82ce014bb3d63f521a5d553307948eff0/index.d.ts#L22-L25

Mismatch of enzyme-to-json serializer type in print function:
https://github.com/adriantoine/enzyme-to-json/blob/d1f86ee82ce014bb3d63f521a5d553307948eff0/src/createSerializer.js#L9-L11

Fixes #108

/cc: @simonsmith @thymikee

Current type declaration for serializer causes errors when used with
`enzyme-to-json`. Changes:
 * `diffOptions` is now an optional parameter (as it is in code).
 * Type declaration for the second `serialize` parameter of the `print`
   function has been weakened to `any` due to incorrect type used in
   `enzyme-to-json` (should be function, but has been typed serializer)
 * Return type declaration from `print` function has been weakened to
   `any`; this function is not limited to only returning strings.

Conflicting `enzyme-to-json` declaration:
https://github.com/adriantoine/enzyme-to-json/blob/d1f86ee82ce014bb3d63f521a5d553307948eff0/index.d.ts#L22-L25

Mismatch of `enzyme-to-json` `serializer` type in `print` function:
https://github.com/adriantoine/enzyme-to-json/blob/d1f86ee82ce014bb3d63f521a5d553307948eff0/src/createSerializer.js#L9-L11

Fixes jest-community#108
@@ -25,8 +25,8 @@ declare namespace jest {

interface Serializer {
test: (value: any) => boolean;
print: (value: any, _serializer?: Function) => string;
diffOptions: (valueA: any, valueB: any) => DiffOptions;
print: (value: any, _serializer?: any) => any;
Copy link
Member

Choose a reason for hiding this comment

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

I think we can leave the string return here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thymikee Unfortunately not; the enzyme-to-json type declaration for JestSerializer has print returning their Json type which is an object.

https://github.com/adriantoine/enzyme-to-json/blob/d1f86ee82ce014bb3d63f521a5d553307948eff0/index.d.ts#L24

Keeping it as string give this error:

Type 'JestSerializer' is not assignable to type 'Serializer'.
  Types of property 'print' are incompatible.
    Type '(CommonWrapper: any, serializer: JestSerializer) => Json' is not assignable to type '(value: any, _serializer?: any) => string'.
      Type 'Json' is not assignable to type 'string'.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, wasn't aware. That's fine then

@thymikee thymikee merged commit 4f6b1ed into jest-community:master Jan 11, 2020
@alistairjcbrown alistairjcbrown deleted the bugfix/correct-serializer-types branch January 11, 2020 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript error when passing enzymeToJson to setSerializers
2 participants