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.

(core/testing) flush() not working when requestAnimationFrame contexts

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

The requestAnimationFrame is one of the same macrotasks as setTimeout, but it is not drained by the flush() function.

20211018211946

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/node-qvczbt?file=src/app/app.component.spec.ts

Please provide the exception or error you saw

Chrome 94.0.4606.81 (Linux x86_64) AppComponent should increased number after drawn FAILED

Please provide the environment you discovered this bug in

❯ ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 12.2.10
Node: 14.16.0
Package Manager: npm 7.17.0
OS: linux x64

Angular: 12.2.10
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.10
@angular-devkit/build-angular   12.2.10
@angular-devkit/core            12.2.10
@angular-devkit/schematics      12.2.10
@schematics/angular             12.2.10
rxjs                            6.6.7
typescript                      4.3.5
    

~/projects/node-qvczbt

from stackblitz WebContainer.

Anything else?

Work tick(16), but flush() should also work.

related

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JiaLiPassioncommented, Feb 15, 2022

I think this is a bug, since the current logic is not consistent and not good for performance either. tick(16) will trigger both macroTask and requestAnimationFrame, but flush() is not, also flush() will not trigger period macroTask such as setInterval which is also not a correct behavior and inconsistent with tick(). I will create a PR to fix this one.

1reaction
petebacondarwincommented, Oct 19, 2021

So it is the case that when you request an animation frame from within fakeAsync then a timer is scheduled to resolve in 16ms time.

https://github.com/angular/angular/blob/9f40d2a0ea229dcf091f5d68d9d114c60bd9bcbd/packages/zone.js/lib/zone-spec/fake-async-test.ts#L610-L617

Further since rAF are treated diifferently to setTimeout they cannot be triggered simply by a call to flush() as discussed above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't context.clearRect() work inside ... - Stack Overflow
I've looked up the beginPath() method but I'm still not 100% sure why clearing the page wouldn't work in this case. Could you...
Read more >
Component testing scenarios - Angular
For a use case in which TestBed.inject() does not work, see the Override component providers section that explains when and why you must...
Read more >
Should React use requestAnimationFrame by default? #11171
Not currently. We do batching between different setState()s within one event handler (everything is flushed when you exit it). For many cases ...
Read more >
Window.requestAnimationFrame() - Web APIs | MDN
requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function ...
Read more >
requestAnimationFrame should execute before the next frame
This was the cause of https://github.com/mapbox/mapbox-gl-js/issues/5390, which necessitated an elaborate workaround.
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