jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Timeout has no effect if test is successful

Open mrenit opened this issue 2 years ago • 4 comments

Version

27.5.1

Steps to reproduce

Steps:

  1. Clone https://github.com/mrenit/timeout-bug
  2. Run npm test

Expected behavior

 FAIL  src/App.test.tsx
   renders learn react link timeout raises (37 ms)
   renders learn react link timeout not raises (7 ms)

  ● renders learn react link timeout raises

    thrown: "Exceeded timeout of 1 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      3 | import App from './App';
      4 |
    > 5 | test('renders learn react link timeout raises', async () => {
        | ^
      6 |   render(<App />);
      7 |   const linkElement = await screen.findByText(/learn react7/i);
      8 |   expect(linkElement).toBeInTheDocument();

      at Object.<anonymous> (src/App.test.tsx:5:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)

  ● renders learn react link timeout not raises

    thrown: "Exceeded timeout of 1 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      10 |
      11 |
    > 12 | test('renders learn react link timeout not raises', async () => {
         | ^
      13 |   render(<App />);
      14 |   const linkElement = await screen.findByText(/learn react/i);
      15 |   expect(linkElement).toBeInTheDocument();

      at Object.<anonymous> (src/App.test.tsx:12:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)

Test Suites: 1 failed, 1 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        0.856 s, estimated 1 s
Ran all test suites.

Watch Usage: Press w to show more.

Actual behavior

FAIL  src/App.test.tsx
   renders learn react link timeout raises (31 ms)
   renders learn react link timeout not raises (8 ms)

  ● renders learn react link timeout raises

    thrown: "Exceeded timeout of 1 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      3 | import App from './App';
      4 |
    > 5 | test('renders learn react link timeout raises', async () => {
        | ^
      6 |   render(<App />);
      7 |   const linkElement = await screen.findByText(/learn react7/i);
      8 |   expect(linkElement).toBeInTheDocument();

      at Object.<anonymous> (src/App.test.tsx:5:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        0.804 s, estimated 1 s
Ran all test suites.

Watch Usage: Press w to show more.

Additional context

I created this project with npx create-react-app my-app --template typescript. Afterwards I only modified the test App.test.tsx to:

import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link timeout raises', async () => {
  render(<App />);
  const linkElement = await screen.findByText(/learn react7/i);
  expect(linkElement).toBeInTheDocument();
}, 1); // <-- Timout of 1ms


test('renders learn react link timeout not raises', async () => {
  render(<App />);
  const linkElement = await screen.findByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
}, 1); // <-- Timout of 1ms

The first test behaves like expected. The second test does not behave as expected, since the test with an execution time of 8 ms passes instead of failing cause of the timeout of 1ms.

Environment

System:
    OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (16) x64 AMD Ryzen 7 PRO 4750U with Radeon Graphics
  Binaries:
    Node: 16.18.1 - /usr/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.3.0 - /usr/local/bin/npm

mrenit avatar Nov 10 '22 16:11 mrenit

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 10 '22 16:12 github-actions[bot]

It is still an issue

mrenit avatar Dec 12 '22 09:12 mrenit

I don’t think this is a bug. The first test fails, because you expect to find a string matching /learn react7/. The method .screen.findByText, is async, so it keeps trying to find this string on the dom, till it reaches the timeout.

Your second test though, searches for a different string /learn react/. And the test succeeds before the timeout. I’m not sure, but I think Jest adds the time of setup and teardown to the test. So this 8 ms you see in the passing test is setup + your test + teardown.

Also, as an argument as to why I think this is not a bug, you can try the following code:

const delay = ms =>  new Promise((resolve) => setTimeout(resolve,ms));

 test("fails on timeout", async () => {
    await delay(10);
  }, 9); // This should fail

vitorfrs-dev avatar Dec 26 '22 20:12 vitorfrs-dev

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 25 '23 20:01 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Feb 24 '23 20:02 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Feb 24 '23 20:02 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Mar 27 '23 00:03 github-actions[bot]