Js Basics Logging Lab

Learning Goals

  • Practice using console.log to debug in JavaScript

Working through these tests

  • driver - This test is already working for you. When 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, you'll see: invokes console.log() with the string "this code was called". Then, the next line says spy should 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 fully knowing. Just understand that this spy thing helps us test that console.log is called, and what the arguments are. 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, this test should be passing.
    • In 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 there really is no other great way to test the use of console.log(). Because the return value of the console.log() method is undefined, we can't really check the return value of your code. Furthermore, console.log affects your browser's console, which humans can read easily, but computers can't. So we just test to make sure you made use of this function using 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.


Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us