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

Custom Command is not a function #25348

Closed
yurakhomitsky opened this issue Jan 3, 2023 · 11 comments · Fixed by #25400
Closed

Custom Command is not a function #25348

yurakhomitsky opened this issue Jan 3, 2023 · 11 comments · Fixed by #25400

Comments

@yurakhomitsky
Copy link

yurakhomitsky commented Jan 3, 2023

Current behavior

I was following along with the installation instructions from cypress/schematic for "ng new" project

Then I decided to add a custom command however cypress throws the error: "cy.login is not a function"

  • To add a custom command I wrote the following code in the commands.ts file
    Cypress.Commands.add("login", () => { cy.log('login...') })

  • Added it to the namespace
    declare namespace Cypress { interface Chainable<Subject = any> { login(): any; } }

  • Uncommented this line import './commands'; in the e2e.ts file

  • Used cy.login() in the spec.cy.ts file

image

Desired behavior

Cypress should not throw the error "cy.login is not a function"

Test code to reproduce

https://github.com/yurakhomitsky/cypress-angular-schematic-issue

Cypress Version

12.2.0

Node version

16.15.0

Operating System

macOS 13.0.1

Debug Logs

No response

Other

No response

@astone123
Copy link
Contributor

@yurakhomitsky thanks for the example project. I see in your Cypress configuration that you have supportFile: false for e2e

  e2e: {
    baseUrl: 'http://localhost:4200',
    supportFile: false
  }

Setting this option to false means that cypress/support/e2e.ts will not be executed when you launch e2e testing. Because of this, commands.ts is never imported, and your custom commands are never registered.

If you remove supportFile: false, you should be able to call your custom commands successfully.

@astone123
Copy link
Contributor

astone123 commented Jan 6, 2023

I'm going to route this to the tools team to see if we should remove supportFile: false from the Cypress configuration that we generate with cypress-schematic to avoid confusion.

@Amit-P
Copy link

Amit-P commented Jun 27, 2023

I don't have "supportFile: false" in my e2e but still my custom commands are not working.

@mike-plummer
Copy link
Contributor

Hi @Amit-P , sorry you're having trouble. This issue has been closed for some time. If you're experiencing an issue that you believe is a Cypress bug I would recommend you open a new issue and provide all the requested information including a reproducible example (ideally a git repository we can clone and run ourselves) and Debug Logs.

Since this sort of issue is usually a configuration or setup issue (rather than a bug in Cypress) you may want to reach out on Discord for help.

@vecernik
Copy link

having supportFile correctly set, custom commands don't work in 12.17.4 anymore.

@nagash77
Copy link
Contributor

@vecernik please open a new issue with a reproducible example and the Cypress team will be happy to investigate.

@yuritoledo
Copy link

yuritoledo commented Aug 18, 2023

@vecernik dang! Same here. Did you fix it?

@vecernik
Copy link

@yuritoledo Only downgrading back to 12.17.3 helped to get custom commands working.

@Gbuomprisco
Copy link

The solution is described here: #27641

@vecernik
Copy link

"sideEffects": [ "libs/shared/src/e2e/*.ts" ]
in package.json fixed the issue. Btw it's just a plain import (not extra function), it works now even with v13.

@ptakpatryk
Copy link

Workaround above works in my case I had "sideEffects" set to false in the package.json. Removing the whole line helped as well.

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.

9 participants