全局设定·续1
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:
describe.each([ [1, 1, 2], [1, 2, 3], [2, 1, 3],])('.add(%i, %i)', (a, b, expected) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); test(`returned value not be greater than ${expected}`, () => { expect(a + b).not.toBeGreaterThan(expected); }); test(`returned value not be less than ${expected}`, () => { expect(a + b).not.toBeLessThan(expected); });});
describe.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}) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); test(`returned value not be greater than ${expected}`, () => { expect(a + b).not.toBeGreaterThan(expected); }); test(`returned value not be less than ${expected}`, () => { expect(a + b).not.toBeLessThan(expected); });});
2. 2. describe.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: 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:
describe.each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3}`('$a + $b', ({a, b, expected}) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); }); test(`returned value not be greater than ${expected}`, () => { expect(a + b).not.toBeGreaterThan(expected); }); test(`returned value not be less than ${expected}`, () => { expect(a + b).not.toBeLessThan(expected); });});
describe.only(name, fn)
如果你只想运行一个描述块中的test用例,你可以使用describe.only:
如果beforeAll在describe块内,则它将在 describe 块的开头运行。
describe.only('my beverage', () => { test('is delicious', () => { expect(myBeverage.delicious).toBeTruthy(); }); test('is not sour', () => { expect(myBeverage.sour).toBeFalsy(); });});describe('my other beverage', () => { // ... will be skipped});
describe.only.each(table)(name, fn)
Also under the aliases: fdescribe.each(table)(name, fn) and fdescribe.eachtable(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)
describe.only.each([ [1, 1, 2], [1, 2, 3], [2, 1, 3],])('.add(%i, %i)', (a, b, expected) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); });});test('will not be ran', () => { expect(1 / 0).toBe(Infinity);});
describe.only.eachtable(name, fn)
describe.only.each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3}`('returns $expected when $a is added $b', ({a, b, expected}) => { test('passes', () => { expect(a + b).toBe(expected); });});test('will not be ran', () => { expect(1 / 0).toBe(Infinity);});
describe.skip(name, fn)
同xdescribe(name, fn)。
如果不想运行特定的描述块,可以使用describe.skip:
describe('my beverage', () => { test('is delicious', () => { expect(myBeverage.delicious).toBeTruthy(); }); test('is not sour', () => { expect(myBeverage.sour).toBeFalsy(); });});describe.skip('my other beverage', () => { // ... will be skipped});
describe.skip 常用于代替注释掉用例,这样做更显干净。
describe.skip.each(table)(name, fn)
Also under the aliases: xdescribe.each(table)(name, fn) and xdescribe.eachtable(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)
describe.skip.each([ [1, 1, 2], [1, 2, 3], [2, 1, 3],])('.add(%i, %i)', (a, b, expected) => { test(`returns ${expected}`, () => { expect(a + b).toBe(expected); // will not be ran });});test('will be ran', () => { expect(1 / 0).toBe(Infinity);});
describe.skip.eachtable(name, fn)
describe.skip.each` a | b | expected ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3}`('returns $expected when $a is added $b', ({a, b, expected}) => { test('will not be ran', () => { expect(a + b).toBe(expected); // will not be ran });});test('will be ran', () => { expect(1 / 0).toBe(Infinity);});
test(name, fn, timeout)
还有别名:it(name, fn, timeout)
测试文件中需要的全部是运行测试的 test方法。 例如,假设有一个函数inchesOfRain()应该为零。 你的整个测试可能是:
test('did not rain', () => { expect(inchesOfRain()).toBe(0);});
第一个参数是测试名称; 第二个参数是包含测试期望的函数。 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:
test('has lemon in it', () => { return fetchBeverageList().then(list => { expect(list).toContain('lemon'); });});
即使对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
