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

Skip one test in test file Jest

提问人:Gleichmut 提问时间:1/6/2018 最后编辑:Seth McClaineGleichmut 更新时间:3/22/2023 访问量:142491

问:

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

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

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

JavaScript 测试 jestjs

评论

0赞 Skam 1/6/2018
只是把它注释掉?
2赞 Gleichmut 1/6/2018
这不是处理您想要故意跳过的测试的正确方法。至少这种行为没有通过我们团队的软件质量检查。(尽管我在遗留代码中有一个注释测试的例子)
0赞 jlh 8/8/2021
对于任何想要以编程方式跳过测试的人:不幸的是,Jest 开发人员对此有点固执,并且看不到这样一个非常有用的功能的价值。请参阅 github.com/facebook/jest/issues/8604github.com/facebook/jest/issues/7245

答:

244赞 Gleichmut 1/6/2018 #1

我在这里找到了答案

https://devhints.io/jest

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

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

链接打开关闭文档

评论

13赞 ptim 6/21/2019
...和test.only()
7赞 oyeyipo 1/23/2021
describe.skip() 用于测试套装
3赞 marko kraljevic 3/20/2022
它根本不起作用,开玩笑很烦人yarn test
1赞 Someone Somewhere 8/5/2023
对于纱线测试,我使用 it.skip('my test', () => {/* 我在这里损坏的测试代码*/})
101赞 Seth McClaine 7/27/2018 #2

您也可以排除或在它们前面加上 .testdescribex

个人测试

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);
 });
});
71赞 Yuci 7/13/2019 #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)
7赞 Bilal Shafi 2/3/2022 #4

仅运行测试的子集:

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

所以来救援了。您可以选择在测试期间使用 flag 跳过其他人,这对于需要添加或调试测试的大型套件非常有用。.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

评论

0赞 Bilal Shafi 2/3/2022
加法:也适用于,就像.onlyit().skip
12赞 Mohamed Allal 9/25/2022 #5

我会为可能跌倒在这里的人添加这个答案。当我在寻找自己时。

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

你可以略读。获取部分。

从文件中跳过或仅运行一个

skip() [包括文档引用]

使用函数跳过测试或测试套件(描述).skip()

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

=>

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

您可以使用别名:

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

这同样适用于别名。it

it.skip('something', () => {})
xit('something', () => {})

对于一个 :descripe

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()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(() => {
   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() 的别名:

添加 .但要小心不是别名!!!fftest()

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

使用 config 或 cli 忽略测试

本节适用于正在寻找如何忽略完整文件的人。

testPathIgnorePatterns

正则表达式 .的测试文件,如果正则表达式匹配,则忽略它们。(regex not 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;

或者使用 cli:

编号: 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/"

test匹配

testMatch 可以是确定应该运行的内容的另一种方法(就像only())

它确实使用而不是globs regex

编号: https://jestjs.io/docs/configuration#testmatch-arraystring

您也可以使用 glob 否定来忽略某些文件。

例:

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

还有 cli 版本:

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

评论

0赞 sovemp 9/1/2023
添加 .only(test 或它)会导致它跳过该测试,并且只跳过我的那个测试。
0赞 Mohamed Allal 9/3/2023
我不明白。jestjs.io/docs/api#testonlyname-fn-timeout。api 始终是相同的。 或者将使 only() 块仅运行。而不是跳过它们?要么我不明白你的意思?要么你缺少一些东西。可能是你用的 skip()。或一些.如果你能提供细节,那就太好了。或指向完整示例的链接。it.only()test.only()x