Unit Testing with Mocha and Chai [Part 1/2]

  Рет қаралды 31,598

Code With Bubb

Code With Bubb

Күн бұрын

In this video we'll take a look at Unit testing with Mocha and Chai.
Get the code here: github.com/codebubb/unit-test...
You can either see the completed code or at the start of the lesson
git checkout lesson-01-start
git checkout lesson01-complete
Unit testing with Mocha and Chai
-----------------------------------------------------
00:00 Introduction
00:50 Installing dependencies
01:50 Setting up
03:23 Writing first unit test
06:01 Chai
07:26 Useful unit tests
11:32 Exercise
13:05 Using nyc
- Follow Me -
Twitter: / codebubb
Facebook: / juniordevelopercentral
Blog: www.juniordevelopercentral.com/
- Thanks! -
This JavaScript tutorial is intended as a beginners guide to unit testing with Mocha and Chai.
In the lesson, we'll take a look at getting a project set up with the Mocha and Chai dependencies and we'll also install nyc for code coverage.
To start off we'll write your first unit test and see how a simple function can be testing with the basic built in Node.js Assert package and then we'll swap this out for the more power assertions available in the Chai library.
Once we've written our first unit test, we'll expand on this to create a more robust function and cover different scenarios of how the function should behave.
I'll then set you a challenge to see if you can can setup a new function and write unit tests with Mocha and Chai.
Once we've got some good unit tests set up we'll take a look at how you can use nyc to generate code coverage reports which will give you an overview of how much of your code you have covered and also which bits of code still need to be tested. Channel Handle @codebubb

Пікірлер: 39
@alexanderboji7154
@alexanderboji7154 3 жыл бұрын
Awesome ! Thank you. This video and the next one are exactly what I was looking for
@codewithbubb
@codewithbubb 3 жыл бұрын
Great! Hopefully you'll be up and running with unit testing now!
@facelessvoid4657
@facelessvoid4657 Жыл бұрын
Awesome you did my day! Your video helped me with "function is not a function" error. Thank you very much!
@anastasiiazu
@anastasiiazu 2 жыл бұрын
Thank you so much! You explained it like a pro!
@doychindarakchiev7880
@doychindarakchiev7880 2 жыл бұрын
Thank you! I`ve been soo lost with other exponaitions,but yours is top!
@codewithbubb
@codewithbubb 2 жыл бұрын
Thank you very much!
@thomaswyee
@thomaswyee 3 жыл бұрын
Awesome tutorials man, subscribed! Keep up the great work.
@codewithbubb
@codewithbubb 3 жыл бұрын
Thanks very much Thomas! Hope you found it useful.
@tracetate2832
@tracetate2832 3 жыл бұрын
a tip: you can watch movies at flixzone. Been using it for watching all kinds of movies recently.
@connerayden9090
@connerayden9090 3 жыл бұрын
@Trace Tate yea, I have been using Flixzone for since december myself :)
@programmingforfun4158
@programmingforfun4158 Жыл бұрын
Great video. I was exactly looking for this
@codewithbubb
@codewithbubb Жыл бұрын
Great! Hope you found it useful?
@Tiago-rw8cx
@Tiago-rw8cx 2 жыл бұрын
How can you have the terminal lines: "at Context..." "at process.." in light grey font color? They show in aggressive red on my default VScode window and it is so much worse. Thank you very much!
@FiveFourThor
@FiveFourThor 2 жыл бұрын
Amazing tutorial!
@codewithbubb
@codewithbubb 2 жыл бұрын
Thanks very much!!
@dp8134
@dp8134 Жыл бұрын
This is insanely clear and concise. You make life easy!
@codewithbubb
@codewithbubb Жыл бұрын
Cheers! Hope you found it useful!
@prajwalwarad3318
@prajwalwarad3318 3 жыл бұрын
how can we write test cases for function that does not return any thing? I mean in this video you have used add and sub, so they return either a+b or a-b. What if i dont return a+b and rather store it in a variable. How do you prepare test for that?
@codewithbubb
@codewithbubb 3 жыл бұрын
Depends on what the function does - is it calling other functions (you can check that they get called by spying / stubbing them) or does it create a side-effect elsewhere (you can check that the update has gone through).
@dankipruto3727
@dankipruto3727 4 ай бұрын
I am getting this error when I import the expect function from chai and run it "Error [ERR_REQUIRE_ESM]: require() of ES Module /home/dan/Documents/mocha/node_modules/chai/chai.js from /home/dan/Documents/mocha/test/indexTest.js not supported. Instead change the require of chai.js in /home/dan/Documents/mocha/test/indexTest.js to a dynamic import() which is available in all CommonJS modules. at Object. (/home/dan/Documents/mocha/test/indexTest.js:2:18)". How can I fix it? Thank you
@ryanford516
@ryanford516 22 күн бұрын
In order to fix this, you've got to do two things. Firstly, replace the lines const assert = require('assert'); and const { expect } = require('chai'); with just one which is import { assert, expect } from 'chai'; Next open up your package.json file and pop the following line just above your "scripts" property: "type": "module"
@raymondyoo5461
@raymondyoo5461 2 жыл бұрын
I have a question. When we use chai, do people usually do [ expect().to.be() ] instead of [ assert.equal(,) ] ?? I just want to know which is the more common style. Thanks, I’m really grateful for a great tutorial for javascript testing.
@y_thedreamer95
@y_thedreamer95 2 жыл бұрын
that's the basic case, i suppose expect has more methods to compare than assert.
@mariacamilafarelo2741
@mariacamilafarelo2741 2 жыл бұрын
how can we type-check the code?
@dannnsss8034
@dannnsss8034 2 жыл бұрын
Nice intro! And SMH, I've been calling 'nyc' (nice) as 'New York City'). Where do enzyme and sinon come into play?
@codewithbubb
@codewithbubb 2 жыл бұрын
I think 'New York City' is much cooler 😀Sinon is for stubbing which we go through in this video (kzbin.info/www/bejne/nXWXhJpphNqAn6M) Enzyme is for React projects.
@nologicparodays9501
@nologicparodays9501 2 жыл бұрын
When I write npm run test it gives an error saying mocha is not defined as internal or external command.HElp??
@anthonydinino2321
@anthonydinino2321 2 жыл бұрын
Make sure you got Node installed. then run "npm init -y". Then you can run "npm install --save-dev mocha". Then create a folder in the root of your project (same level as package.json) called "test." Then go into package.json and change the value of the "test" property to "mocha test". Then run "npm run test" into your command line. You should see a failing test. Then just follow the video from there.
@Karimdelacreme
@Karimdelacreme 3 жыл бұрын
very good video
@codewithbubb
@codewithbubb 3 жыл бұрын
Thanks for your support!
@aarondz789
@aarondz789 3 жыл бұрын
谢谢分享.thanks.
@codewithbubb
@codewithbubb 3 жыл бұрын
You're welcome 😀
@isaacciliaattard5625
@isaacciliaattard5625 5 ай бұрын
Legend!
@codewithbubb
@codewithbubb 4 ай бұрын
Thanks very much!
@adevtv05
@adevtv05 8 ай бұрын
See shorts about positive test using mocha for rest API here: kzbin.inforWEB0NXjoBU
@_.sunnyraj._
@_.sunnyraj._ 3 жыл бұрын
Is the stripe tutorial not ready yet ?
@codewithbubb
@codewithbubb 3 жыл бұрын
It's on my todo list, been busy at work recently so apologies about the delay😢
@_.sunnyraj._
@_.sunnyraj._ 3 жыл бұрын
@@codewithbubb I have completed that leave it now
@danialabd7473
@danialabd7473 9 ай бұрын
Would've been nice if the video wasn't only on 360p! hehe
Unit Testing with Mocha and Chai [Part 2/2]
18:06
Code With Bubb
Рет қаралды 10 М.
Test-Driven Development // Fun TDD Introduction with JavaScript
12:55
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 49 МЛН
Hot Ball ASMR #asmr #asmrsounds #satisfying #relaxing #satisfyingvideo
00:19
Oddly Satisfying
Рет қаралды 12 МЛН
1 класс vs 11 класс (неаккуратность)
01:00
Introduction To Testing In JavaScript With Jest
13:57
Web Dev Simplified
Рет қаралды 487 М.
Testing Node Server with Jest and Supertest
11:45
Sam Meech-Ward
Рет қаралды 109 М.
The TSConfig Cheat Sheet
5:36
Matt Pocock
Рет қаралды 31 М.
Next.js with React Testing Library, Jest, TypeScript
25:04
Dave Gray
Рет қаралды 51 М.
ExpressJS - Jest & Unit Testing
39:12
Anson the Developer
Рет қаралды 22 М.
Mock vs Spy in Testing with Jest: Which is Better?
25:12
Dev tips by MoHo
Рет қаралды 8 М.
Cypress Complete Beginners Masterclass 1 | Step by Step | Raghav Pal |
1:20:54
Automation Step by Step
Рет қаралды 183 М.
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 139 М.
Introduction to Jest Testing | JavaScript Unit Tests
25:30
Dave Gray
Рет қаралды 27 М.
programming projects that taught me how to code
9:49
isak
Рет қаралды 247 М.
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 49 МЛН