跳过 Jest 测试文件中的一个测试

192

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

搜索文档并没有给我答案。

您是否知道答案或检查信息来源?


只是将其注释掉吗? - Skam
2
处理测试时故意跳过并不是正确的方式。在我们团队中,这种行为至少无法通过软件质量检查。(尽管我有一个遗留代码中被注释掉的测试用例的例子) - Gleichmut
对于想要以编程方式跳过测试的任何人:Jest开发人员不幸地对此有些固执,他们没有看到这个非常有用的功能的价值。请参见https://github.com/facebook/jest/issues/8604和https://github.com/facebook/jest/issues/7245。 - jlh
5个回答

242

13
...和 test.only() - ptim
7
describe.skip()用于测试套件,表示跳过该测试套件。 - oyeyipo
3
它在使用 yarn test 时根本无法工作,jest非常烦人。 - marko kraljevic
1
使用yarn测试时,我会使用it.skip('我的测试', () => {/* 这里是我有问题的测试代码*/})。 - Someone Somewhere

101

你也可以通过在 x 前缀下排除 testdescribe

个别测试

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);
  });
});

在describe块中进行多个测试

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);
 });
});

71

跳过测试

如果你想要跳过 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)

12

我想为那些可能会遇到这个问题的人添加这个答案,因为我自己也在搜索。

[仅限、跳过、别名(更多细节)、文档引用、不同变体(每个、并发、失败)、如何忽略测试文件(最后一节)]

你可以略读来获取各个部分。

跳过或仅运行一个文件内的内容

skip() [包括文档引用]

使用 .skip() 函数来跳过一个测试测试套件(描述)。

test('something', () => {})

=>

test.skip('something', () => {})

您可以使用别名:

xtest('something', () => {})

同样适用于it,它也是一个别名。
it.skip('something', () => {})
xit('something', () => {})

关于一个描述

describe.skip(() => {})
xdescribe(() => {})

文档参考:
https://jestjs.io/docs/api#testskipname-fn
https://jestjs.io/docs/api#describeskipname-fn

别名可以在元素标题下找到:

enter image description here

你也可以通过编辑器测试是否安装了@types/jest:

enter image description here

ftest 不存在,例如。请参见下一个 only() 部分。

注意:

test.skip.each(table)(name, fn)

针对数据驱动测试,同样适用相同的别名。请查看下面的参考链接。

参考链接:https://jestjs.io/docs/api#testskipeachtablename-fn

同样适用于:

test.concurrent.skip.each(table)(name, fn)

用于并发测试。此测试无 x 别名。请查看下面的参考链接。

参考链接: https://jestjs.io/docs/api#testconcurrentskipeachtablename-fn

还有,

describe.skip.each(table)(name, fn)

参考: https://jestjs.io/docs/api#describeskipeachtablename-fn

test.skip.failing(name, fn, timeout)

参考: https://jestjs.io/docs/api#testskipfailingname-fn-timeout

only()

如果您想仅运行一个测试用例而不是其他内容,请使用 only()

您可以将 only() 与多个测试或测试套件(describe)一起使用。这将仅运行已添加 only() 的测试用例。

注意:only仅在文件级别上起作用,而不跨越所有文件。

test('test 1', () => {})
test('test 2', () => {})
test('test 3', () => {})
test.only('test 4', () => {}) // only this would run in this file

多个:

test('test 1', () => {})
test.only('test 2', () => {}) // only this
test('test 3', () => {})
test.only('test 4', () => {}) // and this would run in this file

使用describe函数:

describe(() => {
   test('test 1', () => {})       // nothing will run here
   test.only('test 2', () => {}) // 
})

describe.only(() => {
   test('test 1', () => {})
   test.only('test 2', () => {}) // only this test
   test('test 3', () => {})
   test.only('test 4', () => {}) // and this one would be run (because this is the only active describe block) 
})

describe(() => {
   test('test 1', () => {}).      // No test will run here
   test.only('test 2', () => {}) //
})

only() 的别名:

添加一个 f。但要小心,ftest() 不是别名!!!

it.only()
fit()

describe.only()
fdescribe()

ftest() // WRONG ERROR !!! No such an alias SADLY!!!

嗯,这并不令人难过。我个人更喜欢使用only(),因为它更详细和易读。

文档参考:
https://jestjs.io/docs/api#testonlyname-fn-timeout
https://jestjs.io/docs/api#describeonlyname-fn

也请注意:

describe.only.each(table)(name, fn)

参考: https://jestjs.io/docs/api#describeonlyeachtablename-fn

test.concurrent.only.each(table)(name, fn)

参考: https://jestjs.io/docs/api#testconcurrentonlyeachtablename-fn

test.only.failing(name, fn, timeout)

引用:https://jestjs.io/docs/api#testonlyfailingname-fn-timeout

使用配置或命令行忽略测试

本节面向那些正在寻找如何忽略整个文件的人。

testPathIgnorePatterns

一个正则表达式(不是 glob)。如果正则表达式匹配,则忽略测试文件。

https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring

Github问题参考

例子:

jest.config.ts

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  verbose: true,
  automock: false,
  testPathIgnorePatterns: ['.*/__fixtures__/.*'] // here
};

export default config;

或者使用命令行界面:

参考:https://jestjs.io/docs/cli#--testpathignorepatternsregexarray

jest --testPathIgnorePatterns=".*/__fixtures__/.*"

对于一个数组(多个正则表达式):

有不同的方法:

# one regex with or operator `|` 
jest --testPathIgnorePatterns=".*/__fixtures__/.*|<rootDir>/src/someDir/"

# paranthesis with spaces
jest --testPathIgnorePatterns="\(.*/__fixtures__/.* <rootDir>/src/someDir/\)"

# using the argument multiple times
jest --testPathIgnorePatterns=".*/__fixtures__/.*" --testPathIgnorePatterns="<rootDir>/src/someDir/"

testMatch

testMatch可以是另一种确定应该运行什么的方式(就像only()一样)

它使用的是globs不是regex

参考链接:https://jestjs.io/docs/configuration#testmatch-arraystring

您还可以使用glob negation来忽略某些文件。

例如:

{
  // ... rest of the package
  "jest": {
    "testMatch": [
      "**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
      "**/!(DISABLED.)?(*.)+(spec|test).[tj]s?(x)"
    ]
  }
}

还有命令行版本:

https://jestjs.io/docs/cli#--testmatch-glob1--globn

请注意,如果没有任何参数。
jest my-test #or
jest path/to/my-test.js
jest **/some/**/*.spec.*

这将只运行与模式匹配的测试文件。

参考:https://jestjs.io/docs/cli#running-from-the-command-line


在我的情况下,只要添加.only(测试或它),就会跳过那个测试,仅限于那一个。 - sovemp
我不明白。https://jestjs.io/docs/api#testonlyname-fn-timeout。API始终是相同的。`it.only()`或`test.only()`将使only()块仅运行,而不跳过它们。我是不是没有理解你的意思?或者有些东西你遗漏了。可能你使用了skip()或一些`x`。如果你能提供详细信息,那就太好了。或者一个完整示例的链接。 - undefined

7

运行部分测试:

如果你正在调试/编写/扩展测试套件中的一个测试,而该套件包含许多测试,你只需要运行正在处理的测试,但将.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 一样。 - Bilal Shafi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接