- Practice using
console.log
to debug in JavaScript
driver
- This test is already working for you. When you look inside theit
function, you will see that the first test expects a variable nameddriver
to have a value ofbob
. When you open up theindex.js
file, you will see that this already is declared in the first line.console.log()
- Let's take a look at the seconddescribe
function call inside oftest/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 saysspy
should becalledWithExactly
the stringthis code was called
. To pass this test, we can writeconsole.log('this code was called')
inside of ourindex.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 thisspy
thing helps us test thatconsole.log
is called, and what the arguments are. So whenever you seeexpect(spy.calledWithExactly('this code was called')).to.be.true
what we really mean is that we expectconsole.log
to be called with the argumentthis code was called
. For more details on what thespy
is doing, see our "note about spies" below. - In the second to last
it
function call intest/indexTest.js
, we test thatconsole.log
is called with ourdriver
variable. This is a task you may find yourself doing a lot: usingconsole.log
to see the value of a variable. If you pass the variabledriver
toconsole.log
, this test should be passing. - In the final
it
function call in this lab, the test asks you to practice passing multiple arguments toconsole.log
. The first argument is thedriver
variable, and the second argument should be the stringis the driver variable name
.
- If you look at the first
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.