Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom interval breaks change detection for cypress #28938

Closed
muhamedkarajic opened this issue Feb 14, 2024 · 8 comments
Closed

Custom interval breaks change detection for cypress #28938

muhamedkarajic opened this issue Feb 14, 2024 · 8 comments
Labels
CT Issue related to component testing stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period

Comments

@muhamedkarajic
Copy link

muhamedkarajic commented Feb 14, 2024

Current behavior

When I have a component and that component uses a custom interval which in the below example is called Tick. Once that tick is used in any service cypress is forever awaiting the changes. I assume it forever awaits the function stack to be empty.

Desired behavior

I wonder if cypress could maybe somehow detect such things and give a warrning rather then just not render my component which injects the service which then uses the tick. Ofc I can overcome the issue my making this a factory (service) which will provide me a special Tick when its a testing enviroment.

Test code to reproduce

Tick:

export class Tick {
    onTick$ = new ReplaySubject<void>(1);

    constructor(
        private isSkippingTick: () => boolean, 
        public delayInMilliseconds = 1_000, 
        private onDestroy$: Subject<void> | null = null,
        private onTick: (() => unknown) | null = null
    ) {
        this.tryToTick();
        if(onTick)
            this.onTick$.subscribe(() => onTick())
    }

    private tryToTick() {
        asyncScheduler.schedule(() => {

            if (this.isSkippingTick()) {
                this.scheduleTick(); // --> to fix remove the infinite function stack
                return;
            }

            this.onTick$.next();
            this.scheduleTick(); // --> to fix remove the infinite function stack
        })
    }

    scheduleTick() {
        let tick = timer(this.delayInMilliseconds).pipe(
            observeOn(asyncScheduler));

        if (this.onDestroy$) {
            tick = tick.pipe(
                takeUntil(this.onDestroy$)
            )
        }
        tick.subscribe(() => this.tryToTick());
    }
}

Service:

@Injectable({providedIn: 'root'})
export class ExampleService {
    tick = new Tick(() => false, 16, null, () => { /* Any function which you wish to execute after some delay. */});
}

Component:

@Component({
  standalone: true,
  template: `<div id="example-div">I'm not visible. {{ (condition$ | async) ? "Neither I am." : 'I will anyways never be visible.' }}</div>`,
  imports: [CommonModule],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ExampleComponent {
  exampleService = inject(ExampleService); // -> comment this out then it will work.
  condition$ = new BehaviorSubject<boolean>(true);
}

Cypress code:

describe('Example', () => {
    it('div is properly rendering', () => {
        cy.viewport(900, 500).mount(ExampleComponent).then(_env => {
            cy
                .get('#example-div')
                .should('be.visible')
                .then(() => {
                    console.log('Finished...');
                });
        })
    })
});

Cypress Version

13.6.4

Node version

v18.16.0

Operating System

macOS 12.6.6

Debug Logs

Chrome version: 121.0.6167.160

Other

Please note its not good enough to simply use isSkippingTick cause it still will schedule to reexecute tryToTick. It really has to be so that there is none macro task scheduled in the que.

@jennifer-shehane
Copy link
Member

@muhamedkarajic Could you give an example of Cypress running against this component?

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Feb 14, 2024
@muhamedkarajic
Copy link
Author

muhamedkarajic commented Feb 14, 2024

@muhamedkarajic Could you give an example of Cypress running against this component?

I'll try to do one tomorrow. Its production code and the component is quit the component. However it should be possible to reproduce this. Ty for the quick response.

@muhamedkarajic
Copy link
Author

muhamedkarajic commented Feb 15, 2024

@jennifer-shehane I have added for you a minimal demo in the initial comment.

@rainerhahnekamp
Copy link
Contributor

I can confirm that I've experienced the same behavior.

@jennifer-shehane jennifer-shehane added stage: needs investigating Someone from Cypress needs to look at this and removed stage: needs information Not enough info to reproduce the issue labels Feb 26, 2024
@muhamedkarajic
Copy link
Author

muhamedkarajic commented Apr 22, 2024

@jennifer-shehane this is only in the component testing environment. Any updates? In case of any questions I will gladly answer them.

@jennifer-shehane jennifer-shehane added the CT Issue related to component testing label Apr 23, 2024
@muhamedkarajic
Copy link
Author

Any news on this, I just heared there is a new feature called clock, maybe that can be used somehow to get away from this issue @jennifer-shehane?

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Dec 31, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

4 participants