cypress-image-snapshot icon indicating copy to clipboard operation
cypress-image-snapshot copied to clipboard

Convert to TypeScript and add public types

Open jackjocross opened this issue 5 years ago • 11 comments

There have been a few issues related to TypeScript types (#13, #72), updating the source code to TypeScript would make future changes easier and give consumers better type support.

jackjocross avatar Mar 14 '19 17:03 jackjocross

So admittedly I am a TS amateur. I was just playing around with things and I did get it to config a bit easier if I added index.d.ts directly to the node_modules/cypress-image-snapshot/ with

import 'cypress';

declare global {
  namespace Cypress {
    interface Chainable {
      matchImageSnapshot: (options?: any) => void;
    }
  }
}

Obviously hacking a node_modules folder is a no no but I thought that might be a way to go though im not sure how much different it is from having a custom types folder somewhere else. I have seen a few other plugins use this sort of pattern and thought I would give it a shot.

jordandlaman avatar Mar 14 '19 21:03 jordandlaman

Yep that makes sense! So if we add an index.d.ts to the root of cypress-image-snapshot and publish to NPM again, everyone will receive types without having to touch their node_modules directly.

I think what would be even better is if we rewrite to use TypeScript and then generate index.d.ts from our source, similar to how Formik does.

But creating types manually is definitely useful if we want to start there.

jackjocross avatar Mar 18 '19 13:03 jackjocross

Im pretty new to typescript but would like to have a go at this issue if it is ok?

makeupsomething avatar May 22 '19 14:05 makeupsomething

@makeupsomething for sure give it a shot! I'm happy to review any PRs.

jackjocross avatar May 23 '19 14:05 jackjocross

import "cypress";
import "jest.d.ts";

import * as JestImageSnapshot from "jest-image-snapshot";

declare global {
    namespace Cypress {
        interface Chainable {
            matchImageSnapshot: (
                options?:
                    | string
                    | (Partial<
                          JestImageSnapshot.MatchImageSnapshotOptions &
                              Loggable &
                              Timeoutable &
                              ScreenshotOptions
                      >),
            ) => void;
        }
    }
}

Just based on what I understood from the readme, just a good stop gap for now.

lemcii avatar Jul 09 '19 08:07 lemcii

So admittedly I am a TS amateur. I was just playing around with things and I did get it to config a bit easier if I added index.d.ts directly to the node_modules/cypress-image-snapshot/ with

import 'cypress';

declare global {
  namespace Cypress {
    interface Chainable {
      matchImageSnapshot: (options?: any) => void;
    }
  }
}

Obviously hacking a node_modules folder is a no no but I thought that might be a way to go though im not sure how much different it is from having a custom types folder somewhere else. I have seen a few other plugins use this sort of pattern and thought I would give it a shot.

Adding index.d.ts as you said still gives me error in the .ts spec file- Property 'matchImageSnapshot' does not exist on type 'Chainable<undefined>'.

anujpunia avatar Aug 02 '19 04:08 anujpunia

Any solution? How to use this plugin with typescript?

Lukaszli avatar Dec 16 '19 11:12 Lukaszli

I shifted to Percy. They have a free version as well and much smoother. Had no luck here.

edupeeth avatar Dec 17 '19 07:12 edupeeth

I went ahead and added typings for folks to use: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/41222

Now, all you need to do is yarn add @types/cypress-image-snapshot -D for this plugin to work with TypeScript!

Keysox avatar Dec 27 '19 18:12 Keysox

@Keysox Think the options are missing Cypress.ScreenshotOptions

Svish avatar Jan 27 '20 11:01 Svish

@Svish -- options have been added if you upgrade to 3.1.1 https://github.com/DefinitelyTyped/DefinitelyTyped/pull/41897/files#diff-603108fb86a139bc0934ce518b4267eeR10

Keysox avatar Jan 28 '20 18:01 Keysox