Trouble/Weirdness with accessing aliased values in this
context
#27293
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey everyone, I've been running into an issue with Cypress Component Testing that I can't seem to figure out.
What do I mean?
I'm trying to set up some aliases in a
beforeEach
hook usingcy.wrap().as()
, and then access these aliased values in my tests using thethis
context. However, when I logthis
in my tests, the aliased values areundefined
.Here's a simplified version of my code where I'm facing this issue:
In this example, both
this.component
andthis.someObject
are logged asundefined
, and trying to access a property onthis.component
throws an error becausethis.component
isundefined
.However, I have another example where the aliased values are logged as
undefined
, but I can still access properties on them in my tests:In this second example,
this.component
andthis.someObject
are logged asundefined
, just like in the first example. However, I can still accessthis.component.foo
in my test, and it correctly logs'bar'
.What I tried so far
I've tried a number of things to resolve this issue, but nothing seems to work. Here's a summary of what I've tried:
cy.then()
after setting up the aliases: I thought that maybe the aliases weren't fully resolved when I was trying to access them, so I tried usingcy.then()
after setting up the aliases. This allowed me to log the correct values inside thethen
function, but the aliases were stillundefined
in the actual tests.Using a
before
hook instead of abeforeEach
hook: I also tried using abefore
hook instead of abeforeEach
hook, thinking that there might be an issue with how thethis
context was being handled between tests. However, this didn't change the behavior either - the aliases were stillundefined
in the tests.Using
cy.get('@alias').then()
to access the aliased values: I tried usingcy.get('@alias').then()
to access the aliased values, which did allow me to access the correct values in the tests. However, this approach is not ideal for us. It makes the tests less readable and more difficult to maintain. If we can't get thethis
context working consistently across all test suites, we're considering using a Jasmine-like style where we use a variable scoped in thedescribe
block that is written and read, even though this is not idiomatic Cypress.Despite these attempts, I'm still facing the same issue - the aliases are
undefined
when I log them and when I try to access them in my tests, except in some cases where I can still access properties on the aliased values in my tests, even though they are logged asundefined
.Beta Was this translation helpful? Give feedback.
All reactions