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.

cy.visit() Typescript promise chain error - Type 'Chainable<HTMLInputElement>' is missing the following properties

See original GitHub issue

Current behavior:

The following Typescript code produces an error:

const loadInput = (): Promise<HTMLInputElement> => cy.visit('./core/selectors/selectors-test-file.html')
	.then((contentWindow: Window) => {
		let { document } = contentWindow
		const $input = <HTMLInputElement>document.getElementById('textInputID')
		return $input
	})

it("Shouldn't break", async ()=> {
	const $input = await loadInput()
	expect($input.value).to.equal('')
})

The error message I get:

TS2739: Type 'Chainable<HTMLInputElement>' is missing the following properties from type 'Promise<HTMLInputElement>': catch, [Symbol.toStringTag]

Desired behavior:

The loadInput() function should return a promise that later returns the HTML input DOM element.

Versions

  • Cypress: 3.8.0
  • Node: 12.13.0
  • Browser: Chrome 79.0.3945.130
  • OS: Windows 10

Work around

I worked around the issue by doing this:

const loadInput = (): Promise<HTMLInputElement> => new Promise((resolve) => {
	cy.visit('./core/selectors/selectors-test-file.html')
		.then((contentWindow: Window) => {
			let { document } = contentWindow
			const $input = <HTMLInputElement>document.getElementById('textInputID')
			resolve($input)
		})
})

But now Cypress is complaining about nesting cy.visit() inside a promise. It’s just a warning though so I can ignore it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
Dan503commented, Apr 1, 2020

I like the promise async/await syntax a lot more though.

I switched to using cypress-promise to make my code cleaner. Based on the number of downloads, many other developers feel the same way.

It has the annoying issue of triggering the warning though.

https://github.com/NicholasBoll/cypress-promise/issues/6

Is there a way that the warning can be suppressed if we choose that we would prefer to work with promises?

1reaction
FFdhorkincommented, May 12, 2020

I like the promise async/await syntax a lot more though.

I switched to using cypress-promise to make my code cleaner. Based on the number of downloads, many other developers feel the same way.

It has the annoying issue of triggering the warning though.

NicholasBoll/cypress-promise#6

Is there a way that the warning can be suppressed if we choose that we would prefer to work with promises?

Just to chime in here… We’re using Cypress to validate our API - no UI involved, except for Cypress itself (to inspect requests). Our code often times has >= 5 levels of indentation because we have to keep doing nested then blocks to pull out variables we want to use. And a lot of our helper methods are really messy - I’d like to have a getCount() method that just returns an integer rather than Chainable.

cypress-promise would enable that, and make our code MUCH more readable. But it would also create dozens of warnings in our console, and that’s a non-starter. It would be nice to be have some way to suppress that warning. I’m tempted to make a fork of Cypress just to remove that warning… but that seems like a pretty extreme measure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[]' is missing the following properties from type 'Promise<Cats[]>'
You're returning an array, but your function is async , meaning it should return a Promise of the array. There's two ways you...
Read more >
TypeScript Deep Dive
following example TypeScript will know that foo is of type number below and will give an error on the second line as shown:...
Read more >
Typeerror is not a function typescript - Seba Online
This error happens when JavaScript can't find the setState() function from the ... chaining is a process of querying and calling properties, subscripts, ......
Read more >
TypeError: invalid assignment to const "x" - JavaScript | MDN
JavaScript const declarations can't be re-assigned or redeclared. ... 'x' (Firefox) TypeError: Attempted to assign to readonly property.
Read more >
cypress-io/cypress - Gitter
Hi! can anybody help with some typescript stuff? was trying to switch some spec file from js to ts, and faced this kind...
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