[{"data":1,"prerenderedAt":1718},["ShallowReactive",2],{"series-\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fautomatic-component-testing-with-vitest-and-testing-library":3,"series-context-\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library":1492},{"id":4,"title":5,"body":6,"contentId":1479,"date":1480,"description":1481,"extension":1482,"github":1483,"meetTheInstructor":1483,"meta":1484,"navigation":169,"part":189,"path":1485,"rawbody":1486,"robots":1483,"schemaOrg":1483,"seo":1487,"seriesSlug":1488,"sitemap":1489,"stem":1490,"subtitle":1483,"topics":1483,"__hash__":1491},"series\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fautomatic-component-testing-with-vitest-and-testing-library.md","Automatic Component Testing with Vitest and Testing Library",{"type":7,"value":8,"toc":1473},"minimark",[9,39,56,62,67,80,116,127,134,259,270,280,333,336,340,343,358,397,409,485,490,600,604,611,624,1125,1147,1160,1163,1173,1179,1186,1189,1204,1207,1412,1416,1422,1455,1469],[10,11,12,16,17,20,21,30,31,38],"p",{},[13,14,15],"strong",{},"Testing"," is an essential part of software development. It ensures our components work as expected and helps us catch bugs early in the development process. In this article, we'll explore how to automatically test ",[13,18,19],{},"Vue.js"," components using ",[22,23,27],"a",{"href":24,"rel":25},"https:\u002F\u002Fvitest.dev",[26],"nofollow",[13,28,29],{},"Vitest"," and ",[22,32,35],{"href":33,"rel":34},"https:\u002F\u002Ftesting-library.com",[26],[13,36,37],{},"Testing Library",".",[10,40,41,42,45,46,48,49,51,52,55],{},"When working on a ",[13,43,44],{},"Vite"," project, ",[13,47,29],{}," is the best choice to test our components. It's a test runner optimized for Vite and is very easy to use. ",[13,50,37],{}," builds on ",[13,53,54],{},"Vue Test Utils"," and provides a set of utilities to test our components in a user-centric way.",[10,57,58,59,61],{},"For example, ",[13,60,37],{}," encourages us to test the behavior of our components instead of their implementation. This is a powerful concept that allows us to test components in a way that is closer to the user experience and makes refactoring easier.",[63,64,66],"h2",{"id":65},"setting-up-vitest","Setting up Vitest",[10,68,69,70,30,72,74,75,79],{},"To get started, we need to install ",[13,71,29],{},[13,73,37],{}," in our ",[76,77,78],"code",{},"packages\u002Fhuchet-vue"," package:",[81,82,88],"pre",{"className":83,"code":84,"filename":85,"language":86,"meta":87,"style":87},"language-bash shiki shiki-themes github-light github-light github-dark","pnpm add -D vitest @testing-library\u002Fvue jsdom\n","terminal","bash","",[76,89,90],{"__ignoreMap":87},[91,92,95,99,103,107,110,113],"span",{"class":93,"line":94},"line",1,[91,96,98],{"class":97},"snPdu","pnpm",[91,100,102],{"class":101},"sIIMD"," add",[91,104,106],{"class":105},"sBjJW"," -D",[91,108,109],{"class":101}," vitest",[91,111,112],{"class":101}," @testing-library\u002Fvue",[91,114,115],{"class":101}," jsdom\n",[10,117,118,119,122,123,126],{},"Since we are testing frontend components, we need a ",[13,120,121],{},"DOM"," environment. To avoid launching a real browser, which is slow and resource-consuming, we can use ",[13,124,125],{},"JSDOM",", a lightweight implementation of the DOM in Node.js.",[10,128,129,130,133],{},"To make Vitest work with JSDOM, we need to configure it in the ",[76,131,132],{},"vite.config.ts"," file:",[81,135,139],{"className":136,"code":137,"filename":132,"language":138,"meta":87,"style":87},"language-ts shiki shiki-themes github-light github-light github-dark","\u002F\u002F\u002F \u003Creference types=\"vitest\" \u002F>\n\nimport { defineConfig } from 'vite'\n\nexport default defineConfig({\n  \u002F\u002F ...\n\n  test: {\n    globals: true,\n    environment: 'jsdom',\n  },\n})\n","ts",[76,140,141,164,171,187,192,207,213,218,224,236,247,253],{"__ignoreMap":87},[91,142,143,147,151,154,158,161],{"class":93,"line":94},[91,144,146],{"class":145},"sCsY4","\u002F\u002F\u002F \u003C",[91,148,150],{"class":149},"sovSZ","reference",[91,152,153],{"class":97}," types",[91,155,157],{"class":156},"s8jYJ","=",[91,159,160],{"class":101},"\"vitest\"",[91,162,163],{"class":145}," \u002F>\n",[91,165,167],{"class":93,"line":166},2,[91,168,170],{"emptyLinePlaceholder":169},true,"\n",[91,172,174,177,181,184],{"class":93,"line":173},3,[91,175,176],{"class":156},"import",[91,178,180],{"class":179},"sxrX7"," { defineConfig } ",[91,182,183],{"class":156},"from",[91,185,186],{"class":101}," 'vite'\n",[91,188,190],{"class":93,"line":189},4,[91,191,170],{"emptyLinePlaceholder":169},[91,193,195,198,201,204],{"class":93,"line":194},5,[91,196,197],{"class":156},"export",[91,199,200],{"class":156}," default",[91,202,203],{"class":97}," defineConfig",[91,205,206],{"class":179},"({\n",[91,208,210],{"class":93,"line":209},6,[91,211,212],{"class":145},"  \u002F\u002F ...\n",[91,214,216],{"class":93,"line":215},7,[91,217,170],{"emptyLinePlaceholder":169},[91,219,221],{"class":93,"line":220},8,[91,222,223],{"class":179},"  test: {\n",[91,225,227,230,233],{"class":93,"line":226},9,[91,228,229],{"class":179},"    globals: ",[91,231,232],{"class":105},"true",[91,234,235],{"class":179},",\n",[91,237,239,242,245],{"class":93,"line":238},10,[91,240,241],{"class":179},"    environment: ",[91,243,244],{"class":101},"'jsdom'",[91,246,235],{"class":179},[91,248,250],{"class":93,"line":249},11,[91,251,252],{"class":179},"  },\n",[91,254,256],{"class":93,"line":255},12,[91,257,258],{"class":179},"})\n",[260,261,264],"callout",{"color":262,"icon":263},"info","i-ph-info-duotone",[10,265,266,269],{},[76,267,268],{},"\u002F\u002F\u002F \u003Creference types=\"vitest\" \u002F>"," is crucial to ensure the correct types are available in the Vite configuration file. It augments the Vite configuration file with the Vitest types.",[10,271,272,273,276,277,279],{},"In the ",[76,274,275],{},"package.json"," file of ",[76,278,78],{},", add two new scripts to run the tests:",[81,281,285],{"className":282,"code":283,"filename":275,"language":284,"meta":87,"style":87},"language-json shiki shiki-themes github-light github-light github-dark","{\n  \"scripts\": {\n    \"test\": \"vitest run\",\n    \"test:watch\": \"vitest\"\n  }\n}\n","json",[76,286,287,292,300,313,323,328],{"__ignoreMap":87},[91,288,289],{"class":93,"line":94},[91,290,291],{"class":179},"{\n",[91,293,294,297],{"class":93,"line":166},[91,295,296],{"class":105},"  \"scripts\"",[91,298,299],{"class":179},": {\n",[91,301,302,305,308,311],{"class":93,"line":173},[91,303,304],{"class":105},"    \"test\"",[91,306,307],{"class":179},": ",[91,309,310],{"class":101},"\"vitest run\"",[91,312,235],{"class":179},[91,314,315,318,320],{"class":93,"line":189},[91,316,317],{"class":105},"    \"test:watch\"",[91,319,307],{"class":179},[91,321,322],{"class":101},"\"vitest\"\n",[91,324,325],{"class":93,"line":194},[91,326,327],{"class":179},"  }\n",[91,329,330],{"class":93,"line":209},[91,331,332],{"class":179},"}\n",[10,334,335],{},"The first script runs the tests once, and the second script runs the tests in watch mode if it does not detect a CI environment.",[63,337,339],{"id":338},"typescript-configuration","TypeScript configuration",[10,341,342],{},"Our test files form a new TypeScript context. We need to access the Node.js API but not the browser environment. As with any new TypeScript context, we configure it through a new TSConfig file.",[10,344,345,346,349,350,353,354,357],{},"Create a new ",[76,347,348],{},"tsconfig.test.json"," file that extends from ",[76,351,352],{},"tsconfig.node.json"," and includes the ",[76,355,356],{},"test.ts"," files:",[81,359,361],{"className":282,"code":360,"filename":356,"language":284,"meta":87,"style":87},"{\n  \"extends\": \".\u002Ftsconfig.node.json\",\n  \"include\": [\"src\u002F**\u002F*.test.ts\"]\n}\n",[76,362,363,367,379,393],{"__ignoreMap":87},[91,364,365],{"class":93,"line":94},[91,366,291],{"class":179},[91,368,369,372,374,377],{"class":93,"line":166},[91,370,371],{"class":105},"  \"extends\"",[91,373,307],{"class":179},[91,375,376],{"class":101},"\".\u002Ftsconfig.node.json\"",[91,378,235],{"class":179},[91,380,381,384,387,390],{"class":93,"line":173},[91,382,383],{"class":105},"  \"include\"",[91,385,386],{"class":179},": [",[91,388,389],{"class":101},"\"src\u002F**\u002F*.test.ts\"",[91,391,392],{"class":179},"]\n",[91,394,395],{"class":93,"line":189},[91,396,332],{"class":179},[10,398,272,399,402,403,405,406,357],{},[76,400,401],{},"tsconfig.app.json",", we exclude the ",[76,404,356],{}," files, similar to how we excluded the ",[76,407,408],{},"story.vue",[81,410,413],{"className":411,"code":412,"filename":408,"language":284,"meta":87,"style":87},"language-json shiki shiki-themes github-light github-light github-dark has-diff","{\n  \"extends\": \"@vue\u002Ftsconfig\u002Ftsconfig.dom.json\",\n  \"include\": [\n    \"src\u002F**\u002F*.ts\",\n    \"src\u002F**\u002F*.vue\"\n  ],\n  \"exclude\": [\n    \"src\u002F**\u002F*.story.vue\",\n    \"src\u002F**\u002F*.test.ts\" \u002F\u002F [!code ++]\n  ]\n}\n",[76,414,415,419,430,437,444,449,454,461,468,476,481],{"__ignoreMap":87},[91,416,417],{"class":93,"line":94},[91,418,291],{"class":179},[91,420,421,423,425,428],{"class":93,"line":166},[91,422,371],{"class":105},[91,424,307],{"class":179},[91,426,427],{"class":101},"\"@vue\u002Ftsconfig\u002Ftsconfig.dom.json\"",[91,429,235],{"class":179},[91,431,432,434],{"class":93,"line":173},[91,433,383],{"class":105},[91,435,436],{"class":179},": [\n",[91,438,439,442],{"class":93,"line":189},[91,440,441],{"class":101},"    \"src\u002F**\u002F*.ts\"",[91,443,235],{"class":179},[91,445,446],{"class":93,"line":194},[91,447,448],{"class":101},"    \"src\u002F**\u002F*.vue\"\n",[91,450,451],{"class":93,"line":209},[91,452,453],{"class":179},"  ],\n",[91,455,456,459],{"class":93,"line":215},[91,457,458],{"class":105},"  \"exclude\"",[91,460,436],{"class":179},[91,462,463,466],{"class":93,"line":220},[91,464,465],{"class":101},"    \"src\u002F**\u002F*.story.vue\"",[91,467,235],{"class":179},[91,469,473],{"class":470,"line":226},[93,471,472],"diff","add",[91,474,475],{"class":101},"    \"src\u002F**\u002F*.test.ts\"",[91,477,478],{"class":93,"line":238},[91,479,480],{"class":179},"  ]\n",[91,482,483],{"class":93,"line":249},[91,484,332],{"class":179},[10,486,487,488,133],{},"In our main TSConfig file, we add a reference to the ",[76,489,348],{},[81,491,493],{"className":411,"code":492,"filename":348,"language":284,"meta":87,"style":87},"{\n  \"files\": [],\n  \"references\": [\n    {\n      \"path\": \".\u002Ftsconfig.app.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.node.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.story.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.test.json\" \u002F\u002F [!code ++]\n    }\n  ]\n}\n",[76,494,495,499,507,514,519,529,534,538,547,551,555,564,568,573,584,590,595],{"__ignoreMap":87},[91,496,497],{"class":93,"line":94},[91,498,291],{"class":179},[91,500,501,504],{"class":93,"line":166},[91,502,503],{"class":105},"  \"files\"",[91,505,506],{"class":179},": [],\n",[91,508,509,512],{"class":93,"line":173},[91,510,511],{"class":105},"  \"references\"",[91,513,436],{"class":179},[91,515,516],{"class":93,"line":189},[91,517,518],{"class":179},"    {\n",[91,520,521,524,526],{"class":93,"line":194},[91,522,523],{"class":105},"      \"path\"",[91,525,307],{"class":179},[91,527,528],{"class":101},"\".\u002Ftsconfig.app.json\"\n",[91,530,531],{"class":93,"line":209},[91,532,533],{"class":179},"    },\n",[91,535,536],{"class":93,"line":215},[91,537,518],{"class":179},[91,539,540,542,544],{"class":93,"line":220},[91,541,523],{"class":105},[91,543,307],{"class":179},[91,545,546],{"class":101},"\".\u002Ftsconfig.node.json\"\n",[91,548,549],{"class":93,"line":226},[91,550,533],{"class":179},[91,552,553],{"class":93,"line":238},[91,554,518],{"class":179},[91,556,557,559,561],{"class":93,"line":249},[91,558,523],{"class":105},[91,560,307],{"class":179},[91,562,563],{"class":101},"\".\u002Ftsconfig.story.json\"\n",[91,565,566],{"class":93,"line":255},[91,567,533],{"class":179},[91,569,571],{"class":93,"line":570},13,[91,572,518],{"class":179},[91,574,577,579,581],{"class":575,"line":576},[93,471,472],14,[91,578,523],{"class":105},[91,580,307],{"class":179},[91,582,583],{"class":101},"\".\u002Ftsconfig.test.json\"",[91,585,587],{"class":93,"line":586},15,[91,588,589],{"class":179},"    }\n",[91,591,593],{"class":93,"line":592},16,[91,594,480],{"class":179},[91,596,598],{"class":93,"line":597},17,[91,599,332],{"class":179},[63,601,603],{"id":602},"writing-our-first-test","Writing our first test",[10,605,606,607,610],{},"Every component is isolated and encapsulated in its folder from the start. Testing will follow the same structure. Create a ",[76,608,609],{},"src\u002FButton\u002FButton.test.ts"," file and write our first test:",[81,612,614],{"className":83,"code":613,"filename":609,"language":86,"meta":87,"style":87},"touch src\u002FButton\u002FButton.test.ts\n",[76,615,616],{"__ignoreMap":87},[91,617,618,621],{"class":93,"line":94},[91,619,620],{"class":97},"touch",[91,622,623],{"class":101}," src\u002FButton\u002FButton.test.ts\n",[81,625,628],{"className":136,"code":626,"filename":627,"language":138,"meta":87,"style":87},"import { fireEvent, render, screen } from '@testing-library\u002Fvue'\nimport { describe, expect, it } from 'vitest'\nimport Button from '.\u002FButton.vue'\n\ndescribe('button', () => {\n  it('should emit a click event when clicked', async () => {\n    const { emitted } = render(Button, {\n      props: {\n        label: 'Hello',\n      },\n    })\n\n    await fireEvent.click(screen.getByText('Hello'))\n\n    expect(emitted()).toMatchInlineSnapshot(`\n      {\n        \"click\": [\n          [\n            MouseEvent {\n              \"isTrusted\": false,\n            },\n          ],\n        ],\n      }\n    `)\n  })\n\n  describe('solid', () => {\n    it('should render a solid button', () => {\n      const button = render(Button, {\n        props: {\n          label: 'Hello',\n          variant: 'solid',\n        },\n      })\n\n      expect(button.html()).toMatchSnapshot()\n    })\n  })\n\n  describe('outline', () => {\n    it('should render an outline button', () => {\n      const button = render(Button, {\n        props: {\n          label: 'Hello',\n          variant: 'outline',\n        },\n      })\n\n      expect(button.html()).toMatchSnapshot()\n    })\n  })\n})\n","Example",[76,629,630,642,654,666,670,690,713,735,740,750,755,760,764,788,792,812,817,822,828,834,840,846,852,858,864,873,879,884,901,918,934,940,950,960,966,972,977,997,1002,1007,1012,1028,1044,1057,1062,1071,1080,1085,1090,1095,1110,1115,1120],{"__ignoreMap":87},[91,631,632,634,637,639],{"class":93,"line":94},[91,633,176],{"class":156},[91,635,636],{"class":179}," { fireEvent, render, screen } ",[91,638,183],{"class":156},[91,640,641],{"class":101}," '@testing-library\u002Fvue'\n",[91,643,644,646,649,651],{"class":93,"line":166},[91,645,176],{"class":156},[91,647,648],{"class":179}," { describe, expect, it } ",[91,650,183],{"class":156},[91,652,653],{"class":101}," 'vitest'\n",[91,655,656,658,661,663],{"class":93,"line":173},[91,657,176],{"class":156},[91,659,660],{"class":179}," Button ",[91,662,183],{"class":156},[91,664,665],{"class":101}," '.\u002FButton.vue'\n",[91,667,668],{"class":93,"line":189},[91,669,170],{"emptyLinePlaceholder":169},[91,671,672,675,678,681,684,687],{"class":93,"line":194},[91,673,674],{"class":97},"describe",[91,676,677],{"class":179},"(",[91,679,680],{"class":101},"'button'",[91,682,683],{"class":179},", () ",[91,685,686],{"class":156},"=>",[91,688,689],{"class":179}," {\n",[91,691,692,695,697,700,703,706,709,711],{"class":93,"line":209},[91,693,694],{"class":97},"  it",[91,696,677],{"class":179},[91,698,699],{"class":101},"'should emit a click event when clicked'",[91,701,702],{"class":179},", ",[91,704,705],{"class":156},"async",[91,707,708],{"class":179}," () ",[91,710,686],{"class":156},[91,712,689],{"class":179},[91,714,715,718,721,724,727,729,732],{"class":93,"line":215},[91,716,717],{"class":156},"    const",[91,719,720],{"class":179}," { ",[91,722,723],{"class":105},"emitted",[91,725,726],{"class":179}," } ",[91,728,157],{"class":156},[91,730,731],{"class":97}," render",[91,733,734],{"class":179},"(Button, {\n",[91,736,737],{"class":93,"line":220},[91,738,739],{"class":179},"      props: {\n",[91,741,742,745,748],{"class":93,"line":226},[91,743,744],{"class":179},"        label: ",[91,746,747],{"class":101},"'Hello'",[91,749,235],{"class":179},[91,751,752],{"class":93,"line":238},[91,753,754],{"class":179},"      },\n",[91,756,757],{"class":93,"line":249},[91,758,759],{"class":179},"    })\n",[91,761,762],{"class":93,"line":255},[91,763,170],{"emptyLinePlaceholder":169},[91,765,766,769,772,775,778,781,783,785],{"class":93,"line":570},[91,767,768],{"class":156},"    await",[91,770,771],{"class":179}," fireEvent.",[91,773,774],{"class":97},"click",[91,776,777],{"class":179},"(screen.",[91,779,780],{"class":97},"getByText",[91,782,677],{"class":179},[91,784,747],{"class":101},[91,786,787],{"class":179},"))\n",[91,789,790],{"class":93,"line":576},[91,791,170],{"emptyLinePlaceholder":169},[91,793,794,797,799,801,804,807,809],{"class":93,"line":586},[91,795,796],{"class":97},"    expect",[91,798,677],{"class":179},[91,800,723],{"class":97},[91,802,803],{"class":179},"()).",[91,805,806],{"class":97},"toMatchInlineSnapshot",[91,808,677],{"class":179},[91,810,811],{"class":101},"`\n",[91,813,814],{"class":93,"line":592},[91,815,816],{"class":101},"      {\n",[91,818,819],{"class":93,"line":597},[91,820,821],{"class":101},"        \"click\": [\n",[91,823,825],{"class":93,"line":824},18,[91,826,827],{"class":101},"          [\n",[91,829,831],{"class":93,"line":830},19,[91,832,833],{"class":101},"            MouseEvent {\n",[91,835,837],{"class":93,"line":836},20,[91,838,839],{"class":101},"              \"isTrusted\": false,\n",[91,841,843],{"class":93,"line":842},21,[91,844,845],{"class":101},"            },\n",[91,847,849],{"class":93,"line":848},22,[91,850,851],{"class":101},"          ],\n",[91,853,855],{"class":93,"line":854},23,[91,856,857],{"class":101},"        ],\n",[91,859,861],{"class":93,"line":860},24,[91,862,863],{"class":101},"      }\n",[91,865,867,870],{"class":93,"line":866},25,[91,868,869],{"class":101},"    `",[91,871,872],{"class":179},")\n",[91,874,876],{"class":93,"line":875},26,[91,877,878],{"class":179},"  })\n",[91,880,882],{"class":93,"line":881},27,[91,883,170],{"emptyLinePlaceholder":169},[91,885,887,890,892,895,897,899],{"class":93,"line":886},28,[91,888,889],{"class":97},"  describe",[91,891,677],{"class":179},[91,893,894],{"class":101},"'solid'",[91,896,683],{"class":179},[91,898,686],{"class":156},[91,900,689],{"class":179},[91,902,904,907,909,912,914,916],{"class":93,"line":903},29,[91,905,906],{"class":97},"    it",[91,908,677],{"class":179},[91,910,911],{"class":101},"'should render a solid button'",[91,913,683],{"class":179},[91,915,686],{"class":156},[91,917,689],{"class":179},[91,919,921,924,927,930,932],{"class":93,"line":920},30,[91,922,923],{"class":156},"      const",[91,925,926],{"class":105}," button",[91,928,929],{"class":156}," =",[91,931,731],{"class":97},[91,933,734],{"class":179},[91,935,937],{"class":93,"line":936},31,[91,938,939],{"class":179},"        props: {\n",[91,941,943,946,948],{"class":93,"line":942},32,[91,944,945],{"class":179},"          label: ",[91,947,747],{"class":101},[91,949,235],{"class":179},[91,951,953,956,958],{"class":93,"line":952},33,[91,954,955],{"class":179},"          variant: ",[91,957,894],{"class":101},[91,959,235],{"class":179},[91,961,963],{"class":93,"line":962},34,[91,964,965],{"class":179},"        },\n",[91,967,969],{"class":93,"line":968},35,[91,970,971],{"class":179},"      })\n",[91,973,975],{"class":93,"line":974},36,[91,976,170],{"emptyLinePlaceholder":169},[91,978,980,983,986,989,991,994],{"class":93,"line":979},37,[91,981,982],{"class":97},"      expect",[91,984,985],{"class":179},"(button.",[91,987,988],{"class":97},"html",[91,990,803],{"class":179},[91,992,993],{"class":97},"toMatchSnapshot",[91,995,996],{"class":179},"()\n",[91,998,1000],{"class":93,"line":999},38,[91,1001,759],{"class":179},[91,1003,1005],{"class":93,"line":1004},39,[91,1006,878],{"class":179},[91,1008,1010],{"class":93,"line":1009},40,[91,1011,170],{"emptyLinePlaceholder":169},[91,1013,1015,1017,1019,1022,1024,1026],{"class":93,"line":1014},41,[91,1016,889],{"class":97},[91,1018,677],{"class":179},[91,1020,1021],{"class":101},"'outline'",[91,1023,683],{"class":179},[91,1025,686],{"class":156},[91,1027,689],{"class":179},[91,1029,1031,1033,1035,1038,1040,1042],{"class":93,"line":1030},42,[91,1032,906],{"class":97},[91,1034,677],{"class":179},[91,1036,1037],{"class":101},"'should render an outline button'",[91,1039,683],{"class":179},[91,1041,686],{"class":156},[91,1043,689],{"class":179},[91,1045,1047,1049,1051,1053,1055],{"class":93,"line":1046},43,[91,1048,923],{"class":156},[91,1050,926],{"class":105},[91,1052,929],{"class":156},[91,1054,731],{"class":97},[91,1056,734],{"class":179},[91,1058,1060],{"class":93,"line":1059},44,[91,1061,939],{"class":179},[91,1063,1065,1067,1069],{"class":93,"line":1064},45,[91,1066,945],{"class":179},[91,1068,747],{"class":101},[91,1070,235],{"class":179},[91,1072,1074,1076,1078],{"class":93,"line":1073},46,[91,1075,955],{"class":179},[91,1077,1021],{"class":101},[91,1079,235],{"class":179},[91,1081,1083],{"class":93,"line":1082},47,[91,1084,965],{"class":179},[91,1086,1088],{"class":93,"line":1087},48,[91,1089,971],{"class":179},[91,1091,1093],{"class":93,"line":1092},49,[91,1094,170],{"emptyLinePlaceholder":169},[91,1096,1098,1100,1102,1104,1106,1108],{"class":93,"line":1097},50,[91,1099,982],{"class":97},[91,1101,985],{"class":179},[91,1103,988],{"class":97},[91,1105,803],{"class":179},[91,1107,993],{"class":97},[91,1109,996],{"class":179},[91,1111,1113],{"class":93,"line":1112},51,[91,1114,759],{"class":179},[91,1116,1118],{"class":93,"line":1117},52,[91,1119,878],{"class":179},[91,1121,1123],{"class":93,"line":1122},53,[91,1124,258],{"class":179},[10,1126,1127,1128,1131,1132,1135,1136,1139,1140,1143,1144,1146],{},"In the first test, we use the ",[76,1129,1130],{},"render"," function from Testing Library to render our ",[76,1133,1134],{},"Button"," component. We also use the ",[76,1137,1138],{},"fireEvent"," function to simulate a click event on the button. Finally, we utilize the ",[76,1141,1142],{},"expect"," function from Vitest to assert that the component emits a ",[76,1145,774],{}," event when clicked.",[10,1148,1149,1150,30,1153,1156,1157,1159],{},"In the following tests, we verify the ",[76,1151,1152],{},"solid",[76,1154,1155],{},"outline"," variants of the button. We use the ",[76,1158,993],{}," function from Vitest to assert that the component renders correctly.",[10,1161,1162],{},"These tests reflect two key approaches I prefer when testing components:",[1164,1165,1166,1170],"ol",{},[1167,1168,1169],"li",{},"Testing the behavior of the component. For instance, does it emit the correct events when clicked?",[1167,1171,1172],{},"Testing the appearance of the component. For example, does it render correctly with different variants?",[10,1174,1175,1176,38],{},"Most of the time, these two types of tests are sufficient to ensure our components work as expected, and they can be easily achieved using ",[13,1177,1178],{},"snapshot testing",[260,1180,1183],{"color":1181,"icon":1182},"success","i-ph-lightbulb-duotone",[10,1184,1185],{},"Snapshot testing is a powerful tool that allows us to capture the output of our components and compare it against a reference snapshot. This is very useful for checking the appearance of our components, ensuring they render correctly, and avoiding manually writing the expected HTML.",[10,1187,1188],{},"We can run our tests with the following command:",[81,1190,1192],{"className":83,"code":1191,"filename":85,"language":86,"meta":87,"style":87},"pnpm run test\n",[76,1193,1194],{"__ignoreMap":87},[91,1195,1196,1198,1201],{"class":93,"line":94},[91,1197,98],{"class":97},[91,1199,1200],{"class":101}," run",[91,1202,1203],{"class":101}," test\n",[10,1205,1206],{},"Everything works as expected!",[81,1208,1210],{"className":83,"code":1209,"filename":85,"language":86,"meta":87,"style":87}," DEV  v2.1.5 \u002Fhuchet-vue\u002Fpackages\u002Fhuchet-vue\n\n ✓ src\u002FButton\u002FButton.test.ts (3)\n   ✓ button (3)\n     ✓ should emit a click event when clicked\n     ✓ solid (1)\n       ✓ should render a solid button\n     ✓ outline (1)\n       ✓ should render an outline button\n\n Test Files  1 passed (1)\n      Tests  3 passed (3)\n   Start at  00:00:00\n   Duration  536ms (transform 42ms, setup 0ms, collect 142ms, tests 22ms, environment 237ms, prepare 34ms)\n",[76,1211,1212,1223,1227,1238,1247,1273,1283,1299,1308,1323,1327,1343,1355,1366],{"__ignoreMap":87},[91,1213,1214,1217,1220],{"class":93,"line":94},[91,1215,1216],{"class":97}," DEV",[91,1218,1219],{"class":101},"  v2.1.5",[91,1221,1222],{"class":101}," \u002Fhuchet-vue\u002Fpackages\u002Fhuchet-vue\n",[91,1224,1225],{"class":93,"line":166},[91,1226,170],{"emptyLinePlaceholder":169},[91,1228,1229,1232,1235],{"class":93,"line":173},[91,1230,1231],{"class":97}," ✓",[91,1233,1234],{"class":101}," src\u002FButton\u002FButton.test.ts",[91,1236,1237],{"class":179}," (3)\n",[91,1239,1240,1243,1245],{"class":93,"line":189},[91,1241,1242],{"class":97},"   ✓",[91,1244,926],{"class":101},[91,1246,1237],{"class":179},[91,1248,1249,1252,1255,1258,1261,1264,1267,1270],{"class":93,"line":194},[91,1250,1251],{"class":97},"     ✓",[91,1253,1254],{"class":101}," should",[91,1256,1257],{"class":101}," emit",[91,1259,1260],{"class":101}," a",[91,1262,1263],{"class":101}," click",[91,1265,1266],{"class":101}," event",[91,1268,1269],{"class":101}," when",[91,1271,1272],{"class":101}," clicked\n",[91,1274,1275,1277,1280],{"class":93,"line":209},[91,1276,1251],{"class":97},[91,1278,1279],{"class":101}," solid",[91,1281,1282],{"class":179}," (1)\n",[91,1284,1285,1288,1290,1292,1294,1296],{"class":93,"line":215},[91,1286,1287],{"class":97},"       ✓",[91,1289,1254],{"class":101},[91,1291,731],{"class":101},[91,1293,1260],{"class":101},[91,1295,1279],{"class":101},[91,1297,1298],{"class":101}," button\n",[91,1300,1301,1303,1306],{"class":93,"line":220},[91,1302,1251],{"class":97},[91,1304,1305],{"class":101}," outline",[91,1307,1282],{"class":179},[91,1309,1310,1312,1314,1316,1319,1321],{"class":93,"line":226},[91,1311,1287],{"class":97},[91,1313,1254],{"class":101},[91,1315,731],{"class":101},[91,1317,1318],{"class":101}," an",[91,1320,1305],{"class":101},[91,1322,1298],{"class":101},[91,1324,1325],{"class":93,"line":238},[91,1326,170],{"emptyLinePlaceholder":169},[91,1328,1329,1332,1335,1338,1341],{"class":93,"line":249},[91,1330,1331],{"class":97}," Test",[91,1333,1334],{"class":101}," Files",[91,1336,1337],{"class":105},"  1",[91,1339,1340],{"class":101}," passed",[91,1342,1282],{"class":179},[91,1344,1345,1348,1351,1353],{"class":93,"line":255},[91,1346,1347],{"class":97},"      Tests",[91,1349,1350],{"class":105},"  3",[91,1352,1340],{"class":101},[91,1354,1237],{"class":179},[91,1356,1357,1360,1363],{"class":93,"line":570},[91,1358,1359],{"class":97},"   Start",[91,1361,1362],{"class":101}," at",[91,1364,1365],{"class":101},"  00:00:00\n",[91,1367,1368,1371,1374,1377,1380,1383,1386,1389,1392,1395,1398,1401,1404,1407,1410],{"class":93,"line":576},[91,1369,1370],{"class":97},"   Duration",[91,1372,1373],{"class":101},"  536ms",[91,1375,1376],{"class":179}," (transform ",[91,1378,1379],{"class":101},"42ms,",[91,1381,1382],{"class":101}," setup",[91,1384,1385],{"class":101}," 0ms,",[91,1387,1388],{"class":101}," collect",[91,1390,1391],{"class":101}," 142ms,",[91,1393,1394],{"class":101}," tests",[91,1396,1397],{"class":101}," 22ms,",[91,1399,1400],{"class":101}," environment",[91,1402,1403],{"class":101}," 237ms,",[91,1405,1406],{"class":101}," prepare",[91,1408,1409],{"class":101}," 34ms",[91,1411,872],{"class":179},[63,1413,1415],{"id":1414},"typechecking-tests","Typechecking tests",[10,1417,1418,1419,1421],{},"Besides tests, we can ensure every type in the project is correct. This is a straightforward way to detect errors before they happen, but it doesn't replace the tests. We can add a new script to the ",[76,1420,275],{}," file to run the typecheck:",[81,1423,1425],{"className":282,"code":1424,"filename":275,"language":284,"meta":87,"style":87},"{\n  \"scripts\": {\n    \"typecheck\": \"vue-tsc -p tsconfig.app.json --noEmit\"\n  }\n}\n",[76,1426,1427,1431,1437,1447,1451],{"__ignoreMap":87},[91,1428,1429],{"class":93,"line":94},[91,1430,291],{"class":179},[91,1432,1433,1435],{"class":93,"line":166},[91,1434,296],{"class":105},[91,1436,299],{"class":179},[91,1438,1439,1442,1444],{"class":93,"line":173},[91,1440,1441],{"class":105},"    \"typecheck\"",[91,1443,307],{"class":179},[91,1445,1446],{"class":101},"\"vue-tsc -p tsconfig.app.json --noEmit\"\n",[91,1448,1449],{"class":93,"line":189},[91,1450,327],{"class":179},[91,1452,1453],{"class":93,"line":194},[91,1454,332],{"class":179},[10,1456,1457,1458,1461,1462,1464,1465,1468],{},"This script uses the ",[76,1459,1460],{},"vue-tsc"," command to typecheck the project within the context of the ",[76,1463,401],{}," file. The ",[76,1466,1467],{},"--noEmit"," option prevents transpiling the TypeScript files into JavaScript files, focusing solely on typechecking without building.",[1470,1471,1472],"style",{},"html pre.shiki code .snPdu, html code.shiki .snPdu{--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sIIMD, html code.shiki .sIIMD{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sBjJW, html code.shiki .sBjJW{--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sCsY4, html code.shiki .sCsY4{--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sovSZ, html code.shiki .sovSZ{--shiki-light:#22863A;--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .s8jYJ, html code.shiki .s8jYJ{--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sxrX7, html code.shiki .sxrX7{--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":87,"searchDepth":166,"depth":166,"links":1474},[1475,1476,1477,1478],{"id":65,"depth":166,"text":66},{"id":338,"depth":166,"text":339},{"id":602,"depth":166,"text":603},{"id":1414,"depth":166,"text":1415},"e95133e4-2362-4aa3-bd3d-b70afc6c4d4e","2024-11-24","Learn how to automate testing for Vue.js components using Vitest and Testing Library for efficient and user-focused testing.","md",null,{},"\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fautomatic-component-testing-with-vitest-and-testing-library","---\ncontentId: e95133e4-2362-4aa3-bd3d-b70afc6c4d4e\npart: 4\ntitle: \"Automatic Component Testing with Vitest and Testing Library\"\ndescription: \"Learn how to automate testing for Vue.js components using Vitest and Testing Library for efficient and user-focused testing.\"\ndate: 2024-11-24\nseriesSlug: the-complete-guide-to-building-a-vue-js-component-library\n---\n\n**Testing** is an essential part of software development. It ensures our components work as expected and helps us catch bugs early in the development process. In this article, we'll explore how to automatically test **Vue.js** components using [**Vitest**](https:\u002F\u002Fvitest.dev) and [**Testing Library**](https:\u002F\u002Ftesting-library.com).\n\nWhen working on a **Vite** project, **Vitest** is the best choice to test our components. It's a test runner optimized for Vite and is very easy to use. **Testing Library** builds on **Vue Test Utils** and provides a set of utilities to test our components in a user-centric way.\n\nFor example, **Testing Library** encourages us to test the behavior of our components instead of their implementation. This is a powerful concept that allows us to test components in a way that is closer to the user experience and makes refactoring easier.\n\n## Setting up Vitest\n\nTo get started, we need to install **Vitest** and **Testing Library** in our `packages\u002Fhuchet-vue` package:\n\n```bash [terminal]\npnpm add -D vitest @testing-library\u002Fvue jsdom\n```\n\nSince we are testing frontend components, we need a **DOM** environment. To avoid launching a real browser, which is slow and resource-consuming, we can use **JSDOM**, a lightweight implementation of the DOM in Node.js.\n\nTo make Vitest work with JSDOM, we need to configure it in the `vite.config.ts` file:\n\n```ts [vite.config.ts]\n\u002F\u002F\u002F \u003Creference types=\"vitest\" \u002F>\n\nimport { defineConfig } from 'vite'\n\nexport default defineConfig({\n  \u002F\u002F ...\n\n  test: {\n    globals: true,\n    environment: 'jsdom',\n  },\n})\n```\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\n`\u002F\u002F\u002F \u003Creference types=\"vitest\" \u002F>` is crucial to ensure the correct types are available in the Vite configuration file. It augments the Vite configuration file with the Vitest types.\n::\n\nIn the `package.json` file of `packages\u002Fhuchet-vue`, add two new scripts to run the tests:\n\n```json [package.json]\n{\n  \"scripts\": {\n    \"test\": \"vitest run\",\n    \"test:watch\": \"vitest\"\n  }\n}\n```\n\nThe first script runs the tests once, and the second script runs the tests in watch mode if it does not detect a CI environment.\n\n## TypeScript configuration\n\nOur test files form a new TypeScript context. We need to access the Node.js API but not the browser environment. As with any new TypeScript context, we configure it through a new TSConfig file.\n\nCreate a new `tsconfig.test.json` file that extends from `tsconfig.node.json` and includes the `test.ts` files:\n\n```json [test.ts]\n{\n  \"extends\": \".\u002Ftsconfig.node.json\",\n  \"include\": [\"src\u002F**\u002F*.test.ts\"]\n}\n```\n\nIn the `tsconfig.app.json`, we exclude the `test.ts` files, similar to how we excluded the `story.vue` files:\n\n```json [story.vue]\n{\n  \"extends\": \"@vue\u002Ftsconfig\u002Ftsconfig.dom.json\",\n  \"include\": [\n    \"src\u002F**\u002F*.ts\",\n    \"src\u002F**\u002F*.vue\"\n  ],\n  \"exclude\": [\n    \"src\u002F**\u002F*.story.vue\",\n    \"src\u002F**\u002F*.test.ts\" \u002F\u002F [!code ++]\n  ]\n}\n```\n\nIn our main TSConfig file, we add a reference to the `tsconfig.test.json` file:\n\n```json [tsconfig.test.json]\n{\n  \"files\": [],\n  \"references\": [\n    {\n      \"path\": \".\u002Ftsconfig.app.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.node.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.story.json\"\n    },\n    {\n      \"path\": \".\u002Ftsconfig.test.json\" \u002F\u002F [!code ++]\n    }\n  ]\n}\n```\n\n## Writing our first test\n\nEvery component is isolated and encapsulated in its folder from the start. Testing will follow the same structure. Create a `src\u002FButton\u002FButton.test.ts` file and write our first test:\n\n```bash [src\u002FButton\u002FButton.test.ts]\ntouch src\u002FButton\u002FButton.test.ts\n```\n\n```ts [Example]\nimport { fireEvent, render, screen } from '@testing-library\u002Fvue'\nimport { describe, expect, it } from 'vitest'\nimport Button from '.\u002FButton.vue'\n\ndescribe('button', () => {\n  it('should emit a click event when clicked', async () => {\n    const { emitted } = render(Button, {\n      props: {\n        label: 'Hello',\n      },\n    })\n\n    await fireEvent.click(screen.getByText('Hello'))\n\n    expect(emitted()).toMatchInlineSnapshot(`\n      {\n        \"click\": [\n          [\n            MouseEvent {\n              \"isTrusted\": false,\n            },\n          ],\n        ],\n      }\n    `)\n  })\n\n  describe('solid', () => {\n    it('should render a solid button', () => {\n      const button = render(Button, {\n        props: {\n          label: 'Hello',\n          variant: 'solid',\n        },\n      })\n\n      expect(button.html()).toMatchSnapshot()\n    })\n  })\n\n  describe('outline', () => {\n    it('should render an outline button', () => {\n      const button = render(Button, {\n        props: {\n          label: 'Hello',\n          variant: 'outline',\n        },\n      })\n\n      expect(button.html()).toMatchSnapshot()\n    })\n  })\n})\n```\n\nIn the first test, we use the `render` function from Testing Library to render our `Button` component. We also use the `fireEvent` function to simulate a click event on the button. Finally, we utilize the `expect` function from Vitest to assert that the component emits a `click` event when clicked.\n\nIn the following tests, we verify the `solid` and `outline` variants of the button. We use the `toMatchSnapshot` function from Vitest to assert that the component renders correctly.\n\nThese tests reflect two key approaches I prefer when testing components:\n\n1. Testing the behavior of the component. For instance, does it emit the correct events when clicked?\n2. Testing the appearance of the component. For example, does it render correctly with different variants?\n\nMost of the time, these two types of tests are sufficient to ensure our components work as expected, and they can be easily achieved using **snapshot testing**.\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nSnapshot testing is a powerful tool that allows us to capture the output of our components and compare it against a reference snapshot. This is very useful for checking the appearance of our components, ensuring they render correctly, and avoiding manually writing the expected HTML.\n::\n\nWe can run our tests with the following command:\n\n```bash [terminal]\npnpm run test\n```\n\nEverything works as expected!\n\n```bash [terminal]\n DEV  v2.1.5 \u002Fhuchet-vue\u002Fpackages\u002Fhuchet-vue\n\n ✓ src\u002FButton\u002FButton.test.ts (3)\n   ✓ button (3)\n     ✓ should emit a click event when clicked\n     ✓ solid (1)\n       ✓ should render a solid button\n     ✓ outline (1)\n       ✓ should render an outline button\n\n Test Files  1 passed (1)\n      Tests  3 passed (3)\n   Start at  00:00:00\n   Duration  536ms (transform 42ms, setup 0ms, collect 142ms, tests 22ms, environment 237ms, prepare 34ms)\n```\n\n## Typechecking tests\n\nBesides tests, we can ensure every type in the project is correct. This is a straightforward way to detect errors before they happen, but it doesn't replace the tests. We can add a new script to the `package.json` file to run the typecheck:\n\n```json [package.json]\n{\n  \"scripts\": {\n    \"typecheck\": \"vue-tsc -p tsconfig.app.json --noEmit\"\n  }\n}\n```\n\nThis script uses the `vue-tsc` command to typecheck the project within the context of the `tsconfig.app.json` file. The `--noEmit` option prevents transpiling the TypeScript files into JavaScript files, focusing solely on typechecking without building.\n",{"title":5,"description":1481},"the-complete-guide-to-building-a-vue-js-component-library",{"loc":1485},"series\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fautomatic-component-testing-with-vitest-and-testing-library","bXz2BPczSYrp5JzcCyBOCb4dT2BgaDJHiAelHPRezCI",{"overview":1493,"chapters":1692},{"id":1494,"title":1495,"body":1496,"contentId":1674,"date":1480,"description":1675,"extension":1482,"github":1676,"meetTheInstructor":1679,"meta":1680,"navigation":169,"part":1483,"path":1681,"rawbody":1682,"robots":1483,"schemaOrg":1483,"seo":1683,"seriesSlug":1488,"sitemap":1684,"stem":1685,"subtitle":1686,"topics":1687,"__hash__":1691},"series\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library.md","The Complete Guide to Building a Vue.js Component Library",{"type":7,"value":1497,"toc":1670},[1498,1501,1504,1516,1520,1523,1536,1540,1543,1657,1660,1664],[10,1499,1500],{},"Welcome! 👋",[10,1502,1503],{},"Initially, this series was intended to be a single article, but it soon became apparent that it would be too lengthy. Even GitHub didn't want to display it in the file tab of pull requests. Therefore, I decided to divide it into multiple articles to make it more digestible and to focus on each part individually.",[10,1505,1506,1507,1511,1512],{},"This series is somewhat of a sequel to the article ",[22,1508,1510],{"href":1509},"\u002Fposts\u002Fthe-simplest-method-to-create-a-vue-js-component-library","The Simplest Method to Create a Vue.js Component Library"," but adopts a more professional approach. ",[1513,1514,1515],"em",{},"You will learn how the most popular component libraries are constructed behind the scenes and how to create your own.",[1517,1518],"callout-read-more",{"id":1519},"f9754148-40bd-4df2-a144-f686e894b200",[10,1521,1522],{},"While it's still my preferred method when I am certain about what I want to achieve, I also received feedback from readers seeking more custom behavior and requiring more complex features.",[10,1524,1525,1526,1531,1532,38],{},"That's understandable. While ",[22,1527,1530],{"href":1528,"rel":1529},"https:\u002F\u002Fgithub.com\u002Funjs\u002Funbuild",[26],"unbuild"," is a powerful tool, it cannot match the flexibility, plugin ecosystem, and adoption of ",[22,1533,44],{"href":1534,"rel":1535},"https:\u002F\u002Fvite.dev\u002F",[26],[63,1537,1539],{"id":1538},"what-to-expect","What to expect",[10,1541,1542],{},"Throughout this series, we will address the following topics:",[1164,1544,1545,1557,1589,1607,1621,1627,1639],{},[1167,1546,1547,1550,1551,1556],{},[13,1548,1549],{},"Getting Started with Monorepo, Linter, and Formatter",": We'll set up a monorepo to manage our component library and all the tools necessary to ensure a good development experience. We'll also configure ",[22,1552,1555],{"href":1553,"rel":1554},"https:\u002F\u002Feslint.org\u002F",[26],"ESLint"," to ensure code consistency.",[1167,1558,1559,1562,1563,702,1567,1572,1573,1578,1579,30,1584,38],{},[13,1560,1561],{},"Building a First Component with Vue.js and Tailwind CSS",": We'll create our first component with ",[22,1564,19],{"href":1565,"rel":1566},"https:\u002F\u002Fvuejs.org\u002F",[26],[22,1568,1571],{"href":1569,"rel":1570},"https:\u002F\u002Ftailwindcss.com\u002F",[26],"Tailwind CSS",", and ",[22,1574,1577],{"href":1575,"rel":1576},"https:\u002F\u002Fwww.tailwind-variants.org\u002F",[26],"Tailwind Variants"," using the most advanced and powerful techniques employed in popular component libraries like ",[22,1580,1583],{"href":1581,"rel":1582},"https:\u002F\u002Fwww.radix-vue.com\u002F",[26],"Radix Vue",[22,1585,1588],{"href":1586,"rel":1587},"https:\u002F\u002Fui.nuxt.com\u002F",[26],"Nuxt UI",[1167,1590,1591,1594,1595,1600,1601,1606],{},[13,1592,1593],{},"A Better Development Experience with TypeScript and TSConfig",": We'll enhance our development experience by adding ",[22,1596,1599],{"href":1597,"rel":1598},"https:\u002F\u002Fwww.typescriptlang.org\u002F",[26],"TypeScript"," to our component library and configuring ",[22,1602,1605],{"href":1603,"rel":1604},"https:\u002F\u002Fwww.typescriptlang.org\u002Ftsconfig",[26],"TSConfig"," to ensure that our components are typed correctly and that we can take full advantage of TypeScript's features.",[1167,1608,1609,1611,1612,30,1616,1620],{},[13,1610,5],{},": We'll write tests for our components using ",[22,1613,29],{"href":1614,"rel":1615},"https:\u002F\u002Fvitest.dev\u002Fguide\u002F",[26],[22,1617,37],{"href":1618,"rel":1619},"https:\u002F\u002Ftesting-library.com\u002F",[26]," to ensure that our components function as expected. This is crucial for a professional component library that remains reliable over time.",[1167,1622,1623,1626],{},[13,1624,1625],{},"Delivering Components Continuously and with Confidence",": We'll establish a continuous integration process to ensure that our components are always delivered with confidence. For every commit of every branch, we'll run our tests to verify everything is functioning correctly. We'll also establish a release process to publish our components to npm.",[1167,1628,1629,1632,1633,1638],{},[13,1630,1631],{},"Creating a Playground and a Storybook for Components",": We'll create a playground to test our components in a real environment and a storybook with ",[22,1634,1637],{"href":1635,"rel":1636},"https:\u002F\u002Fhistoire.dev\u002F",[26],"Histoire"," to document them. This will enable us to share our components with other developers and to test them in various scenarios, both in isolation and in combination.",[1167,1640,1641,1644,1645,1650,1651,1656],{},[13,1642,1643],{},"Enhancing Integration with Unplugin and Nuxt Module",": We'll enhance the integration of our component library with the ecosystem by creating a Vite plugin with ",[22,1646,1649],{"href":1647,"rel":1648},"https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components",[26],"Unplugin Vue Components"," and a ",[22,1652,1655],{"href":1653,"rel":1654},"https:\u002F\u002Fnuxt.com\u002Fdocs\u002Fguide\u002Fgoing-further\u002Fmodules",[26],"Nuxt module"," to utilize our components in a Nuxt application.",[10,1658,1659],{},"By the end of this series, you will have a complete Vue.js component library that you can use in your projects or share with the community, like a professional developer. Isn't that exciting? 😎",[63,1661,1663],{"id":1662},"chapters","Chapters",[1665,1666],"series-chapters-list",{"className":1667},[1668,1669],"mx-auto","max-w-5xl",{"title":87,"searchDepth":166,"depth":166,"links":1671},[1672,1673],{"id":1538,"depth":166,"text":1539},{"id":1662,"depth":166,"text":1663},"55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1","Discover how to build a Vue.js component library using Vite with a guide that covers essential techniques for professional web development.",{"name":1677,"repository":1678},"huchet-vue","barbapapazes\u002Fhuchet-vue","I create this series because I love \u003Cstrong>Vue.js\u003C\u002Fstrong> and, for years, I have been unable to find a complete, up-to-date, and \u003Cstrong>professional guide\u003C\u002Fstrong> on how to build a Vue.js component library and want more software developers to feel empowered to create their own.",{},"\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library","---\ncontentId: 55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1\ntitle: The Complete Guide to Building a Vue.js Component Library\nsubtitle: The most advanced and professional course to build a Vue.js component library with Vite.\ndescription: Discover how to build a Vue.js component library using Vite with a guide that covers essential techniques for professional web development.\ndate: 2024-11-24\ntopics:\n  - vue\n  - vite\n  - component library\ngithub:\n  name: huchet-vue\n  repository: barbapapazes\u002Fhuchet-vue\nmeetTheInstructor: I create this series because I love \u003Cstrong>Vue.js\u003C\u002Fstrong> and, for years, I have been unable to find a complete, up-to-date, and \u003Cstrong>professional guide\u003C\u002Fstrong> on how to build a Vue.js component library and want more software developers to feel empowered to create their own.\nseriesSlug: the-complete-guide-to-building-a-vue-js-component-library\n---\n\nWelcome! 👋\n\nInitially, this series was intended to be a single article, but it soon became apparent that it would be too lengthy. Even GitHub didn't want to display it in the file tab of pull requests. Therefore, I decided to divide it into multiple articles to make it more digestible and to focus on each part individually.\n\nThis series is somewhat of a sequel to the article [The Simplest Method to Create a Vue.js Component Library](\u002Fposts\u002Fthe-simplest-method-to-create-a-vue-js-component-library) but adopts a more professional approach. _You will learn how the most popular component libraries are constructed behind the scenes and how to create your own._\n\n\u003CCalloutReadMore id=\"f9754148-40bd-4df2-a144-f686e894b200\" \u002F>\n\nWhile it's still my preferred method when I am certain about what I want to achieve, I also received feedback from readers seeking more custom behavior and requiring more complex features.\n\nThat's understandable. While [unbuild](https:\u002F\u002Fgithub.com\u002Funjs\u002Funbuild) is a powerful tool, it cannot match the flexibility, plugin ecosystem, and adoption of [Vite](https:\u002F\u002Fvite.dev\u002F).\n\n## What to expect\n\nThroughout this series, we will address the following topics:\n\n1. **Getting Started with Monorepo, Linter, and Formatter**: We'll set up a monorepo to manage our component library and all the tools necessary to ensure a good development experience. We'll also configure [ESLint](https:\u002F\u002Feslint.org\u002F) to ensure code consistency.\n2. **Building a First Component with Vue.js and Tailwind CSS**: We'll create our first component with [Vue.js](https:\u002F\u002Fvuejs.org\u002F), [Tailwind CSS](https:\u002F\u002Ftailwindcss.com\u002F), and [Tailwind Variants](https:\u002F\u002Fwww.tailwind-variants.org\u002F) using the most advanced and powerful techniques employed in popular component libraries like [Radix Vue](https:\u002F\u002Fwww.radix-vue.com\u002F) and [Nuxt UI](https:\u002F\u002Fui.nuxt.com\u002F).\n3. **A Better Development Experience with TypeScript and TSConfig**: We'll enhance our development experience by adding [TypeScript](https:\u002F\u002Fwww.typescriptlang.org\u002F) to our component library and configuring [TSConfig](https:\u002F\u002Fwww.typescriptlang.org\u002Ftsconfig) to ensure that our components are typed correctly and that we can take full advantage of TypeScript's features.\n4. **Automatic Component Testing with Vitest and Testing Library**: We'll write tests for our components using [Vitest](https:\u002F\u002Fvitest.dev\u002Fguide\u002F) and [Testing Library](https:\u002F\u002Ftesting-library.com\u002F) to ensure that our components function as expected. This is crucial for a professional component library that remains reliable over time.\n5. **Delivering Components Continuously and with Confidence**: We'll establish a continuous integration process to ensure that our components are always delivered with confidence. For every commit of every branch, we'll run our tests to verify everything is functioning correctly. We'll also establish a release process to publish our components to npm.\n6. **Creating a Playground and a Storybook for Components**: We'll create a playground to test our components in a real environment and a storybook with [Histoire](https:\u002F\u002Fhistoire.dev\u002F) to document them. This will enable us to share our components with other developers and to test them in various scenarios, both in isolation and in combination.\n7. **Enhancing Integration with Unplugin and Nuxt Module**: We'll enhance the integration of our component library with the ecosystem by creating a Vite plugin with [Unplugin Vue Components](https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components) and a [Nuxt module](https:\u002F\u002Fnuxt.com\u002Fdocs\u002Fguide\u002Fgoing-further\u002Fmodules) to utilize our components in a Nuxt application.\n\nBy the end of this series, you will have a complete Vue.js component library that you can use in your projects or share with the community, like a professional developer. Isn't that exciting? 😎\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":1495,"description":1675},{"loc":1681},"series\u002Fthe-complete-guide-to-building-a-vue-js-component-library","The most advanced and professional course to build a Vue.js component library with Vite.",[1688,1689,1690],"vue","vite","component library","2VSSnBlkuSLlt05n9MySLBFt6vBkgd7691eX-TK2cTs",[1693,1697,1701,1705,1706,1710,1714],{"contentId":1694,"title":1549,"description":1695,"path":1696,"part":94,"github":1483,"topics":1483},"1f6cf4af-8637-458d-93f8-52ac60dd8f87","Start your monorepo journey! Learn how to set up a linter and formatter for streamlined coding and organizing your project.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fgetting-started-with-monorepo-linter-and-formatter",{"contentId":1698,"title":1561,"description":1699,"path":1700,"part":166,"github":1483,"topics":1483},"e9f57d8c-a722-4fd7-b326-61af0ea9dbd4","Creating a simple Vue.js button component using Tailwind CSS for styling and variants, along with step-by-step library build configuration using Vite.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fbuilding-a-first-component-with-vuejs-and-tailwind-css",{"contentId":1702,"title":1593,"description":1703,"path":1704,"part":173,"github":1483,"topics":1483},"c2d651b0-4487-472d-b55a-b3cf195bcba8","Learn how to configure TypeScript with multiple contexts and generate types for a library to expose types to developers.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fa-better-development-experience-with-typescript-and-tsconfig",{"contentId":1479,"title":5,"description":1481,"path":1485,"part":189,"github":1483,"topics":1483},{"contentId":1707,"title":1625,"description":1708,"path":1709,"part":194,"github":1483,"topics":1483},"34bd87b4-cefd-44bb-affe-21e5e0fe7d56","Implement a CI pipeline with GitHub Actions and automate the release process to npm for Vue.js component libraries.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fdelivering-components-continuously-and-with-confidence",{"contentId":1711,"title":1631,"description":1712,"path":1713,"part":209,"github":1483,"topics":1483},"53bbc0ae-a7b8-49d3-8ae6-73b585c43efc","Learn how to create a playground and use Histoire to visualize and document your components in both isolated and combined scenarios.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fcreating-a-playground-and-a-storybook-for-components",{"contentId":1715,"title":1643,"description":1716,"path":1717,"part":215,"github":1483,"topics":1483},"8b808104-15ac-42ca-9535-cef0996ddf66","Discover how to seamlessly integrate a Vue.js component library using Unplugin Vue Components and Nuxt Module, enhancing usability and developer experience.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fenhancing-integration-with-unplugin-and-nuxt-module",1795016694771]