在測試文件中,Jest 將這些方法和對(duì)象中的每一個(gè)都放入全局環(huán)境中。不必要求或?qū)肴魏螙|西即可使用它們。但是,如果你更喜歡顯式導(dǎo)入,則可以執(zhí)行?import {describe, expect, test} from '@jest/globals'
?.
在此文件中的所有測試完成后運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在繼續(xù)之前等待該承諾解決。
或者,可以提供?timeout
?(以毫秒為單位)用于指定 在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
這通常是有用的如果你想要清理一些在測試之間共享的全局設(shè)置狀態(tài)。
例如:
const globalDatabase = makeGlobalDatabase();
function cleanUpDatabase(db) {
db.cleanUp();
}
afterAll(() => {
cleanUpDatabase(globalDatabase);
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
這里 ?afterAll
?確保那 ?cleanUpDatabase
?調(diào)用后運(yùn)行所有測試。
?afterAll
?是在 ?describe
?塊的內(nèi)部,它運(yùn)行在 ?describe
?塊結(jié)尾。
如果你想要在每個(gè)測試后運(yùn)行一些清理,請(qǐng)使用 ?afterEach
?。
在此文件中的每個(gè)測試完成后運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在繼續(xù)之前等待該承諾解決。
或者,可以提供?timeout
?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
這通常是有用的如果你想要清理一些在每個(gè)測試之間共享的全局設(shè)置狀態(tài)。
例如:
const globalDatabase = makeGlobalDatabase();
function cleanUpDatabase(db) {
db.cleanUp();
}
afterEach(() => {
cleanUpDatabase(globalDatabase);
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
這里 ?afterEach
?確保那 ?cleanUpDatabase
?調(diào)用后運(yùn)行每個(gè)測試。
?afterEach
?是在 ?describe
?塊的內(nèi)部,它運(yùn)行在 ?describe
?塊結(jié)尾。
如果你想要一些清理只運(yùn)行一次,在所有測試運(yùn)行后,使用 ?afterAll
?。
在此文件中的任何測試運(yùn)行之前運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在運(yùn)行測試之前等待該承諾解決。
或者,可以提供?timeout
?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
如果要設(shè)置將被許多測試使用的一些全局狀態(tài),這通常很有用。
例如:
const globalDatabase = makeGlobalDatabase();
beforeAll(() => {
// Clears the database and adds some testing data.
// Jest will wait for this promise to resolve before running tests.
return globalDatabase.clear().then(() => {
return globalDatabase.insert({testData: 'foo'});
});
});
// Since we only set up the database once in this example, it's important
// that our tests don't modify it.
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
在這里 ?beforeAll
?確保數(shù)據(jù)庫設(shè)置運(yùn)行測試之前。如果安裝程序是同步的,那么你可以不必事先進(jìn)行設(shè)置。 關(guān)鍵是Jest將等待承諾解決,所以你也可以進(jìn)行異步設(shè)置。
如果?beforeAll
?在?describe
?塊內(nèi),則它將在 ?describe
?塊的開頭運(yùn)行。
如果要在每次測試之前運(yùn)行某些操作,而不是在任何測試運(yùn)行之前運(yùn)行,請(qǐng)改用?beforeEach
?。
在此文件中的每個(gè)測試運(yùn)行之前運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在運(yùn)行測試之前等待該承諾解決。
或者,可以提供?timeout
?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
如果要重置許多測試將使用的全局狀態(tài),這通常很有用。
例如:
const globalDatabase = makeGlobalDatabase();
beforeEach(() => {
// Clears the database and adds some testing data.
// Jest will wait for this promise to resolve before running tests.
return globalDatabase.clear().then(() => {
return globalDatabase.insert({testData: 'foo'});
});
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
這里,?beforeEach
?確保每個(gè)測試重置數(shù)據(jù)庫。
如果?beforeEach
?在?describe
?塊內(nèi),則它將在 ?describe
?塊中為每個(gè)測試運(yùn)行。
如果只需要運(yùn)行一些設(shè)置代碼,在任何測試運(yùn)行之前,請(qǐng)使用?beforeAll
?。
?describe(name, fn)
? 創(chuàng)建將多個(gè)相關(guān)測試分組在一起的塊例如,如果你有一個(gè)?myBeverage
?對(duì)象,該對(duì)象應(yīng)該是不錯(cuò)的,可以通過以下方式測試:
const myBeverage = {
delicious: true,
sour: false,
};
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
這不是必需的 - 可以?test
?直接在頂層編寫塊。但是,如果你希望將測試組織成組,這會(huì)很方便。
?describe
?如果有測試層次結(jié)構(gòu),還可以嵌套塊:
const binaryStringToNumber = binString => {
if (!/^[01]+$/.test(binString)) {
throw new CustomError('Not a binary number.');
}
return parseInt(binString, 2);
};
describe('binaryStringToNumber', () => {
describe('given an invalid binary string', () => {
test('composed of non-numbers throws CustomError', () => {
expect(() => binaryStringToNumber('abc')).toThrowError(CustomError);
});
test('with extra whitespace throws CustomError', () => {
expect(() => binaryStringToNumber(' 100')).toThrowError(CustomError);
});
});
describe('given a valid binary string', () => {
test('returns the correct number', () => {
expect(binaryStringToNumber('100')).toBe(4);
});
});
});
使用?describe.each
?,如果你繼續(xù)重復(fù)使用不同的數(shù)據(jù)相同的測試套件。?describe.each
?允許編寫一次測試套件并傳入數(shù)據(jù)。
?describe.each
? 有兩個(gè) API 可用:
table
?: ?Arrayof Arrays
? 的參數(shù)被傳遞到fn每一行。○ 注意如果你傳入一個(gè)一維基元數(shù)組,它在內(nèi)部將被映射到一個(gè)表,即?[1, 2, 3] -> [[1], [2], [3]]
name
?:?String
?測試套件的標(biāo)題。 ○ 通過位置注入帶有printf格式的參數(shù)來生成唯一的測試標(biāo)題:
■ ?%p
?- pertty-format。
■ ?%s
?- String。
■ ?%d
?- Number。
■ ?%i
? - Integer。
■ ?%f
? - Float。
■ ?%j
? - JSON。
■ ?%o
? - Object。
■ ?%#
? - 測試用例的索引。
■ ?%%
?- 單個(gè)百分號(hào) ('%')。這不消耗參數(shù)。
fn
?:?Function
?要運(yùn)行的測試套件,這是將每行中的參數(shù)作為函數(shù)參數(shù)接收的函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
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);
});
});
table
?: ?Tagged Template Literal
? ○ 變量名列標(biāo)題的第一行用 ?|
?
○ 使用?${value}
?語法作為模板文字表達(dá)式提供的一個(gè)或多個(gè)后續(xù)數(shù)據(jù)行。
name
?:?String
?測試套件的標(biāo)題,用于$variable將測試數(shù)據(jù)從標(biāo)記的模板表達(dá)式中注入套件標(biāo)題。 ○ 要注入嵌套對(duì)象值,可以提供一個(gè) ????keyPath??
?即? $variable.path.to.value
?
fn
?:?Function
?要運(yùn)行的測試套件,這是將接收測試數(shù)據(jù)對(duì)象的函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
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);
});
});
還有別名:?fdescribe(name, fn)
?
如果你只想運(yùn)行一個(gè)描述塊,你可以使用?describe.only
?:
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
});
同樣在別名下:?fdescribe.each(table)(name, fn)
?和?fdescribe.each`table`(name, fn)
?
?describe.only.each
?如果你只想運(yùn)行數(shù)據(jù)驅(qū)動(dòng)測試的特定測試套件,請(qǐng)使用。
?describe.only.each
? 有兩個(gè) API 可用:
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.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);
});
還有別名:?xdescribe(name, fn)
?
如果不想運(yùn)行特定的描述塊,可以使用?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
?通常是臨時(shí)注釋掉一大塊測試的更干凈的替代方法。
同樣在別名下:?xdescribe.each(table)(name, fn)
?和?xdescribe.each`table`(name, fn)
?
使用?describe.skip.each
?,如果你想停止運(yùn)行了一套驅(qū)動(dòng)測試數(shù)據(jù)。
?describe.skip.each
? 有兩個(gè) API 可用:
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.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);
});
還有別名:?it(name, fn, timeout)
?
在測試文件中你需要的只是test運(yùn)行測試的方法。例如,假設(shè)有一個(gè)函數(shù)?inchesOfRain()
?應(yīng)該為零。整個(gè)測試可能是:
test('did not rain', () => {
expect(inchesOfRain()).toBe(0);
});
第一個(gè)參數(shù)是測試名稱; 第二個(gè)參數(shù)是包含測試期望的函數(shù)。第三個(gè)參數(shù)(可選)是?timeout
?(以毫秒為單位),用于指定中止前的等待時(shí)間。注意:默認(rèn)超時(shí)為5秒。
注意:如果從返回承諾?test
?,Jest 將等待承諾解決,然后讓測試完成。如果你向通常調(diào)用的測試函數(shù)提供參數(shù), Jest 也會(huì)等待?done
?。當(dāng)你想測試回調(diào)時(shí),這可能會(huì)很方便。在此處查看如何測試異步代碼。
例如,假設(shè)?fetchBeverageList()
?返回一個(gè)應(yīng)該解析為其中包含的列表的承諾?lemon
???梢允褂靡韵路椒ㄟM(jìn)行測試:
test('has lemon in it', () => {
return fetchBeverageList().then(list => {
expect(list).toContain('lemon');
});
});
即使對(duì)?test
?的調(diào)用將立即返回,測試還沒有完成,直到承諾也解決。
同樣在別名下:? it.concurrent(name, fn, timeout)
?
?如果希望測試同時(shí)運(yùn)行,請(qǐng)使用?test.concurrent
。
注意:?test.concurrent
?被認(rèn)為是實(shí)驗(yàn)性的 - 請(qǐng)參閱 [此處]) https://github.com/facebook/jest/labels/Area%3A%20Concurrent ) 了解有關(guān)缺失功能和其他問題的詳細(xì)信息
第一個(gè)參數(shù)是測試名稱;第二個(gè)參數(shù)是一個(gè)包含要測試的期望的異步函數(shù)。第三個(gè)參數(shù)(可選)是?timeout
?(以毫秒為單位)用于指定在中止之前等待多長時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
test.concurrent('addition of 2 numbers', async () => {
expect(5 + 3).toBe(8);
});
test.concurrent('subtraction 2 numbers', async () => {
expect(5 - 3).toBe(2);
});
注意:?maxConcurrency
?在配置中使用以防止 Jest 同時(shí)執(zhí)行超過指定數(shù)量的測試
同樣在別名下:? it.concurrent.each(table)(name, fn, timeout)
?
使用?test.concurrent.each
?,如果你繼續(xù)重復(fù)使用不同的數(shù)據(jù)相同的測試。?test.each
?允許編寫一次測試并傳入數(shù)據(jù),測試都是異步運(yùn)行的。
?test.concurrent.each
? 有兩個(gè) API 可用:
table
?: ?Arrayof
?帶有傳遞到fn每一行測試中的參數(shù)的數(shù)組。 ○ 注意如果你傳入一個(gè)一維基元數(shù)組,它在內(nèi)部將被映射到一個(gè)表,即?[1, 2, 3] -> [[1], [2], [3]]
?
name
?:?String
?測試塊的標(biāo)題。○ 通過位置注入帶有printf格式的參數(shù)來生成唯一的測試標(biāo)題:
■ ? %p
?- Pretty-format。
■ ? %s
?- String。
■ ? %d
?- Number。
■ ? %i
? - Integer。
■ ? %f
? - Float。
■ ?%j
? - JSON。
■ ? %o
? - Object。
■ ? %#
? - 測試用例的索引。
■ ? %%
?- 單個(gè)百分號(hào) ('%')。這不消耗參數(shù)。
fn
?:?Function
?要運(yùn)行的測試,這是將接收每一行中的參數(shù)作為函數(shù)參數(shù)的函數(shù),這必須是一個(gè)異步函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
test.concurrent.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected);
});
table
?: ?Tagged Template Literal
? ○ 變量名列標(biāo)題的第一行用 ?|
?
○ 使用?${value}
?語法作為模板文字表達(dá)式提供的一個(gè)或多個(gè)后續(xù)數(shù)據(jù)行。
name
?:?String
?測試的標(biāo)題,用于?$variable
?將測試數(shù)據(jù)從標(biāo)記的模板表達(dá)式中注入到測試標(biāo)題中。 ○ 要注入嵌套對(duì)象值,可以提供一個(gè) keyPath 即? $variable.path.to.value
?
fn
?:?Function
?要運(yùn)行的測試,這是將接收測試數(shù)據(jù)對(duì)象的函數(shù),這必須是一個(gè)異步函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
test.concurrent.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
同樣在別名下:? it.concurrent.only.each(table)(name, fn)
?
使用?test.concurrent.only.each
?,如果你只想用不同的測試數(shù)據(jù)上運(yùn)行特定的測試同時(shí)進(jìn)行。
?test.concurrent.only.each
? 有兩個(gè) API 可用:
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.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);
});
同樣在別名下: ?it.concurrent.skip.each(table)(name, fn)
?
?test.concurrent.skip.each
?如果要停止運(yùn)行異步數(shù)據(jù)驅(qū)動(dòng)的測試集合,請(qǐng)使用。
?test.concurrent.skip.each
? 有兩個(gè) API 可用:
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.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);
});
同樣在別名下:?it.each(table)(name, fn)
?和?it.each`table`(name, fn)
?
使用?test.each
?,如果你繼續(xù)重復(fù)使用不同的數(shù)據(jù)相同的測試。?test.each
?允許您編寫一次測試并傳入數(shù)據(jù)。
?test.each
? 有兩個(gè) API 可用:
table
?: ?Arrayof
?帶有傳遞到?fn
?每一行測試中的參數(shù)的數(shù)組。 ○ 注意如果你傳入一個(gè)一維基元數(shù)組,它在內(nèi)部將被映射到一個(gè)表,即?[1, 2, 3] -> [[1], [2], [3]]
?
name
?:?String
?測試塊的標(biāo)題。○ 通過位置注入帶有printf格式的參數(shù)來生成唯一的測試標(biāo)題:
■ ?%p
?- Pretty-format。
■ ?%s
?- String。
■ ?%d
?- Number。
■ ?%i
? - Integer。
■ %f
- Float。
■ ?%j
? - JSON。
■ ?%o
? - Object。
■ ?%#
? - 測試用例的索引。
■ ?%%
?- 單個(gè)百分號(hào) ('%')。這不消耗參數(shù)。
fn
?:?Function
?要運(yùn)行的測試,這是將接收每一行中的參數(shù)作為函數(shù)參數(shù)的函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected);
});
table
?: ?Tagged Template Literal
? ○ 變量名列標(biāo)題的第一行用 ?|
?
○ 使用?${value}
?語法作為模板文字表達(dá)式提供的一個(gè)或多個(gè)后續(xù)數(shù)據(jù)行。
name
?:?String
?測試的標(biāo)題,用于?$variable
?將測試數(shù)據(jù)從標(biāo)記的模板表達(dá)式中注入到測試標(biāo)題中。 ○ 要注入嵌套對(duì)象值,您可以提供一個(gè) keyPath 即 ?$variable.path.to.value
?
fn
?:?Function
?要運(yùn)行的測試,這是將接收測試數(shù)據(jù)對(duì)象的函數(shù)。timeout
?(以毫秒為單位)用于指定在中止之前等待每一行的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。示例:
test.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
同樣在別名下:i?t.only(name, fn, timeout)
?, 和?fit(name, fn, timeout)
?
當(dāng)調(diào)試大型測試文件時(shí),通常只想運(yùn)行測試的子集??梢允褂?.only
?指定哪些測試是您要在該測試文件中運(yùn)行的唯一測試。
或者,可以提供?timeout
?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。
比如說,你有這些測試︰
test.only('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
只有“it is raining”測試會(huì)在該測試文件中運(yùn)行,因?yàn)樗褂?test.only
?.
通常,不會(huì)使用?test.only
?源代碼管理檢查代碼——將使用它進(jìn)行調(diào)試,并在修復(fù)損壞的測試后將其刪除。
同樣在別名下:?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
?,如果你想只運(yùn)行不同的測試數(shù)據(jù)的特定測試。
?test.only.each
? 有兩個(gè) API 可用:
test.only.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected);
});
test('will not be ran', () => {
expect(1 / 0).toBe(Infinity);
});
test.only.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
test('will not be ran', () => {
expect(1 / 0).toBe(Infinity);
});
同樣在別名下:?it.skip(name, fn), xit(name, fn)
?, 和?xtest(name, fn)
?
當(dāng)你維護(hù)一個(gè)很大的代碼庫時(shí),有時(shí)可能會(huì)發(fā)現(xiàn)一個(gè)由于某種原因被暫時(shí)破壞的測試。 如果要跳過運(yùn)行此測試,但不想刪除此代碼,可以使用?test.skip
?指定要跳過的某些測試。
比如說,你有這些測試︰
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
只有“it is raining”測試才會(huì)運(yùn)行,因?yàn)榱硪粋€(gè)測試是使用?test.skip
?。
可以將測試注釋掉,但使用起來通常會(huì)更好一些,?test.skip
?因?yàn)樗鼤?huì)保持縮進(jìn)和語法突出顯示。
同樣在別名下:?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
?如果要停止運(yùn)行數(shù)據(jù)驅(qū)動(dòng)的測試集合,請(qǐng)使用。
?test.skip.each
? 有兩個(gè) API 可用:
test.skip.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected); // will not be ran
});
test('will be ran', () => {
expect(1 / 0).toBe(Infinity);
});
test.skip.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected); // will not be ran
});
test('will be ran', () => {
expect(1 / 0).toBe(Infinity);
});
同樣在別名下: ?it.todo(name)
?
使用?test.todo
?當(dāng)你在編寫測試計(jì)劃。這些測試將在最后的摘要輸出中突出顯示,以便您知道還需要執(zhí)行多少測試。
注意:如果你提供測試回調(diào)函數(shù),?test.todo
?則將引發(fā)錯(cuò)誤。如果已經(jīng)實(shí)施了測試并且它已損壞并且你不希望它運(yùn)行,那么請(qǐng)?test.skip
?改用。
name
?:?String
?測試計(jì)劃的標(biāo)題。示例:
const add = (a, b) => a + b;
test.todo('add should be associative');
更多建議: