GithubHelp home page GithubHelp logo

js-basics-logging-lab-houston-web-082718's Introduction

JavaScript Logging Lab

Objectives

  1. Practice using console.log()

Introduction

Another JavaScript lab! In this lab, we are practicing our use of console.log to debug in JavaScript.

Remember the workflow:

  1. Run learn.
  2. Read the errors; vocalize what they're asking you to do.
  3. Write code; repeat steps 1 and 2 often until a test passes.
  4. Repeat as needed for further tests.
  5. Run learn submit when finished!

Working through these tests

  • driver - This test is already working for you. If you look inside the it function you will see that the first test expects a variable named driver to have a value of bob. When you open up the index.js file, you will see that this already is declared in the first line.
  • console.log() - Let's take a look at the second describe function call inside of test/indexTest.js. Here, you can see that the tests inside of here are asking for a few things. Let's take them in turn.
    • If you look at the first it function call, the tests ask that invokes console.log() with the string "this code was called". Then, the next line says spy to be calledWithExactly the string this code was called. To pass this test, we can write console.log('this code was called') inside of our index.js file. Yes, I know we are a little confused. What is a spy? It's ok if you walk away from the course not knowing. Just understand that here we are using this spy thing to test that console.log is called, and what arguments it is passed to. So whenever you see expect(spy.calledWithExactly('this code was called')).to.be.true what we really mean is that we expect console.log to be called with the argument this code was called. For more details on what the spy is doing, see our "note about spies" below.
    • In the second to last it function call in test/indexTest.js we test that console.log is called with our driver variable. This is a task you may find yourself doing a lot. Using console.log to see the value of a variable. If you pass the variable driver to console.log your test should be passing. More to the point, if you open up your browser's console (by pressing cmd+shift+c on a mac), you can see your corresponding data logged.
    • The final it function call in this lab, the test asks you to practice passing multiple arguments to console.log. The first argument is the driver variable, and the second argument should be the string is the driver variable name.

A note about spies

You might often see errors like the ones above: "Uncaught error: spy was not called". Spies are little bits of code that keep track of whether or not they were called. Here, we are ensuring that console.log was called - so if you see that error it means that you are not properly calling that console.log function.

Why are we using spies all of a sudden? Open up the test/indexTest.js file and let's see how they work, and learn why we are using this feature. Underneath the code describe('console.log', function () { ..., you can see the code spy = sinon.spy(console, 'log');. This asks our tests to watch the console.log function. Later on in an it block, we can ensure that console.log is called, and can see what arguments it is called with. We are using the spy because, well how else can we tell you are calling this function? The function returns undefined after all, so we can't really check the return value of your code, and console.log affects your browser's console, which we can't easily read. So we just test to make sure you made use of this function, and to do so we use a spy. If this was confusing to you, it is not a core JavaScript topic nor is it a prerequisite to learning more with JavaScript, so you can happily move ahead.

Resources

View Logging Lab on Learn.co and start learning to code for free.

js-basics-logging-lab-houston-web-082718's People

Contributors

gj avatar jeffkatzy avatar pianistps avatar

Watchers

 avatar James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar A. Perez avatar Nicole Kroese  avatar  avatar Lore Dirick avatar Nicolas Marcora avatar Lisa Jiang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.