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

Can't get the plugin working with Typescript #72

Open
draunitschke opened this issue Mar 8, 2019 · 5 comments
Open

Can't get the plugin working with Typescript #72

draunitschke opened this issue Mar 8, 2019 · 5 comments

Comments

@draunitschke
Copy link

Hi,
I already worked through the suggestions in #13 however I still can't get the plugin working.

We are using TypeScript with WebPack.
I also installed @types/jest-image-snapshot

I've added a file index.d.ts in my <"root dir">/types/cypress-image-snapshot folder:

index.d.ts:

import Cypress from 'cypress';
import { MatchImageSnapshotOptions } from 'jest-image-snapshot';

declare global {
  namespace Cypress {
    interface MatchImageSnapshotOptions extends MatchImageSnapshotOptions, Partial<ScreenshotOptions> {}

    interface Chainable {
      matchImageSnapshot(options?: MatchImageSnapshotOptions): void;
      matchImageSnapshot(fileName: string, options?: MatchImageSnapshotOptions): void;
    }
  }
}

Then pointing to the file in tsconfig.json:

tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "types": [
      "cypress"
    ]
  },
  "include": [
    // spec files
    "cypress/*/*.ts",
    "tests/**/*.ts"
  ],
  "files": [
    "types/cypress-image-snapshot/index.d.ts"
  ]
}

In <"rootDir">/cypress/plugins/index.js I added the required lines for this plugin:

const wp = require('@cypress/webpack-preprocessor');
const {
  addMatchImageSnapshotPlugin,
} = require('cypress-image-snapshot/plugin');


module.exports = (on, config) => {
    const options = {
        webpackOptions: require('../../webpack.config'),
    };
    on('file:preprocessor', wp(options));
    addMatchImageSnapshotPlugin(on, config);

    return getConfigurationByEnvironment(config)
};

So far so good, but when I add in /cypress/support/commands.ts (not commands.js) :


 import { addMatchImageSnapshotCommand } from '../../node_modules/cypress-image-snapshot/command';

addMatchImageSnapshotCommand();

I get an error saying that no declaration for the module "../../node_modules/cypress-image-snapshot/command" could be found and I should try to install @types/cypress-image-snapshot (which does not exist) or that I should add a declaration file (.d.ts), which contains "declare module 'cypress-image-snapshot';"

And in my test specs I can add a cy.matchImageSnapshot command and I do not get a syntax-error in the spec, but when I run cypress I get an error:

TypeError: cy.matchImageSnapshot is not a function

@jordandlaman
Copy link

So in my case I am using a dist file for the transpiled javascript and pointing my cypress.json to the specific folders for plugins and support. That way I cheated and did this

// tslint:disable-next-line:no-var-requires
const command = require('cypress-image-snapshot/command')
command.addMatchImageSnapshotCommand({
  customDiffConfig: {threshold: 5.0}, // threshold for each pixel
  failureThreshold: 5.0, // threshold for entire image
  failureThresholdType: 'percent', // percent of image or number of pixels
});

And it doesn't pop an error during transpile.

@kremerd
Copy link

kremerd commented Oct 17, 2019

I had the same problems and found a solution.

I get an error saying that no declaration for the module "../../node_modules/cypress-image-snapshot/command" could be found and I should try to install @types/cypress-image-snapshot (which does not exist) or that I should add a declaration file (.d.ts), which contains "declare module 'cypress-image-snapshot';"

You need to add a custom module definition file for that.

And in my test specs I can add a cy.matchImageSnapshot command and I do not get a syntax-error in the spec, but when I run cypress I get an error:

Currently there's an issue with the typescript preprocessor that causes this behavior: nrwl/nx#1609. As a workaround you can declare a type for matchImageSnapshot in support/commands.ts and you have to import that in your test case.

The following setup worked for me:

integration/signin.ts:

import '../support';

describe('signin page', () => {
  beforeEach(() => {
    cy.visit('/signin');
  });

  it('should match the design', () => {
    cy.matchImageSnapshot();
  });
});

support/index.ts:

import './commands';

support/commands.ts:

import { addMatchImageSnapshotCommand, SnapshotOptions } from 'cypress-image-snapshot/command';

addMatchImageSnapshotCommand();

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Screenshot test the current page.
       * @param name file name of the screenshot
       * @param options general options
       */
      matchImageSnapshot(name: string, options?: SnapshotOptions): Chainable<Element>;

      /**
       * Screenshot test the current page.
       * @param options general options
       */
      matchImageSnapshot(options?: SnapshotOptions): Chainable<Element>;
    }
  }
}

support/modules.d.ts:

declare module 'cypress-image-snapshot/command' {
  type SnapshotOptions = Partial<
    Cypress.Loggable &
    Cypress.Timeoutable &
    Cypress.ScreenshotOptions &
    import('jest-image-snapshot').MatchImageSnapshotOptions
  >;

  export const addMatchImageSnapshotCommand: (options?: SnapshotOptions) => void;
}

@Keysox
Copy link

Keysox commented Dec 27, 2019

I went ahead and added typings for folks to use: DefinitelyTyped/DefinitelyTyped#41222

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

@prmichaelsen
Copy link

should issue be closed?

also, adding types.ts instead of index.d.ts worked for me, but decided to use Keysox's types instead

@ivoiv
Copy link

ivoiv commented Feb 3, 2021

I went ahead and added typings for folks to use: DefinitelyTyped/DefinitelyTyped#41222

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

@Keysox
This man is doing god's work! 👏 👏 👏

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

No branches or pull requests

6 participants