1

In an Angular app, I want to access App State inside the Cypress test.

I have followed the suggestions in: How to use Angular Store in Cypress e2e tests

Nothing worked so far. My current setup:

export interface IWindowCypress {
  store: Store;
  Cypress: unknown;
}
export class AppComponent {
  constructor(protected store: Store) {
    const windowWithStore: IWindowCypress = window as unknown as IWindowCypress;
    console.log('AppComponent');
    if (windowWithStore.Cypress) {
      console.log('save store');
      windowWithStore.store = store;
    }
  }
}

In Cypress Side,

describe('My First Test', () => {
  before(() => {
    cy.window().its('store').should('have.length', 6)
  })

I don't have type errors with this setup. But in Cypress test, cy.window.store doesn't exist. I have tried passing an array instead of a store, and even that doesn't exist on Cypress's side. Any ideas on how to fix this problem?

usalin
  • 141
  • 1
  • 7
  • So this question works [How to use Angular Store in Cypress e2e tests](https://stackoverflow.com/questions/68985238/how-to-use-angular-store-in-cypress-e2e-tests), you just made a typo. The [original answer](https://stackoverflow.com/a/60996163/19575305) has 10 votes, so is working for the majority. – Vagdevi Dec 10 '22 at 03:16
  • Original answer could be working yes, but latest typescript (4.9.4) is complaining about the window.Cypress part . First link actually is a working example now. – usalin Dec 10 '22 at 08:45

1 Answers1

4

You missed a step, the reference should be to the component property via this

if (windowWithStore.Cypress) {
  windowWithStore.store = this.store;
}

It looks like the parameter version you are using is destroyed after component initialization.

  • You are right. When I was setting up this code, I accidentally removed the .this part. App component was getting destroyed until I made a little change on the Cypress part, now I seem to have access to the variable. I still have to figure out how to work with this, but defintely looks better. – usalin Dec 09 '22 at 21:40