Skip to content

Specify ontimeout at configuration level #1179

@SimonGodefroid

Description

@SimonGodefroid

Is there a way to specify an onTimeout function at top level?

We are having some flaky tests and I'd like to be able to print the screen when some of the timeouts occur because we figured in some cases the printed DOM during timeouts is really not what we're expecting.

Context:

some tests fails every once in a while because we render the app and redirect to a specific route and sometimes 2 tests that run in a row, the second one is not starting at the redirect location at the beginning of the test but at the location where the previous test stopped (when you click on the submit button of a form, mock with msw a patch request and that the code redirects to the details page). For that case we have set the DEBUG_PRINT_LIMIT in the CI command to be able to see the mounted DOM and figured that weird case.

Motivation:

We have new flaky tests and this time the error message in the CI is:

Error: expect(element).toBeInTheDocument()

element could not be found in the document
    at Object.<anonymous> (/home/circleci/project/src/***.test.tsx:39:53)
    at _callCircusTest (/home/circleci/project/node_modules/jest-circus/build/run.js:212:5)
    at _runTest (/home/circleci/project/node_modules/jest-circus/build/run.js:149:3)
    at _runTestsForDescribeBlock (/home/circleci/project/node_modules/jest-circus/build/run.js:63:9)
    at _runTestsForDescribeBlock (/home/circleci/project/node_modules/jest-circus/build/run.js:57:9)
    at run (/home/circleci/project/node_modules/jest-circus/build/run.js:25:3)
    at runAndTransformResultsToJestFormat (/home/circleci/project/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
    at jestAdapter (/home/circleci/project/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:109:19)
    at runTestInternal (/home/circleci/project/node_modules/jest-runner/build/runTest.js:380:16)
    at runTest (/home/circleci/project/node_modules/jest-runner/build/runTest.js:472:34)

Locally I wrote a purposefully broken statement to see whether I could leverage on onTimeout function to print the DOM as it was when the code failed.

expect(
      await screen.findByText(
        /LOLLLLLL/i,
        {},
        {
          onTimeout: err => {
            console.log('>>>'.repeat(200),err);
            screen.debug(null, 10000000000);
          }
        }
      )
    ).toBeInTheDocument();

I would like to know how I can specify an onTimeout function or whether I can use getElementError to achieve this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions