Version: 27.2

全局设定

In your test files, Jest puts each of these methods and objects into the global environment. You don’t have to require or import anything to use them. In your test files, Jest puts each of these methods and objects into the global environment. You don’t have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

方法


Reference

afterAll(fn, timeout)

Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

例如:

例如:

  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 是在 describe 块的内部,它运行在 describe 块结尾。

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

Runs a function after each one of the tests in this file completes. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.

afterEach(fn, timeout)

Runs a function after each one of the tests in this file completes. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

例如:

例如:

  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 是在 describe 块的内部,它运行在 describe 块结尾。

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

Runs a function before any of the tests in this file run. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running tests.

beforeAll(fn, timeout)

Runs a function before any of the tests in this file run. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running tests.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

例如:

例如:

  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. });
  16. // Jest will wait for this promise to resolve before running tests.
  17. const globalDatabase = makeGlobalDatabase();
  18. beforeEach(() => {
  19. // Clears the database and adds some testing data.
  20. // Jest will wait for this promise to resolve before running tests.
  21. return globalDatabase.clear().then(() => {
  22. return globalDatabase.insert({testData: 'foo'});
  23. });
  24. });
  25. test('can find things', () => {
  26. return globalDatabase.find('thing', {}, results => {
  27. expect(results.length).toBeGreaterThan(0);
  28. });
  29. });
  30. test('can insert a thing', () => {
  31. return globalDatabase.insert('thing', makeThing(), response => {
  32. expect(response.success).toBeTruthy();
  33. });
  34. });

在这里 beforeAll 确保数据库设置运行测试之前。 If setup was synchronous, you could do this without beforeAll. 关键是Jest将等待承诺解决,所以您也可以进行异步设置。 关键是Jest将等待承诺解决,所以您也可以进行异步设置。

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

Runs a function before each of the tests in this file runs. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running the test.

beforeEach(fn, timeout)

Runs a function before each of the tests in this file runs. Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

例如:

例如:

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

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

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

describe(name, fn) creates a block that groups together several related tests. 例如,如果您有一个myBeverage对象,该对象应该是美味但不酸,您可以通过以下方式测试:

describe(name, fn)

describe(name, fn) creates a block that groups together several related tests. 例如,如果您有一个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. });

This isn’t required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups. But this can be handy if you prefer your tests to be organized into groups.

You can also nest describe blocks if you have a hierarchy of tests:

  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)

Use describe.each if you keep duplicating the same test suites with different data. describe.each allows you to write the test suite once and pass data in. describe.each allows you to write the test suite once and pass data in.

describe.each is available with two APIs:

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

  • table: 数组 of Arrays with the arguments that are passed into the fn for each row.
    • Note If you pass in a 1D array of primitives, internally it will be mapped to a table i.e. [1, 2, 3] -> [[1], [2], [3]]
  • name: 字符串 the title of the test suite.
    • Generate unique test titles by positionally injecting parameters with printf formatting:
      • %p - pretty-format.
      • %s- String.
      • %d- Number.
      • %i - Integer.
      • %f - Floating point value.
      • %j - JSON.
      • %o - Object.
      • %# - Index of the test case.
      • %% - single percent sign (‘%’). This does not consume an argument.
    • Or generate unique test titles by injecting properties of test case object with $variable
      • To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value
      • You can use $# to inject the index of the test case
      • You cannot use $variable with the printf formatting except for %%
  • fn: Function the suite of tests to be ran, this is the function that will receive the parameters in each row as function arguments.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

  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. });
  1. describe.each([
  2. {a: 1, b: 1, expected: 2},
  3. {a: 1, b: 2, expected: 3},
  4. {a: 2, b: 1, expected: 3},
  5. ])('.add($a, $b)', ({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. 2. describe.each`table`(name, fn, timeout)

  • table: Tagged Template Literal
    • First row of variable name column headings separated with |
    • One or more subsequent rows of data supplied as template literal expressions using ${value} syntax.
  • name: String the title of the test suite, use $variable to inject test data into the suite title from the tagged template expressions, and $# for the index of the row.
    • To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value
  • fn: Function the suite of tests to be ran, this is the function that will receive the test data object.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

  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)

如果你只想运行一个描述块中的test用例,你可以使用describe.only

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

  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)

Also under the aliases: fdescribe.each(table)(name, fn) and fdescribe.each`table`(name, fn)

Use describe.only.each if you want to only run specific tests suites of data driven tests.

describe.only.each is available with two APIs:

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)

Also under the aliases: xdescribe.each(table)(name, fn) and xdescribe.each`table`(name, fn)

Use describe.skip.each if you want to stop running a suite of data driven tests.

describe.skip.each is available with two APIs:

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

第一个参数是测试名称; 第二个参数是包含测试期望的函数。 The third argument (optional) is timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Note: If a promise is returned from test, Jest will wait for the promise to resolve before letting the test complete. Jest will also wait if you provide an argument to the test function, usually called done. This could be handy when you want to test callbacks. See how to test async code here. Jest will also wait if you provide an argument to the test function, usually called done. This could be handy when you want to test callbacks. See how to test async code here.

例如,假设fetchBeverageList()返回一个应该解析为具有lemon的列表的承诺。 You can test this with:

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

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

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

Also under the alias: it.concurrent(name, fn, timeout)

Use test.concurrent if you want the test to run concurrently.

Note: test.concurrent is considered experimental - see here for details on missing features and other issues

The first argument is the test name; the second argument is an asynchronous function that contains the expectations to test. The third argument (optional) is timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

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

Note: Use maxConcurrency in configuration to prevents Jest from executing more than the specified amount of tests at the same time

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

Also under the alias: it.concurrent.each(table)(name, fn, timeout)

Use test.concurrent.each if you keep duplicating the same test with different data. test.each allows you to write the test once and pass data in, the tests are all run asynchronously. test.each allows you to write the test once and pass data in, the tests are all run asynchronously.

test.concurrent.each is available with two APIs:

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

  • table: 数组 of Arrays with the arguments that are passed into the test fn for each row.
    • Note If you pass in a 1D array of primitives, internally it will be mapped to a table i.e. [1, 2, 3] -> [[1], [2], [3]]
  • name: 字符串 the title of the test block.
    • Generate unique test titles by positionally injecting parameters with printf formatting:
      • %p - pretty-format.
      • %s- String.
      • %d- Number.
      • %i - Integer.
      • %f - Floating point value.
      • %j - JSON.
      • %o - Object.
      • %# - Index of the test case.
      • %% - single percent sign (‘%’). This does not consume an argument.
  • fn: Function the test to be ran, this is the function that will receive the parameters in each row as function arguments, this will have to be an asynchronous function.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

  1. test.concurrent.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. });

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

  • table: Tagged Template Literal
    • First row of variable name column headings separated with |
    • One or more subsequent rows of data supplied as template literal expressions using ${value} syntax.
  • name: 字符串 the title of the test, use $variable to inject test data into the test title from the tagged template expressions.
    • To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value
  • fn: Function the test to be ran, this is the function that will receive the test data object, this will have to be an asynchronous function.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

  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', async ({a, b, expected}) => {
  7. expect(a + b).toBe(expected);
  8. });

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

Also under the alias: it.concurrent.only.each(table)(name, fn)

Use test.concurrent.only.each if you want to only run specific tests with different test data concurrently.

test.concurrent.only.each is available with two APIs:

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)

Also under the alias: it.concurrent.skip.each(table)(name, fn)

Use test.concurrent.skip.each if you want to stop running a collection of asynchronous data driven tests.

test.concurrent.skip.each is available with two APIs:

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

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

Also under the alias: it.each(table)(name, fn) and it.each`table`(name, fn)

Use test.each if you keep duplicating the same test with different data. test.each allows you to write the test once and pass data in. test.each allows you to write the test once and pass data in.

test.each is available with two APIs:

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

  • table: 数组 of Arrays with the arguments that are passed into the test fn for each row.
    • Note If you pass in a 1D array of primitives, internally it will be mapped to a table i.e. [1, 2, 3] -> [[1], [2], [3]]
  • name: 字符串 the title of the test block.
    • Generate unique test titles by positionally injecting parameters with printf formatting:
      • %p - pretty-format.
      • %s- String.
      • %d- Number.
      • %i - Integer.
      • %f - Floating point value.
      • %j - JSON.
      • %o - Object.
      • %# - Index of the test case.
      • %% - single percent sign (‘%’). This does not consume an argument.
    • Or generate unique test titles by injecting properties of test case object with $variable
      • To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value
      • You can use $# to inject the index of the test case
      • You cannot use $variable with the printf formatting except for %%
  • fn: Function the test to be ran, this is the function that will receive the parameters in each row as function arguments.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

  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. });
  1. test.each([
  2. {a: 1, b: 1, expected: 2},
  3. {a: 1, b: 2, expected: 3},
  4. {a: 2, b: 1, expected: 3},
  5. ])('.add($a, $b)', ({a, b, expected}) => {
  6. expect(a + b).toBe(expected);
  7. });

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

  • table: Tagged Template Literal
    • First row of variable name column headings separated with |
    • One or more subsequent rows of data supplied as template literal expressions using ${value} syntax.
  • name: 字符串 the title of the test, use $variable to inject test data into the test title from the tagged template expressions.
    • To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value
  • fn: Function the test to be ran, this is the function that will receive the test data object.
  • Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

Example:

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

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

Also under the aliases: it.only(name, fn, timeout), and fit(name, fn, timeout)

当你维护一个很大的代码库时,有时可能会发现一个由于某种原因被暂时破坏的测试。 If you want to skip running this test, but you don’t want to delete this code, you can use test.skip to specify some tests to skip.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.

比如说,你有这些测试︰

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

Only the “it is raining” test will run in that test file, since it is run with test.only.

Usually you wouldn’t check code using test.only into source control - you would use it for debugging, and remove it once you have fixed the broken tests.

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

Also under the aliases: it.only.each(table)(name, fn), fit.each(table)(name, fn), it.only.each`table`(name, fn) and fit.each`table`(name, fn)

Use test.only.each if you want to only run specific tests with different test data.

test.only.each is available with two APIs:

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)

Also under the aliases: it.skip(name, fn), xit(name, fn), and xtest(name, fn)

当你维护一个很大的代码库时,有时可能会发现一个由于某种原因被暂时破坏的测试。 If you want to skip running this test, but you don’t want to delete this code, you can use test.skip to specify some tests to 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. });

When you are debugging a large test file, you will often only want to run a subset of tests. You can use .only to specify which tests are the only ones you want to run in that test file.

You could comment the test out, but it’s often a bit nicer to use test.skip because it will maintain indentation and syntax highlighting.

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

Also under the aliases: 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) and xtest.each`table`(name, fn)

Use test.skip.each if you want to stop running a collection of data driven tests.

test.skip.each is available with two APIs:

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)

Also under the alias: it.todo(name)

Use test.todo when you are planning on writing tests. Use test.todo when you are planning on writing tests. These tests will be highlighted in the summary output at the end so you know how many tests you still need todo.

Note: If you supply a test callback function then the test.todo will throw an error. If you have already implemented the test and it is broken and you do not want it to run, then use test.skip instead. If you have already implemented the test and it is broken and you do not want it to run, then use test.skip instead.

API

  • name: String the title of the test plan.

Example:

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