Cypress custom command is not recognized when invoked

cypress custom command is not a function
cypress custom commands
cypress is not recognized as an internal or external command
no commands were issued in this test cypress
cypress is not defined
not a function error in cypress
cypress chainable command
cypress documentation

I've created the following custom command in my cypress/support/commands.js file.

Cypress.Commands.add("login", (username, password) => {
    cy.request({
        method: 'POST',
        form: true,
        url: '/test/login/',
        body: {'username': username, 'password': password}
    })
})

I had tests passing and login working before moving the login functionality to this custom command. I'm invoking it in my spec with cy.login(testuser, testpwd), but I'm getting the following error message: TypeError: cy.login is not a function. The docs say that /cypress/support/commands.js is loaded before any test files are evaluated, so I assumed that simply placing a custom command in there would make the command available. I'm running the tests through the local (GUI) test runner.

All the code and referenced modules in index.js are loaded before your test file. So you need to refer(require) commands.js in your index.js file. You can however import commands.js module directly in your test file but then you need to include it every test file. Recommended approach is to include it in index.js file and you are not worried about explicitly refer in your test files.

Cypress custom commands not recognized, loginWith() custom command is recognized and invoked correctly. However I know this is not a good thing to do. This is my custom command in  Cypress detected that you invoked one or more cy commands in a custom command but returned a different value. Because cy commands are asynchronous and are queued to be run later, it doesn’t make sense to return anything else.

To expand on @Dinesh Kumar's excellent answer, it's also important you haven't disabled support for the default support file (I know, unfortunate naming scheme in this case) inside your cypress.json by adding the line: supportFile: false.

Delete that line from your cypress.json if it's there. You can also specify a different path if you're not happy using the default path of cypress/support/index.js.

Working index.js with commands.js file - both in the support folder:

// index.js
const customCommands = require('./commands.js')

module.exports = {
  commands: customCommands
}

And double check your settings:

// cypress.json
{
  "baseUrl": "http://demo.your-domain.test",
  "supportFile": false,  <-- delete this if present
  ...
}

Cypress custom commands not recognized - cypress - html, support/commands to the top of my new login spec file, the cy.loginWith() custom command is recognized and invoked correctly. However I know this is not a  .invoke() can time out waiting for a promise you've returned to resolve. Command Log. Invoke jQuery show method on element. cy. get ('.connectors-div'). should ('be.hidden'). invoke ('show'). should ('be.visible') The commands above will display in the Command Log as: When clicking on invoke within the command log, the console outputs the

may help to put a line import './commands.js/'; into index.js

Cypress custom command is not recognized when invoked - html, I've created the following custom command in my cypress/support/commands.js file. Cypress.Commands.add("login", (username, password) => { cy.request({  When you’re issuing many internal Cypress commands, consider passing { log: false } to those commands, and programmatically controlling your custom command. This will cleanup the Command Log and be much more visually appealing and understandable.

Cypress is failed when calling new user-defined function · Issue , However, if I put that function into the command and it worked as normal. I also tried to delete the cache, reinstall Cypress but it is not working  The “is not recognized as an internal command” error usually occurs because the computer can’t find the executable that you’re asking it to launch. However, you can provide it with the full path to your executable file and it should then be able to run it without any issues. Launch a Command Prompt window on your PC.

Custom Commands, Cypress comes with its own API for creating custom commands and overwriting existing commands. Commands.add() and not supported for use in Cypress. Invoking it like this would error: If you're working on a search_spec.js file and want to compose several repeatable actions together, you should  You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously. // this won't work the way you think it does const button = cy.get('button') const form = cy.get('form') button.click()

Error Messages, You'll likely get this message if you have an empty test file and have not y. Cypress detected that you invoked one or more cy commands but returned a different If you are setting Cypress defaults or utilizing custom Cypress commands, Please review the natively recognized environment variables for your CI provider. It is very important to understand that Cypress commands don’t do anything at the moment they are invoked, but rather enqueue themselves to be run later. This is what we mean when we say Cypress commands are asynchronous. Take this short test, for example:

Comments
  • Can you confirm that the index.js file within cypress/support has this line of code? import "./commands"
  • Yes, I've now un-commented that line in index.js.
  • Thank you. In this section of the docs it says: "A great place to define or overwrite commands is in your cypress/support/commands.js file, since it is loaded before any test files are evaluated." From that it sounded like no import of commands.js is required, so it never occurred to me to try that. Similarly, for index.js they say that it is imported prior to running any spec "so you don’t have to import this file in every single one of your spec files" - but I had to import index.js and, within it, import commands.js.
  • I also think it's not particularly clear from the docs... Will expand on this answer with a sample.