<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chayut Ruksomya on Medium]]></title>
        <description><![CDATA[Stories by Chayut Ruksomya on Medium]]></description>
        <link>https://medium.com/@chayutruksomya?source=rss-14f2e9a6723d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CJetY3UDTg_ctfiK.</url>
            <title>Stories by Chayut Ruksomya on Medium</title>
            <link>https://medium.com/@chayutruksomya?source=rss-14f2e9a6723d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 00:22:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chayutruksomya/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[มาวางโครงสร้าง Project Next JS + TypeScript กันเถอะ]]></title>
            <link>https://medium.com/bluepi-engineer/%E0%B8%A1%E0%B8%B2%E0%B8%A7%E0%B8%B2%E0%B8%87%E0%B9%82%E0%B8%84%E0%B8%A3%E0%B8%87%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-project-next-js-typescript-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-9f5d628b5ef0?source=rss-14f2e9a6723d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f5d628b5ef0</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[boilerplate]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Chayut Ruksomya]]></dc:creator>
            <pubDate>Fri, 01 Jan 2021 09:30:22 GMT</pubDate>
            <atom:updated>2021-01-18T04:59:51.823Z</atom:updated>
            <content:encoded><![CDATA[<h3>มาวางโครงสร้าง Project Next JS + TypeScript และ Jest กันเถอะ</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*rlECGadHnhCRXw-YYs9usg.jpeg" /></figure><p>สวัสดีครับ ท่านผู้เจริญทั้งหลาย วันนี้ ผมขอเสนอแนวคิด การวางโครงสร้าง Project Next JS + TS กันนะครับ เพื่อไม่ให้เป็นการเสียเวลา เรามาเริ่มกันเลย โดยจะมีขั้นตอนดังนี้</p><h4>สิ่งที่ควรรู้ก่อน</h4><ul><li>NPM / YARN package manager tool</li><li>TypeScript</li><li>React JS</li><li>Jest test runner</li></ul><h4><strong>ติดตั้ง </strong><a href="https://nextjs.org/docs/getting-started"><strong>Next JS</strong></a><strong> ก่อนเลยครับ (ในบทความนี้ จะใช้ </strong><a href="https://yarnpkg.com/"><strong>yarn </strong></a><strong>นะครับ)</strong></h4><pre>yarn create next-app</pre><p>จาก script ดังกล่าวจะมีการถามชื่อ Project ที่ต้องการจะสร้าง ให้ใส่ชื่อ ตามที่ต้องการเลยครับ (ในบทความนี้ จะใช้ชื่อ <strong>next-ts-jest-starter</strong>) และด้วยความที่ Next JS นั้น support TypeScript อยู่แล้ว ต่อไปจะเป็นการ เปลี่ยน Project ที่ เพิ่งสร้างเสร็จไปเป็น TypeScript นะครับ</p><h4><strong>ติดตั้ง TypeScript และ สร้างไฟล์​ </strong><strong>tsconfig.json ขึ้นมา เพื่อให้ Next JS รู้ว่า</strong><a href="https://nextjs.org/docs/basic-features/typescript"><strong>ต้องการ จะใช้ </strong>TypeScript</a></h4><pre>cd <strong>next-ts-jest-starter</strong></pre><pre>touch tsconfig.json</pre><pre>yarn add --dev typescript <a href="http://twitter.com/types/react">@types/react</a> <a href="http://twitter.com/types/node">@types/node</a></pre><p>ก่อนที่จะ run server ขึ้นมาให้ลอง เปิด project ขึ้นมา ด้วย IDE อะไรก็ได้ดูครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*QEySXvHb10jzhTBcUksbxA.png" /></figure><p>แล้วลอง แก้นามสกุลไฟล์ ใน folder page component จาก *.js/*.jsx เป็น *.tsx ดู</p><h4>Run server ด้วย dev script ได้เลย</h4><pre>yarn dev</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EnBhRzSD6npYSHDdlso5Dw.png" /></figure><p>ลองเปิด browser แล้วไปที่ url <a href="http://localhost:3000/">http://localhost:3000/</a> ก็จะพบว่า Web เรานั้นใช้ได้โดยปกติ 🥳</p><blockquote>สังเกตได้ว่า Next JS จะมา overwrite ไฟล์ <strong>tsconfig.json</strong> ของเราให้มี config พื้นฐานที่จำเป็น และอีกไฟล์ ที่ถูกสร้างขึ้นมาก็คือ <strong>next-env.d.ts</strong> เพื่อเอาไว้ refer Next JS module กับตัว TypeScript Compiler ครับ (ห้ามลบไฟล์นี้ออกนะ!)</blockquote><h4>ติดตั้ง ESLint โดยจะใช้ Airbnb JavaScript Style Guide</h4><pre>yarn add --dev eslint eslint-config-airbnb-typescript</pre><pre>yarn add --dev eslint-plugin-import@^2.22.0 \<br>            eslint-plugin-jsx-a11y@^6.3.1 \<br>            eslint-plugin-react@^7.20.3 \<br>            eslint-plugin-react-hooks@^4.0.8 \<br>            @typescript-eslint/eslint-plugin@^4.4.1</pre><h4>Linter config</h4><p>Airbnb’s ESLint config ใช้ได้ทั้ง .ts .tsx หรือ แม้แต่ JS ไฟล์ ก็ยังใช้ได้</p><p>การลง eslint-config-airbnb-typescript ในโปรเจค จะเป็นการใส่ config ต่างตาม dependency ด้านล่างนี้ :</p><ul><li><a href="https://airbnb.io/javascript">Airbnb JavaScript Style Guide</a></li><li><a href="https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb">eslint-config-airbnb</a></li><li><a href="https://github.com/typescript-eslint/typescript-eslint">typescript-eslint</a></li></ul><h4>ขั้นตอนถัดไป ให้สร้าง ไฟล์ config ของ ESLint นะครับ</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zwGV_iPtQKsDuUxIYo9dMQ.png" /><figcaption>.eslintignore เอาไว้ บอกให้ ESLint ไม่ต้อง run ตามไฟล์ที่ระบุไว้</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-5IHsipTaX2TZv9UbdWiGQ.png" /><figcaption>.eslintrc.json เป็นไฟล์สำคัญที่เอาไว้บอกว่า ESLint ต้องใช้ standard ไหน มี rule ต่างๆ ยังไงบ้างซึ่งสามารถ customize ได้ทีหลังครับ</figcaption></figure><p>ตอนนี้ จะได้ ไฟล์ ดังรูปด้านล่าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*6VTQtKoBR8XE-g_-pr9NZw.png" /></figure><h4>เอาละ ทีนี้มาลอง run lint ให้มัน เช็ค syntax ของเรากันเถอะ</h4><pre>yarn eslint .</pre><blockquote>คำสั่งด้านบน จะ run lint ทั้ง project ของเรายกเว้น ไฟล์ที่ list ไว้ใน .eslintignore</blockquote><p>ผลลัพธ์ที่ได้คือ Error บานเลยจ้า นี่ขนาดไฟล์ตั้งต้นนะ 5555 ค่อยๆเขียนแก้ไปเรื่อยๆนะครับ ✌️</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bPssi3zpiK3jgMT2KY3Erg.png" /></figure><p>เมื่อแก้เสร็จเรียบร้อย ให้ run อีกรอบ จนเห็น แต่ warning ไม่มี error ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ayib_hpBvZ2MwoJucnHxSQ.png" /></figure><h4>ติดตั้ง <a href="https://github.com/facebook/jest">Jest</a> เป็น test runner และ <a href="https://github.com/enzymejs/enzyme/">Enzyme</a> สำหรับ เขียน Test React component กันต่อเลย</h4><blockquote>สำหรับคนที่ไม่อยากใช้ Enzyme สามารถใช้ <a href="https://github.com/testing-library/react-testing-library"><strong>react testing library</strong></a> ก็ได้นะครับ</blockquote><p>ติดตั้ง jest พร้อมกันกับ Type Definition</p><pre>yarn add --dev jest babel-jest ts-jest @types/jest</pre><p>ติดตั้ง Enzyme กันต่อเลยครับ (สำหรับใครที่ ใช้ <a href="https://github.com/testing-library/react-testing-library">react testing library</a> ให้ไปลองลงเอาเองนะครับ 555+</p><pre>yarn add --dev enzyme enzyme-adapter-react-16 enzyme-to-json @types/enzyme @types/enzyme-adapter-react-16</pre><h4>ก่อนจะเริ่มเขียน Test เรามาใส่ config กันก่อนดีกว่า</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TRxZ61Z-qt1yfnA9A_tp9w.png" /><figcaption>jest.config.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jD1W2s099psOWlfmsX_cJQ.png" /><figcaption>config/setup.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D-Tbfdzxuhdwqy6xo_fMXw.png" /><figcaption>jest.tsconfig.json</figcaption></figure><p>โดยจะมี Path ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*or6hIkbpqqXgXXkUl63AyA.png" /></figure><p>คำอธิบาย :</p><ul><li>jest.config.js เป็นไฟล์กำหนด <a href="https://jestjs.io/docs/en/configuration">config ต่างๆ ของ jest</a></li><li>config/setup.js ไฟล์นี้จะถูก run ก่อนเข้า test ทุกรอบที่ run test ซึ่งเราจะเอาไว้ผูก Jest กับ Enzyme กันที่นี่</li><li>jest.tsconfig.json สำหรับ transform code จาก TS ไปเป็น JS ก่อนที่จะ run test</li></ul><h4>เมื่อมีไฟล์ config พร้อมแล้ว เราลองมา เขียน component และลอง run test กันเถอะ</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pkh1s4qcVNm_gn0nHAKZbQ.png" /><figcaption>React Component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGYZs42wu9l4M56T2Et_VA.png" /><figcaption>Test File</figcaption></figure><h4>เมื่อสร้างไฟล์ Test แล้ว ให้ run ดูครับ</h4><pre>yarn jest</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CvF4AW7BPtMAsr_cIUZCLA.png" /></figure><blockquote>ข้อสังเกตุ ในบรรทัดที่ 17 ของ Test File จะมีการ <a href="https://jestjs.io/docs/en/snapshot-testing">check snapshot</a> ด้วย เพราะเราติดตั้ง <em>snapshotSerializers: [‘enzyme-to-json/serializer’] ไว้เรียบร้อยแล้ว</em></blockquote><h4>สุดท้ายเรามาลองวางโครงสร้าง folder กันดูครับ</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*ivpwCVI9ukjfdtMbjuGOvA.png" /><figcaption>Folder Structure</figcaption></figure><h4>คำอธิบาย :</h4><ul><li>components— ไว้ใส่ React component ของเรา</li><li>config — เก็บ config ไฟล์ ต่างๆ ตัวอย่างเช่น config ของ jest</li><li>contexts — สำหรับเก็บไฟล์ ที่ใช้เก็บ <a href="https://reactjs.org/docs/context.html">React context</a> ครับ</li><li>documents — สำหรับเอาไว้เก็บ wiki ไฟล์ ของ project พวก .md ไรงี้</li><li>hooks — เอาไว้เก็บไฟล์ <a href="https://reactjs.org/docs/hooks-custom.html">custom hook</a> ของเราครับ</li><li>models — สำหรับเก็บ type หรือ interface ต่างๆ ที่ใช้ใน project ได้ครับ</li><li>services — ที่นี่เราจะเก็บ ไฟล์ที่เอาไว้ยิง AJAX / API ต่างๆ ข้างนอก</li><li>utils — สำหรับใส่ไฟล์ ที่เป็น function กลางหรือ utility function ครับ</li></ul><h3>ทิ้งท้าย</h3><p>เท่าที่ผมลอง เล่นมา เรายังสามารถ ใส่ option หรือ dependency อื่นๆได้เพิ่มอีก เช่น</p><ul><li><a href="https://github.com/typicode/husky">Husky</a> สำหรับ trigger run test หรือ lint ก่อนที่จะ commit/push</li><li><a href="https://styled-components.com/docs/api#typescript">Styled-Component</a> เพื่อเขียน css ใน react และ <a href="https://github.com/styled-components/jest-styled-components">jest-styled-components</a> สำหรับ Test Styled Component ด้วยกัน</li><li>Sass module ซึ่ง เป็นแบบ <a href="https://nextjs.org/docs/basic-features/built-in-css-support#sass-support">built in ของ Next JS</a> อยู่แล้ว</li><li>i18n สำหรับทำ Translation แบบ <a href="https://nextjs.org/docs/advanced-features/i18n-routing">built in ของ Next JS</a> เช่นกัน</li></ul><p>สุดท้ายนี้ก็ขอขอบคุณ ที่อ่าน หรือ ทำตาม จนจบนะครับ 🙏 ผมได้ push code ขึ้น Github เรียบร้อย ตามไปเล่นต่อกันได้เลยที่</p><h4><a href="https://github.com/tomhero/next-ts-jest-starter">https://github.com/tomhero/next-ts-jest-starter</a></h4><h4>References:</h4><ul><li><a href="https://dev.to/oahehc/how-to-config-react-project-with-next-js-typescript-tslint-and-jest-11l0">https://dev.to/oahehc/how-to-config-react-project-with-next-js-typescript-tslint-and-jest-11l0</a></li><li><a href="https://qiita.com/keitakn/items/0a714997eb058f2f67e2">https://qiita.com/keitakn/items/0a714997eb058f2f67e2</a></li><li><a href="https://github.com/typescript-eslint/typescript-eslint">https://github.com/typescript-eslint/typescript-eslint</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f5d628b5ef0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bluepi-engineer/%E0%B8%A1%E0%B8%B2%E0%B8%A7%E0%B8%B2%E0%B8%87%E0%B9%82%E0%B8%84%E0%B8%A3%E0%B8%87%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-project-next-js-typescript-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-9f5d628b5ef0">มาวางโครงสร้าง Project Next JS + TypeScript กันเถอะ</a> was originally published in <a href="https://medium.com/bluepi-engineer">bluePi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>