question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress is failed when calling new user-defined function

See original GitHub issue

Current behavior:

I created a new file that defined all functions which will be used for my module. When I created new spec and called it, Cypress showed the error like XXX is not a function. 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 properly.

Desired behavior:

New function should be called and working properly.

Steps to reproduce:

  1. Create new file, e.g. plan.js and put it under support folder
  2. Declare the function, e.g. Createnewplan as below
Cypress.Commands.add('createNewPlan', (planType, planName) => {
       // Put your source code here
})
  1. Go to integration folder then create a spec file, e.g. plan_spec.js
  2. In the spec file, put the function as the below code
describe('Plan List page', function() {
	it('Create new plan', function() {
	
		const user = this.users[0]
		const planList = this.planLists[0]
		
		cy.login(user.username, user.password)
	
		// Create new plan
		cy.createNewPlan(planList.planType, planList.planName)
				
	})
  1. At the runtime, Cypress shows the error TypeError: cy.createNewPlan is not a function image

Versions

Cypress version: 3.1.1 OS: Win 10 Browser: Chrome 70

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
chrisbreidingcommented, Nov 13, 2018

Files in cypress/support are not all automatically loaded. Only cypress/support/index.js is loaded. If you look at it, you’ll see its contents include something like:

// ...

import './commands'

// ...

If you create cypress/support/plan.js, you need to import it in cypress/support/index.js:

// ...

import './commands'
import './plan'

// ...
9reactions
tormodhaucommented, Mar 13, 2020

As mentioned above you get this error if you convert your support/index.js to Typescript without telling Cypress to look for a TS support file.

Add this to your cypress.json file:

"supportFile": "./cypress/support/index.ts"

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress custom command is not recognized when invoked
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...
Read more >
Error Messages | Cypress Documentation
This message means that Cypress was unable to find tests in the specified file. You'll likely get this message if you have an...
Read more >
Custom Commands - Cypress Documentation
Cypress comes with its own API for creating custom commands and overwriting existing commands. The built in Cypress commands use the very same...
Read more >
Test Retries - Cypress Documentation
If the test fails a third time, Cypress will mark the test as failed and then move on to run any remaining tests....
Read more >
Best Practices - Cypress Documentation
The Real World App (RWA) uses two useful custom commands for selecting elements ... If the content of the element changed would you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found