本文介绍了开玩笑的问题:调试失败的测试和使用--bail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Jest相当陌生.使用版本16.0.1.

I am fairly new to Jest. Using version 16.0.1.

我有一个包含3个测试的测试套件.我专注于其中一项失败的测试.我的理解是,Jest应该在运行非失败测试之前尝试失败的测试(根据Jest主页:"Jest首先运行先前失败的测试.与--bail一起,它可以快速提供有用的信号."

I have a test suite with 3 tests. I'm focused on one of these tests which is failing. My understanding is that Jest should attempt failing tests before running non-failing tests (as per Jest homepage: "Jest runs previously failed tests first. Together with --bail it provides useful signal quickly."

我似乎无法正常工作.我正在使用以下选项运行Jest:

I can't seem to get this working. I'm running Jest with the following options:

node_modules\jest-cli\bin\jest.js --runInBand --bail

-runInBand确保所有内容都在一个进程中(可用于调试).

The --runInBand ensures that everything is in a single process (useful for debugging).

但是,我的测试都在运行!我在调试器中看到的是,在执行以下代码之前,每个测试都已运行完毕:

However, my tests are all being run! What I see in the debugger is that each test is run to completion before the following code is executed:

_bailIfNeeded(aggregatedResults, watcher) {
  if (this._config.bail && aggregatedResults.numFailedTests !== 0) {
    if (watcher.isWatchMode()) {
      watcher.setState({ interrupted: true });
    } else {
      this._dispatcher.onRunComplete(this._config, aggregatedResults);
      process.exit(1);
    }
  }
}}

我已下达测试命令,以便首先运行失败的测试,但这不会导致进程退出(或者,在其他测试运行之后,这种情况发生得太晚了.)

I've ordered my tests so that the failing test is run first, but it is not causing the process to exit (or rather this is happening too late, after the other tests are run).

此外,我看不到Jest在哪里/如何记录失败测试的状态,因此不确定该功能是否起作用或如何起作用.如果我将失败的测试移至测试套件的末尾,则即使其他测试通过了,也要先运行其他测试,因此我肯定会遗漏一些东西.

Also, I cannot see where/how Jest records the status of failed tests, so am not sure if or how this feature works. If I move my failing test to the end of the test suite, the other tests are run first even though they are passing, so I must be missing something.

为完整起见,我的测试文件如下:

For completeness, my test file looks like this:

describe('My test suite', () => {
  it('Test 1', () => {
    fail("Failing!");
  });
  it('Test 2', () => {
    // Passing
  });
  it('Test 3', () => {
    // Passing
  });
});

非常感谢您的帮助.

推荐答案

我知道这是1年前,但是--bail适用于运行多个测试文件.因此,如果将此测试分为三个文件,则会看到预期的结果.

I know this is >1 year ago, but --bail applies to running multiple test files. So, if you split this test up into three files, you would see the results you are expecting.

这篇关于开玩笑的问题:调试失败的测试和使用--bail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:07