Jest 全局API

2021-09-18 20:46 更新

在测试文件中,Jest 将这些方法和对象中的每一个都放入全局环境中。不必要求或导入任何东西即可使用它们。但是,如果你更喜欢显式导入,则可以执行​import {describe, expect, test} from '@jest/globals'​.

方法

参考

​afterAll(fn, timeout)

在此文件中的所有测试完成后运行一个函数。如果函数返回一个承诺或者是一个生成器,Jest 会在继续之前等待该承诺解决。

或者,可以提供​timeout​(以毫秒为单位)用于指定    在中止之前等待的时间。注意:默认超时为 5 秒。

这通常是有用的如果你想要清理一些在测试之间共享的全局设置状态。

例如:

  1. const globalDatabase = makeGlobalDatabase();
  2. function cleanUpDatabase(db) {
  3. db.cleanUp();
  4. }
  5. afterAll(() => {
  6. cleanUpDatabase(globalDatabase);
  7. });
  8. test('can find things', () => {
  9. return globalDatabase.find('thing', {}, results => {
  10. expect(results.length).toBeGreaterThan(0);
  11. });
  12. });
  13. test('can insert a thing', () => {
  14. return globalDatabase.insert('thing', makeThing(), response => {
  15. expect(response.success).toBeTruthy();
  16. });
  17. });

这里 ​afterAll ​确保那 ​cleanUpDatabase ​调用后运行所有测试。

afterAll ​是在 ​describe ​块的内部,它运行在 ​describe ​块结尾。

如果你想要在每个测试后运行一些清理,请使用 ​afterEach​。

​afterEach(fn, timeout)​

在此文件中的每个测试完成后运行一个函数。如果函数返回一个承诺或者是一个生成器,Jest 会在继续之前等待该承诺解决。

或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待的时间。注意:默认超时为 5 秒。

这通常是有用的如果你想要清理一些在每个测试之间共享的全局设置状态。

例如:

  1. const globalDatabase = makeGlobalDatabase();
  2. function cleanUpDatabase(db) {
  3. db.cleanUp();
  4. }
  5. afterEach(() => {
  6. cleanUpDatabase(globalDatabase);
  7. });
  8. test('can find things', () => {
  9. return globalDatabase.find('thing', {}, results => {
  10. expect(results.length).toBeGreaterThan(0);
  11. });
  12. });
  13. test('can insert a thing', () => {
  14. return globalDatabase.insert('thing', makeThing(), response => {
  15. expect(response.success).toBeTruthy();
  16. });
  17. });

这里 ​afterEach ​确保那 ​cleanUpDatabase ​调用后运行每个测试。

afterEach ​是在 ​describe ​块的内部,它运行在 ​describe ​块结尾。

如果你想要一些清理只运行一次,在所有测试运行后,使用 ​afterAll ​。

​beforeAll(fn, timeout)​

在此文件中的任何测试运行之前运行一个函数。如果函数返回一个承诺或者是一个生成器,Jest 会在运行测试之前等待该承诺解决。

或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待的时间。注意:默认超时为 5 秒。

如果要设置将被许多测试使用的一些全局状态,这通常很有用。

例如:

  1. const globalDatabase = makeGlobalDatabase();
  2. beforeAll(() => {
  3. // Clears the database and adds some testing data.
  4. // Jest will wait for this promise to resolve before running tests.
  5. return globalDatabase.clear().then(() => {
  6. return globalDatabase.insert({testData: 'foo'});
  7. });
  8. });
  9. // Since we only set up the database once in this example, it's important
  10. // that our tests don't modify it.
  11. test('can find things', () => {
  12. return globalDatabase.find('thing', {}, results => {
  13. expect(results.length).toBeGreaterThan(0);
  14. });
  15. });

在这里 ​beforeAll ​确保数据库设置运行测试之前。如果安装程序是同步的,那么你可以不必事先进行设置。 关键是Jest将等待承诺解决,所以你也可以进行异步设置。

如果​beforeAll​在​describe​块内,则它将在 ​describe ​块的开头运行。

如果要在每次测试之前运行某些操作,而不是在任何测试运行之前运行,请改用​beforeEach​。

​beforeEach(fn, timeout)​

在此文件中的每个测试运行之前运行一个函数。如果函数返回一个承诺或者是一个生成器,Jest 会在运行测试之前等待该承诺解决。

或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待的时间。注意:默认超时为 5 秒。

如果要重置许多测试将使用的全局状态,这通常很有用。

例如:

  1. const globalDatabase = makeGlobalDatabase();
  2. beforeEach(() => {
  3. // Clears the database and adds some testing data.
  4. // Jest will wait for this promise to resolve before running tests.
  5. return globalDatabase.clear().then(() => {
  6. return globalDatabase.insert({testData: 'foo'});
  7. });
  8. });
  9. test('can find things', () => {
  10. return globalDatabase.find('thing', {}, results => {
  11. expect(results.length).toBeGreaterThan(0);
  12. });
  13. });
  14. test('can insert a thing', () => {
  15. return globalDatabase.insert('thing', makeThing(), response => {
  16. expect(response.success).toBeTruthy();
  17. });
  18. });

这里,​beforeEach​确保每个测试重置数据库。

如果​beforeEach​在​describe​块内,则它将在 ​describe ​块中为每个测试运行。

如果只需要运行一些设置代码,在任何测试运行之前,请使用​beforeAll​。

​describe(name, fn)​

describe(name, fn)​ 创建将多个相关测试分组在一起的块例如,如果你有一个​myBeverage​对象,该对象应该是不错的,可以通过以下方式测试:

  1. const myBeverage = {
  2. delicious: true,
  3. sour: false,
  4. };
  5. describe('my beverage', () => {
  6. test('is delicious', () => {
  7. expect(myBeverage.delicious).toBeTruthy();
  8. });
  9. test('is not sour', () => {
  10. expect(myBeverage.sour).toBeFalsy();
  11. });
  12. });

这不是必需的 - 可以​test​直接在顶层编写块。但是,如果你希望将测试组织成组,这会很方便。

describe​如果有测试层次结构,还可以嵌套块:

  1. const binaryStringToNumber = binString => {
  2. if (!/^[01]+$/.test(binString)) {
  3. throw new CustomError('Not a binary number.');
  4. }
  5. return parseInt(binString, 2);
  6. };
  7. describe('binaryStringToNumber', () => {
  8. describe('given an invalid binary string', () => {
  9. test('composed of non-numbers throws CustomError', () => {
  10. expect(() => binaryStringToNumber('abc')).toThrowError(CustomError);
  11. });
  12. test('with extra whitespace throws CustomError', () => {
  13. expect(() => binaryStringToNumber(' 100')).toThrowError(CustomError);
  14. });
  15. });
  16. describe('given a valid binary string', () => {
  17. test('returns the correct number', () => {
  18. expect(binaryStringToNumber('100')).toBe(4);
  19. });
  20. });
  21. });

​describe.each(table)(name, fn, timeout​)

使用​describe.each​,如果你继续重复使用不同的数据相同的测试套件。​describe.each​允许编写一次测试套件并传入数据。

describe.each​ 有两个 API 可用:

1. ​describe.each(table)(name, fn, timeout)​

  • table​: ​Arrayof Arrays​ 的参数被传递到fn每一行。

        ○ 注意如果你传入一个一维基元数组,它在内部将被映射到一个表,即​[1, 2, 3] -> [[1], [2], [3]]

  • name​:​String​测试套件的标题。 

        ○ 通过位置注入带有printf格式的参数来生成唯一的测试标题:

            ■ ​%p​- pertty-format。

            ■ ​%s​- String。

            ■ ​%d​- Number。

            ■ ​%i​ - Integer。

            ■ ​%f​ - Float。

            ■ ​%j​ - JSON。

            ■ ​%o​ - Object。

            ■ ​%#​ - 测试用例的索引。

            ■ ​%%​- 单个百分号 ('%')。这不消耗参数。

  • fn​:​Function​要运行的测试套件,这是将每行中的参数作为函数参数接收的函数。
  • 或者,您可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. describe.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. test(`returns ${expected}`, () => {
  7. expect(a + b).toBe(expected);
  8. });
  9. test(`returned value not be greater than ${expected}`, () => {
  10. expect(a + b).not.toBeGreaterThan(expected);
  11. });
  12. test(`returned value not be less than ${expected}`, () => {
  13. expect(a + b).not.toBeLessThan(expected);
  14. });
  15. });

2. ​describe.each`table`(name, fn, timeout)​

  • table​: ​Tagged Template Literal

        ○ 变量名列标题的第一行用 ​|

        ○ 使用​${value}​语法作为模板文字表达式提供的一个或多个后续数据行。

  • name​:​String​测试套件的标题,用于$variable将测试数据从标记的模板表达式中注入套件标题。

        ○ 要注入嵌套对象值,可以提供一个 ​​​​keyPath​​​即​ $variable.path.to.value

  • fn​:​Function​要运行的测试套件,这是将接收测试数据对象的函数。
  • 或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. describe.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('$a + $b', ({a, b, expected}) => {
  7. test(`returns ${expected}`, () => {
  8. expect(a + b).toBe(expected);
  9. });
  10. test(`returned value not be greater than ${expected}`, () => {
  11. expect(a + b).not.toBeGreaterThan(expected);
  12. });
  13. test(`returned value not be less than ${expected}`, () => {
  14. expect(a + b).not.toBeLessThan(expected);
  15. });
  16. });

​describe.only(name, fn)​

还有别名:​fdescribe(name, fn)

如果你只想运行一个描述块,你可以使用​describe.only​:

  1. describe.only('my beverage', () => {
  2. test('is delicious', () => {
  3. expect(myBeverage.delicious).toBeTruthy();
  4. });
  5. test('is not sour', () => {
  6. expect(myBeverage.sour).toBeFalsy();
  7. });
  8. });
  9. describe('my other beverage', () => {
  10. // ... will be skipped
  11. });

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

同样在别名下:​fdescribe.each(table)(name, fn)​和​fdescribe.each`table`(name, fn)

describe.only.each​如果你只想运行数据驱动测试的特定测试套件,请使用。

describe.only.each​ 有两个 API 可用:

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

  1. describe.only.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. test(`returns ${expected}`, () => {
  7. expect(a + b).toBe(expected);
  8. });
  9. });
  10. test('will not be ran', () => {
  11. expect(1 / 0).toBe(Infinity);
  12. });

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

  1. describe.only.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. test('passes', () => {
  8. expect(a + b).toBe(expected);
  9. });
  10. });
  11. test('will not be ran', () => {
  12. expect(1 / 0).toBe(Infinity);
  13. });

​describe.skip(name, fn)​

还有别名:​xdescribe(name, fn)

如果不想运行特定的描述块,可以使用​describe.skip​:

  1. describe('my beverage', () => {
  2. test('is delicious', () => {
  3. expect(myBeverage.delicious).toBeTruthy();
  4. });
  5. test('is not sour', () => {
  6. expect(myBeverage.sour).toBeFalsy();
  7. });
  8. });
  9. describe.skip('my other beverage', () => {
  10. // ... will be skipped
  11. });

使用​describe.skip​通常是临时注释掉一大块测试的更干净的替代方法。

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

同样在别名下:​xdescribe.each(table)(name, fn)​和​xdescribe.each`table`(name, fn)

使用​describe.skip.each​,如果你想停止运行了一套驱动测试数据。

describe.skip.each​ 有两个 API 可用:

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

  1. describe.skip.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. test(`returns ${expected}`, () => {
  7. expect(a + b).toBe(expected); // will not be ran
  8. });
  9. });
  10. test('will be ran', () => {
  11. expect(1 / 0).toBe(Infinity);
  12. });

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

  1. describe.skip.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. test('will not be ran', () => {
  8. expect(a + b).toBe(expected); // will not be ran
  9. });
  10. });
  11. test('will be ran', () => {
  12. expect(1 / 0).toBe(Infinity);
  13. });

​test(name, fn, timeout)​

还有别名:​it(name, fn, timeout)

在测试文件中你需要的只是test运行测试的方法。例如,假设有一个函数​inchesOfRain()​应该为零。整个测试可能是:

  1. test('did not rain', () => {
  2. expect(inchesOfRain()).toBe(0);
  3. });

第一个参数是测试名称; 第二个参数是包含测试期望的函数。第三个参数(可选)是​timeout​(以毫秒为单位),用于指定中止前的等待时间。注意:默认超时为5秒。

注意:如果从返回承诺​test​,Jest 将等待承诺解决,然后让测试完成。如果你向通常调用的测试函数提供参数, Jest 也会等待​done​。当你想测试回调时,这可能会很方便。在此处查看如何测试异步代码。

例如,假设​fetchBeverageList()​返回一个应该解析为其中包含的列表的承诺​lemon​。可以使用以下方法进行测试:

  1. test('has lemon in it', () => {
  2. return fetchBeverageList().then(list => {
  3. expect(list).toContain('lemon');
  4. });
  5. });

即使对​test​的调用将立即返回,测试还没有完成,直到承诺也解决。

​test.concurrent(name, fn, timeout)​

同样在别名下:​ it.concurrent(name, fn, timeout)

​如果希望测试同时运行,请使用​test.concurrent

注意:​test.concurrent​被认为是实验性的 - 请参阅 [此处]) https://github.com/facebook/jest/labels/Area%3A%20Concurrent ) 了解有关缺失功能和其他问题的详细信息

第一个参数是测试名称;第二个参数是一个包含要测试的期望的异步函数。第三个参数(可选)是​timeout​(以毫秒为单位)用于指定在中止之前等待多长时间。注意:默认超时为 5 秒。

  1. test.concurrent('addition of 2 numbers', async () => {
  2. expect(5 + 3).toBe(8);
  3. });
  4. test.concurrent('subtraction 2 numbers', async () => {
  5. expect(5 - 3).toBe(2);
  6. });
注意:​maxConcurrency​在配置中使用以防止 Jest 同时执行超过指定数量的测试

​test.concurrent.each(table)(name, fn, timeout)​

同样在别名下:​ it.concurrent.each(table)(name, fn, timeout)

使用​test.concurrent.each​,如果你继续重复使用不同的数据相同的测试。​test.each​允许编写一次测试并传入数据,测试都是异步运行的。

test.concurrent.each​ 有两个 API 可用:

1. ​test.concurrent.each(table)(name, fn, timeout)​

  • table​: ​Arrayof ​带有传递到fn每一行测试中的参数的数组。

     ○  注意如果你传入一个一维基元数组,它在内部将被映射到一个表,即​[1, 2, 3] -> [[1], [2], [3]]

  • name​:​String​测试块的标题。

     ○  通过位置注入带有printf格式的参数来生成唯一的测试标题:

            ■ ​ %p​- Pretty-format。

            ■ ​ %s​- String。

            ■ ​ %d​- Number。

            ■ ​ %i​ - Integer。

            ■ ​ %f​ - Float。

            ■  ​%j​ - JSON。

            ■ ​ %o​ - Object。

            ■ ​ %#​ - 测试用例的索引。

            ■ ​ %%​- 单个百分号 ('%')。这不消耗参数。 

  • fn​:​Function​要运行的测试,这是将接收每一行中的参数作为函数参数的函数,这必须是一个异步函数。
  • 或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. test.concurrent.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. expect(a + b).toBe(expected);
  7. });

2. ​test.concurrent.each`table`(name, fn, timeout)​

  • table​: ​Tagged Template Literal

    ○  变量名列标题的第一行用 ​|

    ○  使用​${value}​语法作为模板文字表达式提供的一个或多个后续数据行。

  • name​:​String​测试的标题,用于​$variable​将测试数据从标记的模板表达式中注入到测试标题中。

    ○  要注入嵌套对象值,可以提供一个 keyPath 即​ $variable.path.to.value

  • fn​:​Function​要运行的测试,这是将接收测试数据对象的函数,这必须是一个异步函数。
  • 或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. test.concurrent.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. expect(a + b).toBe(expected);
  8. });

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

同样在别名下:​ it.concurrent.only.each(table)(name, fn)

使用​test.concurrent.only.each​,如果你只想用不同的测试数据上运行特定的测试同时进行。

test.concurrent.only.each​ 有两个 API 可用:

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

  1. test.concurrent.only.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', async (a, b, expected) => {
  6. expect(a + b).toBe(expected);
  7. });
  8. test('will not be ran', () => {
  9. expect(1 / 0).toBe(Infinity);
  10. });

​test.only.each`table`(name, fn)​

  1. test.concurrent.only.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', async ({a, b, expected}) => {
  7. expect(a + b).toBe(expected);
  8. });
  9. test('will not be ran', () => {
  10. expect(1 / 0).toBe(Infinity);
  11. });

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

同样在别名下: ​it.concurrent.skip.each(table)(name, fn)

test.concurrent.skip.each​如果要停止运行异步数据驱动的测试集合,请使用。

test.concurrent.skip.each​ 有两个 API 可用:

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

  1. test.concurrent.skip.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', async (a, b, expected) => {
  6. expect(a + b).toBe(expected); // will not be ran
  7. });
  8. test('will be ran', () => {
  9. expect(1 / 0).toBe(Infinity);
  10. });

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

  1. test.concurrent.skip.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', async ({a, b, expected}) => {
  7. expect(a + b).toBe(expected); // will not be ran
  8. });
  9. test('will be ran', () => {
  10. expect(1 / 0).toBe(Infinity);
  11. });

​test.each(table)(name, fn, timeout)​

同样在别名下:​it.each(table)(name, fn)​和​it.each`table`(name, fn)

使用​test.each​,如果你继续重复使用不同的数据相同的测试。​test.each​允许您编写一次测试并传入数据。

test.each​ 有两个 API 可用:

1. ​test.each(table)(name, fn, timeout)​

  • table​: ​Arrayof ​带有传递到​fn​每一行测试中的参数的数组。

    ○ 注意如果你传入一个一维基元数组,它在内部将被映射到一个表,即​[1, 2, 3] -> [[1], [2], [3]]

  • name​:​String​测试块的标题。

    ○ 通过位置注入带有printf格式的参数来生成唯一的测试标题:

        ■ ​%p​- Pretty-format。

        ■ ​%s​- String。

        ■ ​%d​- Number。

        ■ ​%i​ - Integer。

        ■ %f- Float。

        ■ ​%j​ - JSON。

        ■ ​%o​ - Object。

        ■ ​%#​ - 测试用例的索引。

        ■ ​%%​- 单个百分号 ('%')。这不消耗参数。

  • fn​:​Function​要运行的测试,这是将接收每一行中的参数作为函数参数的函数。
  • 或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. test.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. expect(a + b).toBe(expected);
  7. });

2. ​test.each`table`(name, fn, timeout)​

  • table​: ​Tagged Template Literal

    ○ 变量名列标题的第一行用 ​|

    ○ 使用​${value}​语法作为模板文字表达式提供的一个或多个后续数据行。

  • name​:​String​测试的标题,用于​$variable​将测试数据从标记的模板表达式中注入到测试标题中。

    ○ 要注入嵌套对象值,您可以提供一个 keyPath 即 ​$variable.path.to.value

  • fn​:​Function​要运行的测试,这是将接收测试数据对象的函数。
  • 或者,您可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待每一行的时间。注意:默认超时为 5 秒。

示例:

  1. test.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. expect(a + b).toBe(expected);
  8. });

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

同样在别名下:i​t.only(name, fn, timeout)​, 和​fit(name, fn, timeout)

当调试大型测试文件时,通常只想运行测试的子集。可以使用​.only​指定哪些测试是您要在该测试文件中运行的唯一测试。

或者,可以提供​timeout​(以毫秒为单位)用于指定在中止之前等待的时间。注意:默认超时为 5 秒。

比如说,你有这些测试︰

  1. test.only('it is raining', () => {
  2. expect(inchesOfRain()).toBeGreaterThan(0);
  3. });
  4. test('it is not snowing', () => {
  5. expect(inchesOfSnow()).toBe(0);
  6. });

只有“it is raining”测试会在该测试文件中运行,因为它使用​test.only​.

通常,不会使用​test.only​源代码管理检查代码——将使用它进行调试,并在修复损坏的测试后将其删除。

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

同样在别名下:​it.only.each(table)(name, fn)​, ​fit.each(table)(name, fn)​,​it.only.each`table`(name, fn)​和​fit.each`table`(name, fn)

使用​test.only.each​,如果你想只运行不同的测试数据的特定测试。

test.only.each​ 有两个 API 可用:

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

  1. test.only.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. expect(a + b).toBe(expected);
  7. });
  8. test('will not be ran', () => {
  9. expect(1 / 0).toBe(Infinity);
  10. });

​test.only.each`table`(name, fn)​

  1. test.only.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. expect(a + b).toBe(expected);
  8. });
  9. test('will not be ran', () => {
  10. expect(1 / 0).toBe(Infinity);
  11. });

​test.skip(name, fn)​

同样在别名下:​it.skip(name, fn), xit(name, fn)​, 和​xtest(name, fn)

当你维护一个很大的代码库时,有时可能会发现一个由于某种原因被暂时破坏的测试。 如果要跳过运行此测试,但不想删除此代码,可以使用​test.skip​指定要跳过的某些测试。

比如说,你有这些测试︰

  1. test('it is raining', () => {
  2. expect(inchesOfRain()).toBeGreaterThan(0);
  3. });
  4. test.skip('it is not snowing', () => {
  5. expect(inchesOfSnow()).toBe(0);
  6. });

只有“it is raining”测试才会运行,因为另一个测试是使用​test.skip​。

可以将测试注释掉,但使用起来通常会更好一些,​test.skip​因为它会保持缩进和语法突出显示。

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

同样在别名下:​it.skip.each(table)(name, fn)​, ​xit.each(table)(name, fn)​, ​xtest.each(table)(name, fn)​, ​it.skip.each`table`(name, fn)​,​xit.each`table`(name, fn)​和​xtest.each`table`(name, fn)

test.skip.each​如果要停止运行数据驱动的测试集合,请使用。

test.skip.each​ 有两个 API 可用:

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

  1. test.skip.each([
  2. [1, 1, 2],
  3. [1, 2, 3],
  4. [2, 1, 3],
  5. ])('.add(%i, %i)', (a, b, expected) => {
  6. expect(a + b).toBe(expected); // will not be ran
  7. });
  8. test('will be ran', () => {
  9. expect(1 / 0).toBe(Infinity);
  10. });

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

  1. test.skip.each`
  2. a | b | expected
  3. ${1} | ${1} | ${2}
  4. ${1} | ${2} | ${3}
  5. ${2} | ${1} | ${3}
  6. `('returns $expected when $a is added $b', ({a, b, expected}) => {
  7. expect(a + b).toBe(expected); // will not be ran
  8. });
  9. test('will be ran', () => {
  10. expect(1 / 0).toBe(Infinity);
  11. });

​test.todo(name)​

同样在别名下: ​it.todo(name)

使用​test.todo​当你在编写测试计划。这些测试将在最后的摘要输出中突出显示,以便您知道还需要执行多少测试。

注意:如果你提供测试回调函数,​test.todo​则将引发错误。如果已经实施了测试并且它已损坏并且你不希望它运行,那么请​test.skip​改用。

API

  • name​:​String​测试计划的标题。

示例:

  1. const add = (a, b) => a + b;
  2. test.todo('add should be associative');


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号