成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

前端自動化測試:Jest 測試框架應用

開發 前端 自動化
作為一個面向前端的測試框架, Jest 可以利用其特有的快照測試功能,通過比對UI 代碼生成的快照文件,實現對 React 等常見前端框架的自動測試。

[[407449]]

Jest : https://jestjs.io/zh-Hans/是 Facebook 出品的一個 JavaScript 開源測試框架。

相對其他測試框架,其一大特點就是就是內置了常用的測試工具,比如零配置、自帶斷言、測試覆蓋率工具等功能,實現了開箱即用。

Jest 適用但不局限于使用以下技術的項目:Babel、TypeScript、 Node、 React、Angular、Vue 等。

Jest 主要特點:

  • 零配置
  • 自帶斷言
  • 作為一個面向前端的測試框架, Jest 可以利用其特有的快照測試功能,通過比對UI 代碼生成的快照文件,實現對 React 等常見前端框架的自動測試。
  • Jest 的測試用例是并行執行的,而且只執行發生改變的文件所對應的測試,提升了測試速度。
  • 測試覆蓋率
  • Mock 模擬

快速體驗 Jest

安裝 Jest 到項目中:

  1. npm install --save-dev jest 
  1. //math.js 
  2. functionadd (a, b) { 
  3.   return a * b 
  4.  
  5. functionsubtract (x, y) { 
  6.   return x - y 
  7.  
  8. module.exports= { 
  9.   add
  10.   subtract 
  1. //test.js ==> math.test.js 
  2. const { add, subtract } =require('./math'
  3.  
  4. test('測試加法', () => { 
  5.   expect(add(1,2)).toBe(3) 
  6. }) 
  7.  
  8. test('測試減法', () => { 
  9.   expect(subtract(2,1)).toBe(1) 
  10. }) 
  1. //package.json 
  2.   "scripts":{ 
  3.     "test":"jest" 
  4.   } 

jest 命令會運行項目中所有以 .test.js 結尾的文件

最后運行測試命令:

  1. npm run test 

解析:

  • jest 找到項目中所有以 .test.js 結尾的文件并運行
  • jest 會給測試文件提供 test、expect 等全局函數,所以在測試文件中可以直接使用
  • jest 為測試結果提供了良好的日志輸出

解決 vscode 中 jest 代碼提示問題

  1. npm i -D @types/jest 

注意:@types/jest 必須安裝到項目的根目錄,并且以根目錄的方式在 vscode 中打開,否則不生效?;蛘哒f只要是 vscode 打開的項目根目錄有 @types/jest 這個包就可以了。

配置文件

  1. npx jest--init 

配置文件生成選項:

  • 是否使用 ts ;
  • 使用哪種測試環境;
  • 使用 jest 收集測試覆蓋率報告;
  • 使用那種引擎檢測覆蓋率:v8 處于實驗性階段,建議 Node v14 后使用,babel 較為成熟
  • 每次測試時,是否自動清除 mock 實例;

詳細配置信息參考:https://jestjs.io/docs/zh-Hans/configuration。

  1. //jest.config.js 
  2. /* 
  3.  * For a detailedexplanation regarding each configuration property, visit: 
  4.  *https://jestjs.io/docs/en/configuration.html 
  5.  */ 
  6.  
  7. module.exports= { 
  8.   // 自動 mock 所有導入的外部模塊 
  9.   // automock: false
  10.  
  11.   // 在指定次數失敗后停止運行測試 
  12.   // bail: 0, 
  13.  
  14.   // The directory where Jest should store its cached dependencyinformation 
  15.   // cacheDirectory:"/private/var/folders/5h/_98rffpj1z95b_0dm76lvzm40000gn/T/jest_dx"
  16.  
  17.   // 在每個測試之間自動清除 mock 調用和實例 
  18.   clearMocks:true
  19.  
  20.   // 是否收集測試覆蓋率信息 
  21.   // collectCoverage: false
  22.  
  23.   // 一個 glob 模式數組,指示應該為其收集覆蓋率信息的一組文件 
  24.   // collectCoverageFrom: undefined, 
  25.  
  26.   // 測試覆蓋率報錯文件輸出的目錄 
  27.   coverageDirectory:"coverage"
  28.  
  29.   // 忽略測試覆蓋率統計 
  30.   // coveragePathIgnorePatterns: [ 
  31.   //  "/node_modules/" 
  32.   // ], 
  33.  
  34.   // 指示應該使用哪個提供程序檢測代碼的覆蓋率,默認是 babel,可選 v8,但是 v8 不太穩定,建議Node 14 以上版本使用 
  35.   // coverageProvider: "babel"
  36.  
  37.   // A list of reporter names that Jest uses when writingcoverage reports 
  38.   // coverageReporters: [ 
  39.   //   "json"
  40.   //   "text"
  41.   //   "lcov"
  42.   //   "clover" 
  43.   // ], 
  44.  
  45.   // An object that configures minimum threshold enforcement forcoverage results 
  46.   // coverageThreshold: undefined, 
  47.  
  48.   // A path to a custom dependency extractor 
  49.   // dependencyExtractor: undefined, 
  50.  
  51.   // Make calling deprecated APIs throw helpful error messages 
  52.   // errorOnDeprecated: false
  53.  
  54.   // Force coverage collection from ignored files using an arrayof glob patterns 
  55.   // forceCoverageMatch: [], 
  56.  
  57.   // A path to a module which exports an async function that istriggered once before all test suites 
  58.   // globalSetup: undefined, 
  59.  
  60.   // A path to a module which exports an async function that istriggered once after all test suites 
  61.   // globalTeardown: undefined, 
  62.  
  63.   // A set of global variables that need to be available in alltest environments 
  64.   // globals: {}, 
  65.  
  66.   // The maximum amount of workers used to run your tests. Canbe specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPUamount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2workers. 
  67.   // maxWorkers: "50%"
  68.  
  69.   // An array of directory names to be searched recursively upfrom the requiring module's location 
  70.   // moduleDirectories: [ 
  71.   //  "node_modules" 
  72.   // ], 
  73.  
  74.   // An array of file extensions your modules use 
  75.   // moduleFileExtensions: [ 
  76.   //   "js"
  77.   //   "json"
  78.   //   "jsx"
  79.   //   "ts"
  80.   //   "tsx"
  81.   //   "node" 
  82.   // ], 
  83.  
  84.   // A map from regular expressions to module names or to arraysof module names that allow to stub out resources with a single module 
  85.   // moduleNameMapper: {}, 
  86.  
  87.   // An array of regexp pattern strings, matched against allmodule paths before considered 'visible' to the module loader 
  88.   // modulePathIgnorePatterns: [], 
  89.  
  90.   // Activates notifications for test results 
  91.   // notify: false
  92.  
  93.   // An enum that specifies notification mode. Requires {notify: true } 
  94.   // notifyMode: "failure-change"
  95.  
  96.   // A preset that is used as a base for Jest's configuration 
  97.   // preset: undefined, 
  98.  
  99.   // Run tests from one or more projects 
  100.   // projects: undefined, 
  101.  
  102.   // Use this configuration option to add custom reporters toJest 
  103.   // reporters: undefined, 
  104.  
  105.   // Automatically reset mock state between every test 
  106.   // resetMocks: false
  107.  
  108.   // Reset the module registry before running each individualtest 
  109.   // resetModules: false
  110.  
  111.   // A path to a custom resolver 
  112.   // resolver: undefined, 
  113.  
  114.   // Automatically restore mock state between every test 
  115.   // restoreMocks: false
  116.  
  117.   // The root directory that Jest should scan for tests andmodules within 
  118.   // rootDir: undefined, 
  119.  
  120.   // A list of paths to directories that Jest should use tosearch for files in 
  121.   // roots: [ 
  122.   //  "<rootDir>" 
  123.   // ], 
  124.  
  125.   // Allows you to use a custom runner instead of Jest's defaulttest runner 
  126.   // runner: "jest-runner"
  127.  
  128.   // The paths to modules that run some code to configure or setup the testing environment before each test 
  129.   // setupFiles: [], 
  130.  
  131.   // A list of paths to modules that run some code to configureor set up the testing framework before each test 
  132.   // setupFilesAfterEnv: [], 
  133.  
  134.   // The number of seconds after which a test is considered asslow and reported as such in the results. 
  135.   // slowTestThreshold: 5, 
  136.  
  137.   // A list of paths to snapshot serializer modules Jest shoulduse for snapshot testing 
  138.   // snapshotSerializers: [], 
  139.  
  140.   // The test environment that will be used for testing 
  141.   // testEnvironment: "jest-environment-jsdom"
  142.  
  143.   // Options that will be passed to the testEnvironment 
  144.   // testEnvironmentOptions: {}, 
  145.  
  146.   // Adds a location field to test results 
  147.   // testLocationInResults: false
  148.  
  149.   // The glob patterns Jest uses to detect test files 
  150.   // testMatch: [ 
  151.   //  "**/__tests__/**/*.[jt]s?(x)"
  152.   //  "**/?(*.)+(spec|test).[tj]s?(x)" 
  153.   // ], 
  154.  
  155.   // An array of regexp pattern strings that are matched againstall test paths, matched tests are skipped 
  156.   // testPathIgnorePatterns: [ 
  157.   //  "/node_modules/" 
  158.   // ], 
  159.  
  160.   // The regexp pattern or array of patterns that Jest uses todetect test files 
  161.   // testRegex: [], 
  162.  
  163.   // This option allows the use of a custom results processor 
  164.   // testResultsProcessor: undefined, 
  165.  
  166.   // This option allows use of a custom test runner 
  167.   // testRunner: "jasmine2"
  168.  
  169.   // This option sets the URL for the jsdom environment. It isreflected in properties such as location.href 
  170.   // testURL: "http://localhost"
  171.  
  172.   // Setting this value to "fake" allows the use offake timers for functions such as "setTimeout" 
  173.   // timers: "real"
  174.  
  175.   // A map from regular expressions to paths to transformers 
  176.   // transform: undefined, 
  177.  
  178.   // An array of regexp pattern strings that are matched againstall source file paths, matched files will skip transformation 
  179.   // transformIgnorePatterns: [ 
  180.   //  "/node_modules/"
  181.   //  "\\.pnp\\.[^\\/]+$" 
  182.   // ], 
  183.  
  184.   // An array of regexp pattern strings that are matched againstall modules before the module loader will automatically return a mock for them 
  185.   // unmockedModulePathPatterns: undefined, 
  186.  
  187.   // Indicates whether each individual test should be reportedduring the run 
  188.   // verbose: undefined, 
  189.  
  190.   // An array of regexp patterns that are matched against allsource file paths before re-running tests in watch mode 
  191.   // watchPathIgnorePatterns: [], 
  192.  
  193.   // Whether to use watchman for file crawling 
  194.   // watchman: true
  195. }; 

Jest CLI Options

參考:https://jestjs.io/zh-Hans/docs/cli

指定測試文件運行

  1. "scripts":{ 
  2.    "test":"jest ./math.test.js" 
  3.  }, 

Jest 監視模式

--watchAll 選項:監視文件的更改并在任何更改時重新運行所有測試。

  1. "scripts": { 
  2.   "test""jest --watchAll" 
  3.  }, 

Jest API

在測試文件中,Jest 將所有這些方法和對象放入全局環境中。無需要求或導入任何內容即可使用它們。但是,如果喜歡顯式導入,則可以:

  1. import { describe, expect, test } from'@jest/globals' 

Test 函數

test 函數別名:it(name, fn, timeout)。

  • test(name,fn, timeout)
  • test.concurrent(name, fn, timeout)
  • test.concurrent.each(table)(name, fn, timeout)
  • test.concurrent.only.each(table)(name, fn)
  • test.concurrent.skip.each(table)(name, fn)
  • test.each(table)(name, fn, timeout)
  • test.only(name, fn, timeout)

只運行當前測試用例

  • test.only.each(table)(name, fn)
  • test.skip(name,fn)
  • test.skip.each(table)(name, fn)
  • test.todo(name)

創建 global-api.test.js 測試文件,注意,測試文件中必須有一個測試用例,如果沒有則直接報錯。

  1. test('should ', () => { 
  2.   console.log('test--api'
  3. }) 
  1. test('should ', () => { 
  2.   console.log('test--api'
  3. }) 
  4. test('should1 ', () => { 
  5.   console.log('test--api1'
  6. }) 
  7.  
  8. // 上面兩個不運行 
  9. test.only('should2 ', () => { 
  10.   console.log('test--api2'
  11. }) 

Expect 匹配器

在編寫測試時,通常需要檢查值是否滿足某些條件。Expect 讓我們可以訪問許多“匹配器”,以驗證不同的內容。

  1. test('two plus two is four', () => { 
  2.   expect(2+2).toBe(6) 
  3.   expect({ name:'jack' }).toEqual({ name:'jack' }) 
  4.   expect('Christoph').toMatch(/stop/) 
  5.   expect(4).toBeGreaterThan(3) 
  6.   expect(4).toBeLessThan(5) 
  7. }) 

完整的匹配器列表查看:https://jestjs.io/zh-Hans/docs/expect

describe 函數

describe 創建一個將幾個相關測試組合在一起的塊。

  1. const myBeverage = { 
  2.   delicious:true
  3.   sour:false
  4. }; 
  5.  
  6. describe('my beverage', () => { 
  7.   test('is delicious', () => { 
  8.     expect(myBeverage.delicious).toBeTruthy(); 
  9.   }); 
  10.  
  11.   test('is not sour', () => { 
  12.     expect(myBeverage.sour).toBeFalsy(); 
  13.   }); 
  14. }); 

分組最直觀的就是在測試提示中,有更加友好的信息輸出。

  • describe(name,fn)
  • describe.each(table)(name, fn, timeout)
  • describe.only(name,fn)
  • describe.only.each(table)(name, fn)
  • describe.skip(name,fn)
  • describe.skip.each(table)(name, fn)

 

責任編輯:姜華 來源: 勾勾的前端世界
相關推薦

2023-05-18 14:01:00

前端自動化測試

2021-06-30 19:48:21

前端自動化測試Vue 應用

2021-06-25 10:57:30

前端自動化測試開發

2009-08-19 09:00:48

單元測試框架自動化測試

2017-12-24 21:00:10

自動化測試測試框架敏捷

2022-09-14 10:00:12

前端自動化測試

2011-04-18 12:52:37

自動化測試功能測試軟件測試

2016-09-26 16:42:19

JavaScript前端單元測試

2020-04-28 09:00:00

測試測試自動化

2023-10-12 07:40:54

Minium自動化框架

2020-09-14 07:00:00

測試自動化框架

2011-06-03 17:06:09

自動化測試

2019-04-18 09:00:00

Java自動化測試框架

2011-12-23 17:09:57

自動化測試

2023-11-08 13:18:00

JestJavaScript框架

2012-12-24 22:54:31

2014-04-16 14:15:01

QCon2014

2010-09-08 15:25:09

自動化測試技術網站鏈接測試

2011-08-16 15:36:47

iPhone應用測試

2021-07-02 17:22:50

前端TDDBDD
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美福利 | 成人中文网| 日韩久久久久久久 | 欧美日韩一区精品 | 欧美国产视频 | 最新免费av网站 | 精品久久久久久亚洲精品 | 色偷偷888欧美精品久久久 | 国产一区二区三区在线视频 | 国产欧美精品 | 亚洲视频在线免费 | 国产欧美在线观看 | 亚洲精品免费在线观看 | 成人精品一区二区三区四区 | 国产精品久久久久久久久污网站 | 亚洲男女激情 | 亚洲免费在线播放 | 日本午夜在线视频 | 日韩在线日韩 | 国产精品久久久久无码av | 精品不卡| 午夜免费观看网站 | 少妇午夜一级艳片欧美精品 | 欧美久久久久久 | 欧美一区二区在线看 | 羞羞视频免费观 | 美女天天干| 国产天堂| 91一区二区三区在线观看 | 日本精品视频 | 91在线一区| 欧美黑人狂野猛交老妇 | 国产a区| 啪一啪 | 欧美一区二区三区久久精品视 | 久久久久无码国产精品一区 | h视频在线播放 | 亚洲精品久久久久久国产精华液 | 黄色片网站在线观看 | 欧美一区二区在线播放 | 一道本一区二区 |