- Practice using
console.log()
Another JavaScript lab! In this lab, we are practicing our use of console.log
to debug in JavaScript.
Remember the workflow:
- Run
learn
. - Read the errors; vocalize what they're asking you to do.
- Write code; repeat steps 1 and 2 often until a test passes.
- Repeat as needed for further tests.
- Run
learn submit
when finished!
driver
- This test is already working for you. If 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, the tests ask thatinvokes console.log() with the string "this code was called"
. Then, the next line saysspy
to 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 knowing. Just understand that here we are using thisspy
thing to test thatconsole.log
is called, and what arguments it is passed to. 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 thevariable
driver toconsole.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 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, 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.
View Logging Lab on Learn.co and start learning to code for free.