Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Jest fail when a test logs after completing #10715

Closed
NickHeiner opened this issue Oct 26, 2020 · 3 comments · Fixed by #10728
Closed

Make Jest fail when a test logs after completing #10715

NickHeiner opened this issue Oct 26, 2020 · 3 comments · Fixed by #10728

Comments

@NickHeiner
Copy link

In jest-runner/runTest.js:

function freezeConsole(testConsole, config) {
  // @ts-expect-error: `_log` is `private` - we should figure out some proper API here
  testConsole._log = function fakeConsolePush(_type, message) {
    const error = new (_jestUtil().ErrorWithStack)(
      `${_chalk().default.red(
        `${_chalk().default.bold(
          'Cannot log after tests are done.'
        )} Did you forget to wait for something async in your test?`
      )}\nAttempted to log "${message}".`,
      fakeConsolePush
    );
    const formattedError = (0, _jestMessageUtil().formatExecError)(
      error,
      config,
      {
        noStackTrace: false
      },
      undefined,
      true
    );
    process.stderr.write('\n' + formattedError + '\n'); // TODO: set exit code in Jest 25
    // process.exitCode = 1;
  };

Looks like that TODO didn't get done. 😄 But I still think this would be a great change to make. I have tests that are forgetting to await in my project, and this would help track them down.

@SimenB
Copy link
Member

SimenB commented Oct 26, 2020

Right, we should get this into Jest 27. Wanna send a PR?

@NickHeiner
Copy link
Author

Looks like someone else beat me to it! 😄

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants