🖌️
Product Process Documentation
  • Product Process Documentation
  • Definition of Done (DoD)
    • General checkpoints
      • Specific checkpoints by team
    • Important process: QA review & PO review
      • QA Review
      • PO Review
  • Work Items
    • Product Backlog Item (PBI)
    • Bug
      • Basic rules for creating a bug
      • How to report a Bug
    • Bugs Management
  • Code Standards
  • Different Test Levels
    • Unit Test
      • Frontend Unit Testing
        • What is a Unit Test?
        • How do I know if I am developing a good unit test?
        • AAA (Arrange, Act and Assert)
        • Overloaded test suits
        • Setup & Teardown
          • JEST Mocks
          • FakeTimers
        • Istanbul Annotations
        • C8 Annotations
        • JEST Runner (Debug unit tests with Jest)
    • Component Test
      • Frontend Component Testing
        • What is Component Testing?
        • Best practices
        • Bad practices
        • Setup
          • Sandbox
          • Mocks, Services and Providers
          • Test scenario
    • Integration Test
      • Frontend Integration Testing
        • What is a Integration Test?
        • AAA (Arrange, Act and Assert)
        • Best Practices
        • Bad practices
        • Setup & Teardown
        • How to create a scenario
          • Create the migrated app
          • Add to project
        • How to debug
        • Common problems
      • Testing Driven Development Guide and recommendations
    • Functional Test
    • Security Testing
      • Security Testing Tools
      • Frontend Security Testing
    • Performance testing
    • Best Practices
    • Test Documentation
  • Run test projects
    • General steps
    • Specific steps by team
  • DevOps
    • Pipelines
    • Builds
    • Specific information by team
    • Test plan
    • Service Hooks for Azure DevOps Notifications
      • Slack Notifications
      • Microsoft Teams Notifications
  • Dashboards
    • General
    • QA Dashboards
  • Release Process
    • General Steps
    • Specific steps by team
  • Migration Cells
    • Basics of testing process
  • Release process
  • References
Powered by GitBook
On this page

Was this helpful?

  1. Different Test Levels
  2. Unit Test
  3. Frontend Unit Testing
  4. Setup & Teardown

FakeTimers

In some cases, when your code uses timers (setTimeout, setInterval, clearTimeout, clearInterval), your tests can become unpredictable, slow and unreliable.

To solve these problems, or if you need to rely on specific timestamps in your code, most testing frameworks offer the option to replace the real timers in your tests with dummy timers. This should be used sporadically and not on a regular basis, as there is some overhead in using them.

When dummy timers are used in tests, all code within the test uses dummy timers.

The common pattern for setting fake timers is usually inside the beforeEach, but if this is not necessary for all tests, you could initialize the faketimers in the first line of code of the Arrange section and remember to reset the timers at the end of the unit test. Here is an example:

    it('GridExTableViewComponent should call the rowDoubleClickEvent on doubleClickHandler', () => {
    //Arrange
    jest.useFakeTimers();
    const spyRowDoubleClickEvent = jest.spyOn(component, 'rowDoubleClickEvent');
    component.currentDataRow = { 'gridEXRowID': '123456789' };
    const event = {};

    //Act
    component.doubleClickHandler(event);
    jest.runOnlyPendingTimers();

    //Assert
    expect(spyRowDoubleClickEvent).toHaveBeenCalled();
    
    jest.useRealTimers();
  });
PreviousJEST MocksNextIstanbul Annotations

Last updated 2 years ago

Was this helpful?

There are several types of methods to simulate setTimeOut processes: , , ... In case of doubts about how to perform any process you can consult the official JEST documentation, .

runAllTimers
runOnlyPendingTimers
clearAllTimers
Timer Mocks