Skip to content

Tests logging after teardown do not fail tests in non-watch mode #11132

@NMinhNguyen

Description

@NMinhNguyen

🐛 Bug Report

#10728 sets the exit code to 1 when tests log after teardown. This correctly terminates the process if run in non-watch mode. However, if you do jest --watch then all tests are reported as passing, despite there being a warning:

Cannot log after tests are done. Did you forget to wait for something async in your test?

Additionally, I noticed that console.warn and console.error don't cause failures - is that intended? I'm also happy to open a dedicated issue if you prefer keeping things isolated.

To Reproduce

Steps to reproduce the behavior:

  1. Given this test:

    // sum.test.js
    
    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
    
    test('should fail in watch mode', () => {
      async function foo() {
        await delay(100);
    
        console.log('this should fail tests in watch mode too');
      }
    
      foo();
      expect(true).toBe(true);
    });
  2. Run yarn jest --watchAll (or --watch if running inside a git repo)

  3. Observe that Jest reports a passing test, although it does log a warning:

    Screenshot 2021-02-28 at 23 05 55
  4. Now if you just run yarn jest, you'll see the process exits with exit code 1.

Expected behavior

Jest should report a test failure.

Link to repl or repo (highly encouraged)

https://repl.it/@NMinhNguyen/jest-playground#sum.test.js

Note: you'll need to run it in watch mode: yarn jest --watchAll (--watchAll since it's not inside a git repo).

envinfo

npx envinfo --preset jest
npx: installed 1 in 3.502s

  System:
    OS: Linux 5.4 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Xeon(R) CPU @ 2.30GHz
  Binaries:
    Node: 12.18.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.6 - /usr/local/bin/npm
  npmPackages:
    jest: 27.0.0-next.3 => 27.0.0-next.3 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions