100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Advanced JS Questions And Answers Latest Update $15.49   Add to cart

Exam (elaborations)

Advanced JS Questions And Answers Latest Update

 3 views  0 purchase

Advanced JS Questions And Answers Latest Update

Preview 4 out of 105  pages

  • October 21, 2024
  • 105
  • 2024/2025
  • Exam (elaborations)
  • Questions & answers
All documents for this subject (17)
avatar-seller
Schoolflix
Solution 2024/2025
Pepper




Advanced JS Questions And Answers Latest
Update

Why write test? ANS✔✔ As you begin writing more complicated functions
and larger applications, you're bound to make mistakes. Everyone does it,
even professional programmers. When your programs grow they can
become more difficult to reason about, and as hard as you may try it's
impossible to predict every bug in your program. Fixing bugs also has a
cost, as it can be quite easy for one fix to introduce bugs in other parts of
your application.



Is there any way to avoid our programs becoming more brittle and difficult
to maintain as they grow in complexity? Yes! The solution to this problem
lies in testing our code as thoroughly as possible.

, Solution 2024/2025
Pepper
This makes it easier to protect against bugs, and to ensure that you don't
introduce new bugs in your code as you add new features or rewrite old
ones.



What is Jasmine? ANS✔✔ a test runner and expectation/assertion library. We
will be using it to run all of our tests. A test runner is a tool that is
responsible for running tests that you write and logging the results of the
tests for you to see.



Main functions we'll see the most with tests are: ANS✔✔ describe, it, and
expect



describe ANS✔✔ this function is given to us by jasmine and it is what we use
to organize our tests. You can think of a describe function like talking to
someone and telling them "let me describe ____ to you." Very often when
you're writing unit tests, you'll have one describe block per function you're
testing



it ANS✔✔ this function lives inside of describe functions. Inside of these it
functions we make our expectations. Each it function corresponds to a test;
if one of our expectations inside of the it function isn't met, the test fails.



let's just look at an example written in plain old English. Here's how you
might scaffold some tests to check whether a planet in our solar system is
Earth:



describe "Earth"

it "is round"

it "is the third planet from the sun"

, Solution 2024/2025
Pepper
it "is the densest of all the planets"



expect ANS✔✔ this is a function given to us by jasmine. When combined
with describe and it, we can write tests that looks something like this:



describe "Earth"

it "is round"

expect (earth.isRound).toEqual(true)

it "is the third planet from the sun"

expect(earth.numberFromSun).toEqual(3)

it "is the densest of all the planets"

expect(earth.density).toBeGreaterThan(5.51)



Example test code written in JS using jasmine: ANS✔✔ var earth = {

isRound: true,

numberFromSun: 3,

density: 5.51

};



describe("Earth", function() {

it("is round", function() {

expect(earth.isRound).toEqual(true);

});

, Solution 2024/2025
Pepper
it("is the third planet from the sun", function() {

expect(earth.numberFromSun).toEqual(3);

});



it("is the densest of all the planets", function() {

expect(earth.density).toBeGreaterThan(5.5);

});

});



Note the syntax here: both describe and it take a string as their first
parameter, and a callback as the second. The callback to a describe
typically consists of several it functions. Inside of each it function is where
we write our expectations.



Running tests in the browser ANS✔✔ setup:



<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Our First Jasmine Tests</title>

<link
href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine.css"
rel="stylesheet" />

</head>

<body>

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Schoolflix. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $15.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

75632 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$15.49
  • (0)
  Add to cart