how to automatically call discardPeriodicTasks after tests in Angular with jest?

I do a one time subscription that has a debounceTime pipe on my component onInit:

this.subscriptions.add( this.updateJobs$ .pipe( filter(jobs => !!jobs.length), debounceTime(MyComponent.ListStabilizationTimeInMS), first(), ) .subscribe((jobs: Update[]) => { // some code }) ); 

Because of this I need to include discardPeriodicTasks(); as last line after each fakeAsync tests, which I find tedious. Otherwise I get the error Error: 1 periodic timer(s) still in the queue. for all the tests.

I tried to create an afterEach:

afterEach(()=> { discardPeriodicTasks(); }); afterEach(fakeAsync(()=> { discardPeriodicTasks(); })); 

But it does not work and the only way to pass the tests is to manually append discardPeriodicTasks(); to all the test cases. Would be great if it could be automated.

8

Related questions 47 Jest clean up after all tests have run 17 Check if test failed in afterEach of Jest 3 Jest with special tests run on occasion Related questions 47 Jest clean up after all tests have run 17 Check if test failed in afterEach of Jest 3 Jest with special tests run on occasion 2 Jest - do something before every test 1 How to clear Jest mock implementation for next tests? 2 How do I clear jest state (spies, mocks) between each test? 0 Is it possible to clear the mock of a module in JEST? 4 How do you reset the test subject in between tests in jest with typescript? 6 Jest - How do I reset object state for each test? 3 How to prevent Jest from printing all skipped tests? Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like