【问题标题】:Skip one test in test file Jest跳过测试文件 Jest 中的一项测试
【发布时间】:2018-06-15 22:51:21
【问题描述】:

我正在使用 Jest 框架并有一个测试套件。我想关闭/跳过我的一项测试。

谷歌搜索文档没有给我答案。

您知道要检查的答案或信息来源吗?

【问题讨论】:

  • 只是注释掉?
  • 这不是处理您想要跳过的测试的正确方法。至少这种行为没有通过我们团队的软件质量检查。 (尽管我有一个遗留代码中的注释测试示例)
  • 对于任何想以编程方式跳过测试的人:不幸的是,Jest 开发人员对此有点固执,并没有看到这样一个非常有用的功能的价值。见github.com/facebook/jest/issues/8604github.com/facebook/jest/issues/7245

标签: javascript node.js testing jestjs


【解决方案1】:

我在这里找到了答案

https://devhints.io/jest

test('it is raining', () => {
  expect(inchesOfRain()).toBeGreaterThan(0);
});

test.skip('it is not snowing', () => {
  expect(inchesOfSnow()).toBe(0);
});

Link on off doc

【讨论】:

  • ...和test.only()
  • describe.skip() 用于测试套装
【解决方案2】:

您还可以排除testdescribe,方法是在它们前面加上x

个别测试

describe('All Test in this describe will be run', () => {
  xtest('Except this test- This test will not be run', () => {
   expect(true).toBe(true);
  });
  test('This test will be run', () => {
   expect(true).toBe(true);
  });
});

描述中的多个测试

xdescribe('All tests in this describe will be skipped', () => {
 test('This test will be skipped', () => {
   expect(true).toBe(true);
 });

 test('This test will be skipped', () => {
   expect(true).toBe(true);
 });
});

【讨论】:

  • 谢谢,我觉得这个解决方案简单实用。
【解决方案3】:

跳过测试

如果您想跳过 Jest 中的测试,可以使用test.skip

test.skip(name, fn)

也属于以下别名:

  • it.skip(name, fn)
  • xit(name, fn)
  • xtest(name, fn)

跳过测试套件

此外,如果您想跳过测试套件,可以使用describe.skip

describe.skip(name, fn)

也属于以下别名:

  • xdescribe(name, fn)

【讨论】:

    【解决方案4】:

    只运行一个测试子集:

    如果您在一个包含许多测试的测试套件中调试/编写/扩展一个测试,您只需要运行您正在处理的那个,但将.skip 添加到每个人可能会很痛苦。

    所以.only 来救援了。您可以选择在测试期间使用 .only 标志跳过其他人,这对于需要添加或调试测试的大型套件非常有用。

    describe('some amazing test suite', () => {
      test('number one', () => {
        // some testing logic
      }
      test('number two', () => {
        // some testing logic
      }
      ...
      test('number x', () => {
        // some testing logic
      }
      test.only('new test or the one needs debugging', () => {
        // Now when you run this suite only this test will run
        // so now you are free to debug or work on business logic
        // instead to wait for other tests to pass every time you run jest
        // You can remove `.only` flag once you're done!
      }
    }
    
    

    如果您想同时运行多个测试,还可以将.only 添加到测试套件或文件中的多个测试!

    【讨论】:

    • 加法:.only 也适用于it(),就像.skip 一样
    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 2013-04-15
    • 2019-06-13
    • 1970-01-01
    • 2019-05-01
    • 2017-05-26
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多