Testing Serverless Functions with Jest & Next.js API Routes

  Рет қаралды 8,405

Colby Fayock

Colby Fayock

3 жыл бұрын

Learn how to use Jest to test serverless functions using Next.js API routes. We'll walk through spinning up a new demo project to test with, installing and configuring Jest, and writing tests for a serverless function.
Next.js API Route Demo Starter
github.com/colbyfayock/demo-n...
🗒️ Read More
spacejelly.dev/posts/how-to-t...
🎬 Next Up
How to Use Postman to Test APIs and Manage Collections
• How to Use Postman to ...
Visual Regression Testing Tutorial with Applitools Eyes and Cypress
• Visual Regression Test...
How to Use Github Actions to Automate Tests and Slack Notifications
• How to Use Github Acti...
🔔 Subscribe for more tech and developer videos
kzfaq.info?s...
🐦 Get updates straight to your Twitter @colbyfayock
/ colbyfayock
📸 Catch the next stream live on Twitch @colbyfayock
/ colbyfayock
✉️ Or a newsletter right to your inbox!
www.colbyfayock.com/newsletter/
💝 Sponsor me for more free content like this!
GitHub: github.com/sponsors/colbyfayock
Other: hello@colbyfayock.com
👨‍🚀 Brought to by colbyfayock.com
www.colbyfayock.com
🎥 Want to know what A/V equipment I use?
www.colbyfayock.com/what-i-use
🧰 More Resources
Next.js API Route Demo Starter
github.com/colbyfayock/demo-n...
Next.js API Routes
nextjs.org/docs/api-routes/in...
Jest
jestjs.io/
@babel/plugin-transform-modules-commonjs
babeljs.io/docs/en/babel-plug...
🎼 Music
Music from Uppbeat (free for Creators!):
uppbeat.io/t/mountaineer/balcony
License code: BVUDPXZUFNDGNZ0H
#colbyfayock #jest #nextjs #testautomation #serverless #apis #webdevelopment

Пікірлер: 39
@colbyfayock
@colbyfayock 7 күн бұрын
Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course
@jasonden1005
@jasonden1005 2 жыл бұрын
Thank you for walking through and making the points easy to understand. It’s enjoyable.
@colbyfayock
@colbyfayock 2 жыл бұрын
glad it was able to help!
@roymathew7956
@roymathew7956 6 ай бұрын
Thank you Colby for your very clear articulation. I was struggling with understanding why I was getting the error you covered here.
@colbyfayock
@colbyfayock 6 ай бұрын
glad it was able to help!
@c4346
@c4346 3 жыл бұрын
very interesting, thank you for this tutorial
@colbyfayock
@colbyfayock 3 жыл бұрын
No problem 😁
@ARSHADKHAN-hc6pb
@ARSHADKHAN-hc6pb 3 жыл бұрын
Great video 👍
@colbyfayock
@colbyfayock 3 жыл бұрын
Thank you! 🙏
@darkfoxwillie
@darkfoxwillie 2 жыл бұрын
thanks for posting the code :)
@colbyfayock
@colbyfayock 2 жыл бұрын
No problem!
@JamesQQuick
@JamesQQuick 2 жыл бұрын
Awesome!,
@colbyfayock
@colbyfayock 2 жыл бұрын
thank you!!!
@josemfcheo
@josemfcheo 2 жыл бұрын
Bro, I love you...
@colbyfayock
@colbyfayock 2 жыл бұрын
♥️
@paradise5534
@paradise5534 3 ай бұрын
ur the goat!
@colbyfayock
@colbyfayock 3 ай бұрын
🙏
@andrewiglinski148
@andrewiglinski148 Жыл бұрын
Aw dude I'd kiss you right now if you were here... When I first became a pretty capable dev I asked this senior dude at a massive company by me what's the best advice he had for someone trying to become a senior dev. He told me testing and GraphQL (this was a while ago). I mastered GraphQL that weekend and since then I've became pretty capable with a couple more languages... and I've still never written a single test.
@colbyfayock
@colbyfayock Жыл бұрын
haha yeah, tests are one of those things where you don't need it until you really need it, and by then it might be too late. it's a good habit to get into but its not something id fall on my sword for. priorities are important to consider and the level of impact particular pieces have. if im writing logic that handles everything for taking people's money in an online store, id likely wanna have tests of that. if i'm building mission-critical UI for scientists or people saving other's lives, id probably wanna have tests for that. if im building a notetaking app and trying to scale it fast, meh, those could probably wait, but thats a tradeoff risk anyways glad this video helped :)
@chrisl2773
@chrisl2773 Жыл бұрын
This is useful and all, but how would you go about testing for calls to a database? For example, I have a Postgres Database that my Next.JS' backend will need to make a call to? I've been trying to get it to work with a multitude of different packages but still not finding any success
@colbyfayock
@colbyfayock Жыл бұрын
typically you woudl "mock" those requests so that you're not actually making real requests / mutations to the database. for instance: jestjs.io/docs/mock-functions#mocking-modules
@chrisl2773
@chrisl2773 Жыл бұрын
@@colbyfayock I see, I'll give it a look, thank you for the response! :D
@deepakn8581
@deepakn8581 2 жыл бұрын
When I import the handler file and console log for me that's showing undefined instead of handler function.
@colbyfayock
@colbyfayock 2 жыл бұрын
hey not sure about this one, the handler is just an export javascript module so there shouldnt be anything special going on. here's my code github.com/colbyfayock/my-next-jest
@colbyfayock
@colbyfayock Жыл бұрын
Get fresh tutorials and other free content straight to your inbox! colbyfayock.com/news
@jerbparagas3924
@jerbparagas3924 6 ай бұрын
Can you do Next14 version REST API route test tutorial with jest?
@colbyfayock
@colbyfayock 6 ай бұрын
just to clarify what you're looking for, basically this same video but for the App router? are you stuck with Jest? I likely would do a newer video with vitest, though i would expect for the most part there should be enough similarities to follow along
@jerbparagas3924
@jerbparagas3924 6 ай бұрын
@@colbyfayock Yes, app router, I'm new to testing but I would like to start testing rest api endpoints. What can you recommend for a beginner, vitest or jest?
@colbyfayock
@colbyfayock 6 ай бұрын
@@jerbparagas3924the community seems to be largely pooling support around vitest, i think it is a great option. this video and all tests ive run so far in Jest pretty much "just work" with vitest "as is" with the exception of just needing to import some of the functions that jest would auto include, if that makes sense check this out: vitest.dev/guide/ but seems like i need to make a vitest video generally
@27sosite73
@27sosite73 8 ай бұрын
damn is it me or this is only one video on this topic in the hole youTube?
@colbyfayock
@colbyfayock 8 ай бұрын
👀 glad i was able to help
@blahblah-rn1ts
@blahblah-rn1ts 2 жыл бұрын
why were those serverless functions?
@colbyfayock
@colbyfayock 2 жыл бұрын
can you elaborate? do you mean as opposed to running it on a traditional server?
@blahblah-rn1ts
@blahblah-rn1ts 2 жыл бұрын
@@colbyfayock A function is not intrinsically serverless. It becomes serveless by way of deployment and execution. At the moment of you testing it it's just a function.
@colbyfayock
@colbyfayock 2 жыл бұрын
@@blahblah-rn1ts thanks for the heads up!
@blahblah-rn1ts
@blahblah-rn1ts 2 жыл бұрын
@@colbyfayock The content of the video was great, though. Thank you for that. Would also love to see something more close to reality where you'd had to deal with database layer. (e.g. via Prisma)
@RdozeTV
@RdozeTV 2 жыл бұрын
I hate that kind of tutorial. Can you make real tutorial, complex application and not a calculator test
@colbyfayock
@colbyfayock 2 жыл бұрын
thanks for the feedback
@Hyokora
@Hyokora Жыл бұрын
tf
The Story of Next.js
12:13
uidotdev
Рет қаралды 560 М.
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 15 МЛН
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 9 МЛН
Mocking Asynchronous Functions with Jest
21:50
Swashbuckling with Code
Рет қаралды 68 М.
Upload Images to Cloudinary in React & Next.js
18:37
Colby Fayock
Рет қаралды 33 М.
Stop Writing So Many Tests
10:02
Web Dev Simplified
Рет қаралды 85 М.
The ONLY REASON To Unit Test
8:26
Theo - t3․gg
Рет қаралды 75 М.
Build Apps Faster with daisyUI Tailwind Components
7:43
Colby Fayock
Рет қаралды 19 М.
Mocking a Database in Node with Jest
13:29
Sam Meech-Ward
Рет қаралды 65 М.
Mastering Unit Testing in Next JS & TypeScript | Portfolio App #3
52:49
React Testing Crash Course with Next.js
2:57:07
Anson the Developer
Рет қаралды 29 М.
разбил телефон из-за видео
0:15
STANISLAVSKIY Hi
Рет қаралды 751 М.
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 12 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,8 МЛН
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,6 МЛН