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

eqoofvh9  于 2023-03-22  发布在  Node.js
关注(0)|答案(5)|浏览(139)

我正在使用Jest框架并有一个测试套件。我想关闭/跳过我的一个测试。
谷歌文档没有给予我答案。
你知道答案或要检查的信息来源吗?

0qx6xfy6

0qx6xfy61#

我在这里找到了答案
https://devhints.io/jest

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

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

链接打开关闭文档

zlwx9yxi

zlwx9yxi2#

您还可以通过使用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);
 });
});
fjaof16o

fjaof16o3#

跳过测试

如果你想在Jest中跳过一个测试,你可以使用test.skip:

test.skip(name, fn)

也是用以下别名:

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

跳过测试套件

另外,如果您想跳过一个测试套件,您可以使用descript.skip:

describe.skip(name, fn)

它也有以下别名:

  • xdescribe(name, fn)
kmbjn2e3

kmbjn2e34#

只运行测试子集:

如果你在一个有很多测试的测试套件中调试/编写/扩展一个测试,你只需要运行你正在做的那个测试,但是给每个测试都添加.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添加到测试套件或文件中的多个测试中!

krugob8w

krugob8w5#

我会为可能在这里倒下的人加上这个答案。就像我在寻找自己一样。
[only,skip,aliases(more details),doc refs,different variants(each,concurrent,failing),how to ignore test files(last section)]
你可以略读一下,找到章节.

跳过或只运行文件中的一个

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
别名可以在元素标题下找到:

您还可以通过编辑器测试@types/jest何时安装:

例如,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只会在文件级起作用。而不会跨所有文件。

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

添加一个f.但是要小心ftest()不是别名!!!!
一个一个十二个一个一个一个十三个一个一个一个
这并不令人难过,我个人更喜欢使用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测试

本节面向正在搜索如何忽略完整文件的用户。

testPathIgnorePattern

如果正则表达式匹配,则忽略regex(regex not glob). Of测试文件。
https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring
Github issue ref
示例:
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/"

testMatch

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

相关问题