全局设定·续2

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.

test.concurrent('addition of 2 numbers', async () => {  expect(5 + 3).toBe(8);});test.concurrent('subtraction 2 numbers', async () => {  expect(5 - 3).toBe(2);});

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:

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

2. 2. test.concurrent.eachtable(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:

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

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)

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

test.only.eachtable(name, fn)

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

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)

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

test.concurrent.skip.eachtable(name, fn)

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

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

Also under the alias: it.each(table)(name, fn) and it.eachtable(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:

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

2. 2. test.eachtable(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: