Jest 全局API

2021-09-18 20:46 更新

在測試文件中,Jest 將這些方法和對(duì)象中的每一個(gè)都放入全局環(huán)境中。不必要求或?qū)肴魏螙|西即可使用它們。但是,如果你更喜歡顯式導(dǎo)入,則可以執(zhí)行?import {describe, expect, test} from '@jest/globals'?.

方法

參考

?afterAll(fn, timeout)?

在此文件中的所有測試完成后運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在繼續(xù)之前等待該承諾解決。

或者,可以提供?timeout?(以毫秒為單位)用于指定    在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。

這通常是有用的如果你想要清理一些在測試之間共享的全局設(shè)置狀態(tài)。

例如:

  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 ?調(diào)用后運(yùn)行所有測試。

?afterAll ?是在 ?describe ?塊的內(nèi)部,它運(yùn)行在 ?describe ?塊結(jié)尾。

如果你想要在每個(gè)測試后運(yùn)行一些清理,請(qǐng)使用 ?afterEach?。

?afterEach(fn, timeout)?

在此文件中的每個(gè)測試完成后運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在繼續(xù)之前等待該承諾解決。

或者,可以提供?timeout?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。

這通常是有用的如果你想要清理一些在每個(gè)測試之間共享的全局設(shè)置狀態(tài)。

例如:

  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 ?調(diào)用后運(yùn)行每個(gè)測試。

?afterEach ?是在 ?describe ?塊的內(nèi)部,它運(yùn)行在 ?describe ?塊結(jié)尾。

如果你想要一些清理只運(yùn)行一次,在所有測試運(yùn)行后,使用 ?afterAll ?。

?beforeAll(fn, timeout)?

在此文件中的任何測試運(yùn)行之前運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在運(yùn)行測試之前等待該承諾解決。

或者,可以提供?timeout?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。

如果要設(shè)置將被許多測試使用的一些全局狀態(tài),這通常很有用。

例如:

  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 ?確保數(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?。

?beforeEach(fn, timeout)?

在此文件中的每個(gè)測試運(yùn)行之前運(yùn)行一個(gè)函數(shù)。如果函數(shù)返回一個(gè)承諾或者是一個(gè)生成器,Jest 會(huì)在運(yùn)行測試之前等待該承諾解決。

或者,可以提供?timeout?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 5 秒。

如果要重置許多測試將使用的全局狀態(tài),這通常很有用。

例如:

  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?確保每個(gè)測試重置數(shù)據(jù)庫。

如果?beforeEach?在?describe?塊內(nèi),則它將在 ?describe ?塊中為每個(gè)測試運(yùn)行。

如果只需要運(yùn)行一些設(shè)置代碼,在任何測試運(yùn)行之前,請(qǐng)使用?beforeAll?。

?describe(name, fn)?

?describe(name, fn)? 創(chuàng)建將多個(gè)相關(guān)測試分組在一起的塊例如,如果你有一個(gè)?myBeverage?對(duì)象,該對(duì)象應(yīng)該是不錯(cuò)的,可以通過以下方式測試:

  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?直接在頂層編寫塊。但是,如果你希望將測試組織成組,這會(huì)很方便。

?describe?如果有測試層次結(jié)構(gòu),還可以嵌套塊:

  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?,如果你繼續(xù)重復(fù)使用不同的數(shù)據(jù)相同的測試套件。?describe.each?允許編寫一次測試套件并傳入數(shù)據(jù)。

?describe.each? 有兩個(gè) API 可用:

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

  • ?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 秒。

示例:

  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?

        ○ 變量名列標(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 秒。

示例:

  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)?

如果你只想運(yùn)行一個(gè)描述塊,你可以使用?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?如果你只想運(yùn)行數(shù)據(jù)驅(qū)動(dòng)測試的特定測試套件,請(qǐng)使用。

?describe.only.each? 有兩個(gè) 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)?

如果不想運(yùn)行特定的描述塊,可以使用?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?通常是臨時(shí)注釋掉一大塊測試的更干凈的替代方法。

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

同樣在別名下:?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(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運(yùn)行測試的方法。例如,假設(shè)有一個(gè)函數(shù)?inchesOfRain()?應(yīng)該為零。整個(gè)測試可能是:

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

第一個(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)行測試:

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

即使對(duì)?test?的調(diào)用將立即返回,測試還沒有完成,直到承諾也解決。

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

同樣在別名下:? 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 秒。

  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 同時(shí)執(zhí)行超過指定數(shù)量的測試

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

同樣在別名下:? 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 可用:

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

  • ?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 秒。

示例:

  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?

    ○  變量名列標(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 秒。

示例:

  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?,如果你只想用不同的測試數(shù)據(jù)上運(yùn)行特定的測試同時(shí)進(jìn)行。

?test.concurrent.only.each? 有兩個(gè) 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?如果要停止運(yùn)行異步數(shù)據(jù)驅(qū)動(dòng)的測試集合,請(qǐng)使用。

?test.concurrent.skip.each? 有兩個(gè) 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?,如果你繼續(xù)重復(fù)使用不同的數(shù)據(jù)相同的測試。?test.each?允許您編寫一次測試并傳入數(shù)據(jù)。

?test.each? 有兩個(gè) API 可用:

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

  • ?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 秒。

示例:

  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?

    ○ 變量名列標(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 秒。

示例:

  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)?

當(dāng)調(diào)試大型測試文件時(shí),通常只想運(yùn)行測試的子集??梢允褂?.only?指定哪些測試是您要在該測試文件中運(yùn)行的唯一測試。

或者,可以提供?timeout?(以毫秒為單位)用于指定在中止之前等待的時(shí)間。注意:默認(rèn)超時(shí)為 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”測試會(huì)在該測試文件中運(yùn)行,因?yàn)樗褂?test.only?.

通常,不會(huì)使用?test.only?源代碼管理檢查代碼——將使用它進(jìn)行調(diào)試,并在修復(fù)損壞的測試后將其刪除。

?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?,如果你想只運(yùn)行不同的測試數(shù)據(jù)的特定測試。

?test.only.each? 有兩個(gè) 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)?

當(dāng)你維護(hù)一個(gè)很大的代碼庫時(shí),有時(shí)可能會(huì)發(fā)現(xiàn)一個(gè)由于某種原因被暫時(shí)破壞的測試。 如果要跳過運(yùn)行此測試,但不想刪除此代碼,可以使用?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”測試才會(huì)運(yùn)行,因?yàn)榱硪粋€(gè)測試是使用?test.skip?。

可以將測試注釋掉,但使用起來通常會(huì)更好一些,?test.skip?因?yàn)樗鼤?huì)保持縮進(jìn)和語法突出顯示。

?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?如果要停止運(yùn)行數(shù)據(jù)驅(qū)動(dòng)的測試集合,請(qǐng)使用。

?test.skip.each? 有兩個(gè) 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?當(dāng)你在編寫測試計(jì)劃。這些測試將在最后的摘要輸出中突出顯示,以便您知道還需要執(zhí)行多少測試。

注意:如果你提供測試回調(diào)函數(shù),?test.todo?則將引發(fā)錯(cuò)誤。如果已經(jīng)實(shí)施了測試并且它已損壞并且你不希望它運(yùn)行,那么請(qǐng)?test.skip?改用。

API

  • ?name?:?String?測試計(jì)劃的標(biāo)題。

示例:

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)