Nuxt Test Utils - A Primer to Testing in Nuxt

  Рет қаралды 4,790

Alexander Lichter

Alexander Lichter

Күн бұрын

🃏 Testing is important for any application. And at a certain complexity, manual testing is simply not possible anymore. Luckily, writing Unit Tests and E2E Tests for your Nuxt application isn't as difficult as it may seems. In this video, we will dive into the different testing types and build tests for your demo application in less than 20 minutes!
Key points:
🔧 How to set up @nuxt/test-utils
⌨️ Writing unit tests for our demo application
🚨 Creating different E2E tests, e.g. using Playwright
---
Links:
🔗 Docs - nuxt.com/docs/getting-started...
🔗 Code - github.com/manniL/alexander-l...
🔗 Test Utils Repo: github.com/nuxt/test-utils/
🔗 DejaVue - dejavue.fm/ - ‪@DejaVueFm‬
📺 What is useState - • Why you should use use...
Don't forget to hit that "Subscribe" button, ring the notification bell and give a thumbs up!
🌐 Connect further:
Website: www.lichter.io
Twitter/X: / thealexlichter
Twitch: / thealexlichter
Chapters:
00:00 Intro and Agenda
01:16 Demo App Overview
03:28 Set up Test Utils
05:48 Simple Unit test for the AppNumber Component
08:17 Mocking useState in a Unit Test
10:46 Suggest your scenarios!
11:06 End to End Testing asserting the HTML
14:04 E2E Testing with Playwright
17:30 Setting the test env per file if needed
18:30 Summary
19:23 DejaVue - The Vue Podcast

Пікірлер: 83
@user-qb7yc3qg3b
@user-qb7yc3qg3b 2 ай бұрын
testing api showcase would be great!)))
@TheAlexLichter
@TheAlexLichter Ай бұрын
Noted!
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Like if at least one of your Nuxt projects doesn't have tests... YET
@thetakburger7928
@thetakburger7928 2 ай бұрын
Does it count if I had installed the test utils but was waiting for your video ?
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
@thetakburger7928 only if you add the tests soon 😛
@randomtimessomehow
@randomtimessomehow 2 ай бұрын
I tested it with my own eyes, it'll be fine! (🥲)
@georgespanos4680
@georgespanos4680 2 ай бұрын
Testing some forms in combination with some API calls would also be great!!!
@max06de
@max06de 2 ай бұрын
Having tests is great. Having fast tests is better. Since e2e tests require a running instance, can you give out some tricks how to improve the startup speed? (For you it's 4.x seconds, my app already takes ~20 seconds on my machine. And for a coworker, we're talking about 90 seconds...)
@ragura
@ragura 2 ай бұрын
There’s no way around spinning up a new Nuxt instance for separate tests that I know of; the whole point is to have them as isolated units. If you really wanted to you could test more things inside one test (even with Playwright by navigating to another page), but I’d consider that bad practice. I don’t know how many tests you have, but even 90 seconds isn’t too bad for something that should only run on pre-commit and in CI. If you’re writing tests, add “.only” to the test you’re writing to only test that specific test instead of the whole suite.
@joshdeltener2012
@joshdeltener2012 14 күн бұрын
Well, you can at least run more than 1 Playwright test in parallel.
@luc122c
@luc122c 2 ай бұрын
Wow, the testing tools have come a long way since I last tried to setup testing in a Nuxt app. Well done Nuxt team 👏
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Will forward that 🙌
@MartinMasevski
@MartinMasevski 2 ай бұрын
I absolutely love this kind of videos! Please make more content for tests
@TheAlexLichter
@TheAlexLichter Ай бұрын
Coming! 🙌🏻
@CJohnD
@CJohnD 9 күн бұрын
Great job explaining everything as always. Please consider some unit testing that includes Pinia as well. Waiting for part 2!
@TheAlexLichter
@TheAlexLichter Күн бұрын
Thank you 🙏🏻 Will definitely get back to it and add a more thorough example/video!
@scottyzen88
@scottyzen88 2 ай бұрын
Your killing it with these videos Alex. Keep them coming 👍🏻
@TheAlexLichter
@TheAlexLichter Ай бұрын
Thank youu! Will do 🙏🏻
@gro967
@gro967 2 ай бұрын
Thanks for actually showing the most important, but also hardest part with Nuxt testing, which is the e2e. Unit testing and API testing is really easy with vitest, but this one is always something where you don't know where to start.
@TheAlexLichter
@TheAlexLichter Ай бұрын
Especially in an existing application it is tricky and you'd want to start "somewhere" with it. But the setup part (staging data, staging env etc etc) is commonly the most difficult one.
@alexcroox
@alexcroox 2 ай бұрын
Thank you for this!
@DavidDeSloovere
@DavidDeSloovere 2 ай бұрын
You're on a roll. At this rate Nuxt will need new features for you to cover in the videos 🙂. Thanks again for the video. For a primer, it's great to see it as a video to get a quick idea on how to start.
@TheAlexLichter
@TheAlexLichter Ай бұрын
Haha, I am afraid I won't be able to keep up with the speed of new Nuxt + UnJS features 😛 Glad it was useful 🙏🏻
@miguelcassimiro8103
@miguelcassimiro8103 2 ай бұрын
Congrats for the Thesis defence. Nice to see that and the new video also. Tests are something welcome to discuss and with Nuxt even better.
@TheAlexLichter
@TheAlexLichter Ай бұрын
Thank you 🙏🏻 Was about time 🙊 Glad you enjoyed the video!
@RobKrater
@RobKrater 2 ай бұрын
Another great video. Thanks as always
@TheAlexLichter
@TheAlexLichter Ай бұрын
My pleasure!
@nangseakheng2976
@nangseakheng2976 2 ай бұрын
Really appreciate your contents teaching test components in Nuxtjs and I hope you can create new video about hosting Nuxtjs with docker using Bun package manager as well can't wait your new video
@mattroyer
@mattroyer 2 ай бұрын
I agree!
@TheAlexLichter
@TheAlexLichter Ай бұрын
Sure I will!
@youhan96
@youhan96 2 ай бұрын
Congrats!!
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Thanks a lot 🙏🏻
@eothica
@eothica Ай бұрын
You make it look so easy! Would really, really like to know how you'd test: - A component that depends on API endpoint data from a parent component. - A Component requiring props - Deeply nested components that is dependant on their parent. - Mocking the router and using registerEndpoint (how to mock endpoint data?) - Whats the difference between renderSuspended, mountSuspended, mount and when do you use each? I got so many question man. And waiting for VueSchools "Use Vitest with Nuxt 3" to come out too. even though it has been in progress for a long time. I really really appriciate these videos and all the work you guys do! It's so amazing what people are able to build because of this.
@rayanazzam7225
@rayanazzam7225 2 ай бұрын
I used composables to separate my logic from ui, can you please show case of fetching data inside composable with custom fetch function 😵‍💫😵‍💫
@TheAlexLichter
@TheAlexLichter Ай бұрын
You mean as in kzbin.info/www/bejne/oImraYeoYsqrnqs 👀
@rayanazzam7225
@rayanazzam7225 Ай бұрын
Yes exactly i meant if you can show how to test such like thing that in the video 😃😃
@mattroyer
@mattroyer 2 ай бұрын
Thanks for the tutorial! Would love to see a walkthrough on implementing pinia vs useState for global state management
@TheAlexLichter
@TheAlexLichter Ай бұрын
Briefly covered it in kzbin.info/www/bejne/o6eTiJZ4dqeWf80 Anything besides that you are interested in? 😋
@oscarrafaelcampos
@oscarrafaelcampos 2 ай бұрын
Hello Alex, thank you for this video, it will be very useful. Of all the videos you've made, do you have any that explain in practice the use of the .client or .server suffixes in components?
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Not yet! Server components will be covered in the future though and „client only“ components might be an own video too 🙏🏻
@jecht94
@jecht94 Ай бұрын
Would be great to see testing with Pinia integration!
@milos018
@milos018 2 ай бұрын
Showing us your way on how to set /api endpoints would be great as well as Pinia store! Tnx, Milos
@TheAlexLichter
@TheAlexLichter Ай бұрын
Will come ✅
@fmoessle
@fmoessle 2 ай бұрын
Great vid, I haven’t checked out testing with nuxt with something other than cypress. Would have loved to see playwright with an open browser. Now I have to test it for myself 🤓
@TheAlexLichter
@TheAlexLichter Ай бұрын
Hah, good idea for next time! Should've done taht 😋
@AllexSise
@AllexSise 28 күн бұрын
Any advice on how to run it showing the browser?
@TheAlexLichter
@TheAlexLichter 28 күн бұрын
@AllexSise sure! Just set headless: false in the playwright browser launch options
@AllexSise
@AllexSise 27 күн бұрын
@@TheAlexLichter thanks, got it working: edit vitest.config.ts import { defineVitestConfig } from '@nuxt/test-utils/config' export default defineVitestConfig({ test:{ browser:{ enabled: true, name: 'chrome', headless: false } } })
@ribl1000
@ribl1000 2 ай бұрын
You did it😘
@louis4757
@louis4757 2 ай бұрын
For me mocking the VueRouter and testing with useRoute was difficult. Maybe you can do some content about that. Loved this video, very informative❤
@TheAlexLichter
@TheAlexLichter Ай бұрын
Noted 👍🏻 Glad you enjoy them ☺️
@Gaijin101
@Gaijin101 2 ай бұрын
❤❤❤
@codybontecou
@codybontecou 2 ай бұрын
OOoo yea! This is the content I want
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Glad to hear Cody 🎉
@MGJoe93
@MGJoe93 2 ай бұрын
Congratulations on completing your thesis! Will it be publicly available? I'm very interested in reading it :)
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Thank you so much! Yes, it will be in the future! Will give you a ping.
@ragura
@ragura 2 ай бұрын
Finally got around to watching this video. Good introduction to the basics and I like that you skipped the whole testing philosophy explanation many other videos get into before finally showing how to actually write tests. I’m actually a bit of a test writing veteran because of my job (Django tests) and the only thing that still confuses me about Nuxt testing specifically is how to bypass middleware that uses session data, for example in the case of an authentication route guard. It becomes even more complicated when the functionality is abstracted away to some library, like for example Superbase auth functions and Pinia stores storing user data. Is the best approach to mock all of these functions and even the Pinia stores?
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Yeah, I try to keep the lenght still reasonable, with testing philosophy & more we'd be easily 30+ minutes :D I think for bypassing: You can use import.meta.test to check if you are in a test environment 😊
@ManpreetSingh-jy9jc
@ManpreetSingh-jy9jc 2 ай бұрын
@@TheAlexLichter I am currently checking process.env.NODE_ENV which is set to 'Test' when test cases are run to bypass some checks. For session cookies, I am extracting cookie from the login response, saving it in a variable and sending it in subsequent requests
@fmoessle
@fmoessle 2 ай бұрын
I'm a sucker for the thumbnails 😂
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Haha, thank you 🙊 Glad you enjoy them!
@VitorNeves
@VitorNeves 2 ай бұрын
Hi, we can set a default the environment on vitest.config and then when needed we can overide it directly on the file, right?
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Yes, absolutely 🙌
@HadeelYassin-jy9gk
@HadeelYassin-jy9gk 6 күн бұрын
is there a way we can skip mock auto imports for nuxt core composables, or it's a must ?
@TheAlexLichter
@TheAlexLichter 6 күн бұрын
Depends a lot on your test case. I'd mock as little as possible but e.g. for randomness and similar it is more or less necessary
@HadeelYassin-jy9gk
@HadeelYassin-jy9gk 6 күн бұрын
@@TheAlexLichter thanks for your reply, but i am currently using nuxt core auto-imports (utils/composables), like definePageMeta and vitests is failing due to undefined definePageMeta, how can i make those core auto imports recognized by vitest, appreciate your help
@TheAlexLichter
@TheAlexLichter 6 күн бұрын
that *should* work out of the box when following the guide (setting the right vitest environment is important here!)
@DanielMunozMartin-wz4st
@DanielMunozMartin-wz4st 2 ай бұрын
I'm having trouble implementing this in the nuxt layers, does it happen to anyone else?
@cumacos65
@cumacos65 Ай бұрын
Do you have an example of Nuxt configuration with MSWjs Mock (Service Worker) ??
@TheAlexLichter
@TheAlexLichter Ай бұрын
Nope, didn't use MSW with Nuxt yet. But just put it on my list 👍🏻
@cumacos65
@cumacos65 Ай бұрын
@@TheAlexLichter Please how do I put it on your list? That tool is very useful, and with Nuxt even more so.
@TheAlexLichter
@TheAlexLichter Ай бұрын
Sorry, I meant that I just put it on my list of topics 😁 So you kinda did that already 😛
@cumacos65
@cumacos65 Ай бұрын
@@TheAlexLichter Wow, great. Thanks in advance !!
@muhammadrizkiwahyudi3334
@muhammadrizkiwahyudi3334 2 ай бұрын
Can you make video about nuxt 3 + prisma and how to setup on production?
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Might use it in a stream soon!
@muhammadrizkiwahyudi3334
@muhammadrizkiwahyudi3334 2 ай бұрын
@@TheAlexLichter 🔥🔥🔥
@svenvanreenen
@svenvanreenen 2 ай бұрын
Congrats with the Thesis, when will you share your conclusions😋
@TheAlexLichter
@TheAlexLichter 2 ай бұрын
Thank youu 🙏 The thesis will be submitted to an academic conference. After that I will 🙌
@jonileih
@jonileih Ай бұрын
Glückwunsch zur Thesis - Bachelor oder Master?
@TheAlexLichter
@TheAlexLichter Ай бұрын
Danke! Weder noch, Diplom 👀
@taoufiqnaciri8345
@taoufiqnaciri8345 Ай бұрын
Can you do a server api routes test please 🙂
@TheAlexLichter
@TheAlexLichter Ай бұрын
On the list ✅ Any specifics with regards to server api routes testing you want to know?
Aliases in Nuxt 3  (+ raffle 🎁)
13:00
Alexander Lichter
Рет қаралды 2,2 М.
Organize your Composition API code (2k subscriber special)
18:22
Alexander Lichter
Рет қаралды 19 М.
1 класс vs 11 класс  (игрушка)
00:30
БЕРТ
Рет қаралды 4 МЛН
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 124 МЛН
Balloon Stepping Challenge: Barry Policeman Vs  Herobrine and His Friends
00:28
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 101 МЛН
Avoid losing Reactivity in your Vue Application
10:32
Alexander Lichter
Рет қаралды 6 М.
Stop Writing So Many Tests
10:02
Web Dev Simplified
Рет қаралды 81 М.
The BEST way to proxy your API in Nuxt
21:12
Alexander Lichter
Рет қаралды 12 М.
How to narrow and chain your Playwright locators
5:31
Checkly
Рет қаралды 1 М.
Why you should use useState()
17:57
Alexander Lichter
Рет қаралды 11 М.
That's NOT How Async And Await Works in .NET!
12:25
Codewrinkles
Рет қаралды 15 М.
You're Probably Using Nuxt Wrong
5:16
LearnVue
Рет қаралды 13 М.
🚀  TDD, Where Did It All Go Wrong (Ian Cooper)
1:03:55
DevTernity Conference
Рет қаралды 550 М.
Vapor: The Future Of Vue
21:27
Theo - t3․gg
Рет қаралды 110 М.
MacBook Air Японский Прикол!
0:42
Sergey Delaisy
Рет қаралды 565 М.
WWDC 2024 - June 10 | Apple
1:43:37
Apple
Рет қаралды 10 МЛН
Как работает автопилот на Lixiang L9 Max
0:34
Семен Ефимов
Рет қаралды 17 М.