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.

Tests with setTimeout fail after update to v7.29.6

See original GitHub issue
  • @testing-library/dom version: 7.29.6
  • Testing Framework and version: jest 26.6.3
  • DOM Environment:

Relevant code or config:

describe(() => {
    it('should show loading state while search is running', async () => {
        MockInstance(Service, () => ({
            search: (request) => NEVER,
        }))
        const fixture = MockRender(SearchComponent)
        const el = fixture.point.nativeElement
        fixture.point.componentInstance.filters$.next(requestFilterMock)
        await sleep(100) // <-- uses setTimeout
        fixture.detectChanges()

        // initial search hint not shown
        expect(queryByText(el, matchers.initialSearch)).toBeFalsy()

        // loading text
        expect(getByText(el, matchers.loading))

        // show 15 skeleton clusters while loading
        expect(ngMocks.findAll(WagonHireClusterComponent)).toHaveLength(15)
        expect(getAllByLabelText(el, /loading/).length).toBeGreaterThan(0)
    })
})

What you did:

Nothing, test was running fine before updating from 7.29.4 to 7.29.6. Now it fails with the error message below.

What happened:

: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
    at new Spec (xyz\node_modules\jest-jasmine2\build\jasmine\Spec.js:116:22)
    at new Spec (xyz\node_modules\jest-jasmine2\build\setup_jest_globals.js:78:9)
    at new JBPatchedSpec (C:\Program Files\JetBrains\WebStorm\plugins\JavaScriptLanguage\helpers\jest-intellij\lib\jest-intellij-jasmine-reporter.js:94:7)
    at specFactory (xyz\node_modules\jest-jasmine2\build\jasmine\Env.js:523:24)
    at Env.it (xyz\node_modules\jest-jasmine2\build\jasmine\Env.js:592:24)
    at Env.it (xyz\node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:134:23)
    at it (xyz\node_modules\jest-jasmine2\build\jasmine\jasmineLight.js:100:21)
    at context.<computed> (xyz\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4288:39)
    at Suite.<anonymous> (xyz\src\app\wagon-hire\wagon-hire-search\wagon-hire-search.component.spec.ts:87:5)
    at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (xyz\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:407:30)
    at Zone.Object.<anonymous>.Zone.run (xyz\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:167:47)
    at Suite.<anonymous> (xyz\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4227:33)
    at addSpecsToSuite (xyz\node_modules\jest-jasmine2\build\jasmine\Env.js:444:51)
    at Env.describe (xyz\node_modules\jest-jasmine2\build\jasmine\Env.js:414:11)
    at describe (xyz\node_modules\jest-jasmine2\build\jasmine\jasmineLight.js:88:18)
    at context.<computed> (xyz\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4270:39)
    at Object.<anonymous> (xyz\src\app\wagon-hire\wagon-hire-search\wagon-hire-search.component.spec.ts:26:1)
    at Runtime._execModule (xyz\node_modules\jest-runtime\build\index.js:1299:24)
    at Runtime._loadModule (xyz\node_modules\jest-runtime\build\index.js:898:12)
    at Runtime.requireModule (xyz\node_modules\jest-runtime\build\index.js:746:10)
    at jasmine2 (xyz\node_modules\jest-jasmine2\build\index.js:230:13)
    at runTestInternal (xyz\node_modules\jest-runner\build\runTest.js:380:22)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at runTest (xyz\node_modules\jest-runner\build\runTest.js:472:34)

Problem description:

Now one of the following changes the outcome:

  • Calling jest.useRealTimers() fixes the test, but it doesn’t make sense to call it in every test or test suite that uses testing-library.
  • Removing the call await sleep(100) which uses setTimeout moves the test further, but the asserts fail since the needed nodes aren’t available yet. Using waitFor didn’t help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
timdeschryvercommented, Mar 17, 2021

It seems like some tests of ATL are also failing with the latest version of DTL. At first sight, only the tests that use waitFor or waitForElementToBeRemoved.

I’ll take a look at it in a couple of days.

@ma7moudat I just noticed that you’re using the Angular Testing Library. The latest version of ATL has DTL v7.29.4 pinned as a dependency as a quick fx.

2reactions
ma7moudatcommented, Feb 22, 2021

@eps1lon I’ll try to provide one by tomorrow

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing a function which uses setTimeout with Jest: why is this ...
This function works perfectly well in my app, so I'm fairly certain the implementation is fine. However, the following test fails: jest.
Read more >
dom-testing-library - Bountysource
After upgrading to jest v27 I get an error: ○ Test suite failed to run TypeError: Cannot destructure property 'DOMElement' of ...
Read more >
Testing setTimeout/setInterval - DEV Community ‍ ‍
Recently at a client, a question came up about unit testing functionality that used setTimeout and setInterval.
Read more >
Mocking setTimeout with Jest. | Medium - Marek Rozmus
We will get a following error: thrown: “Exceeded timeout of 5000 ms for a test. We could increase timeout for unit tests but...
Read more >
ECMAScript® 2023 Language Specification - TC39
6.2.7 The Environment Record Specification Type; 6.2.8 The Abstract Closure ... Other updates included requiring that Array.prototype.sort be a stable sort, ...
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