Skip to content

Tests with setTimeout fail after update to v7.29.6 #901

Closed
@ma7moudat

Description

@ma7moudat
  • @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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions