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

TypeScript error when passing enzymeToJson to setSerializers #108

Closed
simonsmith opened this issue Jan 10, 2020 · 4 comments · Fixed by #109
Closed

TypeScript error when passing enzymeToJson to setSerializers #108

simonsmith opened this issue Jan 10, 2020 · 4 comments · Fixed by #109

Comments

@simonsmith
Copy link

simonsmith commented Jan 10, 2020

Hello, here is my setup code:

test-setup.ts

import * as snapshotDiff from 'snapshot-diff';

// This is the same function exposed in `enzyme-to-json/serializer' as per README
import { createSerializer } from 'enzyme-to-json'; 

const enzymeToJsonSerializer = createSerializer();
snapshotDiff.setSerializers([enzymeToJsonSerializer]);

This produces the following error:

error TS2741: Property 'diffOptions' is missing in type 'JestSerializer' but required in type 'Serializer'.

This seems like a mismatch between the Serializer type in this repository and the JestSerializer type from enzyme-to-json.

My understanding is that a serializer should only have two required properties (print and test) so perhaps diffOptions needs to be optional? Note that it appears the source code expects it to be optional already

If I try to make that change I get:

 error TS2322: Type 'JestSerializer' is not assignable to type 'Serializer'.
  Types of property 'print' are incompatible.
    Type '(CommonWrapper: CommonWrapper<{}, {}, Component<{}, {}, any>>, serializer: JestSerializer) => Json' is not assignable to type '(value: any, _serializer?: Function | undefined) => string'.
      Types of parameters 'serializer' and '_serializer' are incompatible.
        Type 'Function | undefined' is not assignable to type 'JestSerializer'.
          Type 'undefined' is not assignable to type 'JestSerializer'.

Let me know if I can provide further debugging help, thanks

@thymikee
Copy link
Member

cc @alistairjcbrown

@alistairjcbrown
Copy link
Contributor

alistairjcbrown commented Jan 11, 2020

@simonsmith Thanks for the great debugging notes!
diffOptions is non-standard and so is optional in the code; it should be optional in the type declaration too 👍 I'll get a PR up with this updated

alistairjcbrown added a commit to alistairjcbrown/snapshot-diff that referenced this issue Jan 11, 2020
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
@alistairjcbrown
Copy link
Contributor

@simonsmith PR is up - could you take a look and see if that solves the issue?
Typescript isn't something I've used a lot, but spinning up a "create react app" with the typescript template, the changes in the PR to appease the errors.

thymikee pushed a commit that referenced this issue Jan 11, 2020
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
@simonsmith
Copy link
Author

@alistairjcbrown Working fine now. Thank you for the rapid PR! 👍

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 a pull request may close this issue.

3 participants