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

Commands and types do not import properly #162

Open
brandonlenz opened this issue Jun 16, 2022 · 7 comments
Open

Commands and types do not import properly #162

brandonlenz opened this issue Jun 16, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@brandonlenz
Copy link

brandonlenz commented Jun 16, 2022

What does not work?
pa11y command types are not properly recognized if the library is configured correctly (according do the documentation) with the following error: TS2339: Property 'pa11y' does not exist on type 'cy & CyEventEmitter'.. Satisfying the typescript compiler deviates from setup instructions, and naturally prevents the tests from running.

How to reproduce?
Create a new cypress typescript project. Follow the documentation for using cypress with custom commands and setting up @cypress-aduit/pa11y. In commands.ts, set up a custom commands such as

import '@cypress-audit/pa11y/'

Cypress.Commands.add('check_a11y', () => {
  cy.pa11y({
    runners: ['htmlcs'],
    standard: 'WCAG2AA',
  })
})

Make sure the supportFile also contains the following:

// Import commands.js using ES2015 syntax:
import './commands'

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Custom command to use pa11y with specific configurations
       * @example cy.check_a11y()
       */
      check_a11y(): Chainable<Element>
    }
  }
}

Expected behavior
The exported types should be properly identified when importing the commands as the instructions suggest, and the tests should be able to run as properly configured.

Screenshots / Animated Gifs
Importing /pa11y/commands:
image
TS2339: Property 'pa11y' does not exist on type 'cy & CyEventEmitter'.

Importing /pa11y:
image
however,
image

@brandonlenz brandonlenz added the bug Something isn't working label Jun 16, 2022
@brandonlenz
Copy link
Author

Here's a hacky bandaid/workaround for now:

  1. Create a custom.d.ts file in the root of your cypress project (cypress is a standalone app in our monorepo that lives next to client and server code):
project/
  e2e/
    cypress/
      ..
    custom.d.ts
    package.json
  client/
  server/
  1. Copy and paste the contents of @cypress-audit/pa11y to custom.d.ts:
declare namespace Cypress {
  type AccessibilityStandard = 'Section508' | 'WCAG2A' | 'WCAG2AA' | 'WCAG2AAA'

  interface Options {
    actions?: string[]
    headers?: object
    hideElements?: string
    ignore?: string[]
    ignoreUrl?: boolean
    includeNotices?: boolean
    includeWarnings?: boolean
    level?: string
    method?: string
    postData?: string
    reporter?: string
    rootElement?: string
    runners?: string[]
    rules?: string[]
    screenCapture?: string
    standard?: AccessibilityStandard
    threshold?: number
    timeout?: number
    userAgent?: string
    wait?: number
  }

  interface Chainable<Subject> {
    /**
     * Runs a pa11y audit
     * @example
     * cy.pa11y(opts)
     */
    pa11y(opts?: Options)
  }
}

Type errors gone:
image

@dante01yoon
Copy link

dante01yoon commented Jun 17, 2022

I'm using only cypress-audit package,

same type problems with cy.lighthouse in spec files.
I'm looking work around either. Should I define type explicitly in type in support/commands.ts ?

@devhid
Copy link

devhid commented Jun 17, 2022

I think I had the same issue. Adding cypress-audit in the types option in tsconfig.json fixed this for me:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "sourceMap": false,
    "outDir": "../../dist/out-tsc",
    "allowJs": true,
    "types": ["cypress", "node", "cypress-audit"], <--- Added `cypress-audit` here
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src/**/*.ts", "src/**/*.js"],
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

@mfrachet
Copy link
Owner

I will take care of this one when coming back from vacation. Seems like something is not going super well with Cypress 10 🤔

@matthamil
Copy link

matthamil commented Jul 20, 2022

You should also be able add this at the top of that custom.d.ts and it should work, insofar as you are referencing custom.d.ts inside your tsconfig.json.

/// <reference types="@cypress-audit/lighthouse" />
/// <reference types="@cypress-audit/pa11y" />

@mfrachet
Copy link
Owner

Are you still facing this issue?

@ashrafnazar
Copy link

ashrafnazar commented Jun 4, 2024

I still get this issue, but the workaround provided by @brandonlenz does the trick.

Does indeed feel hacky though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants