Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A nice feature of Mocha is that we can outline tests that need to be written before we’re ready to write them. So when you have an idea for a test, but you aren’t yet sure what it will look like, you can still communicate your idea to other developers and get started.
Resources
Video review
- To mark a test as “pending”, do not add a function as a second argument
- You can also mark tests as "pending" by typing an
x
in front of the pending block, likexdescribe()
orxit()
- Adding an
x
in front of thedescribe()
block marks all of the specs inside as "pending"
-
0:00
In some ways being a programmer is more like being a writer
-
0:03
than being an engineer.
-
0:05
It requires a lot of creativity and you can get stuck a lot waiting for
-
0:09
inspiration to strike.
-
0:11
A nice feature of Mocha is that we can outline tests
-
0:14
that need to be written still before we're ready to write them.
-
0:17
So when you have an idea for a test but you don't have time to figure it out, or
-
0:22
you aren't yet sure what it will look like,
-
0:24
you can still communicate your idea to other people and get started.
-
0:28
Let me show you an example.
-
0:30
In order to play a game, it would be nice to have a function
-
0:33
that checks whether the game is over after each turn.
-
0:36
That way, every time I make a guess, the game engine will check and
-
0:39
see whether I've struck the last ship in one or
-
0:42
whether the next player should start their turn.
-
0:44
I know I need that, but I'm not ready to think about how it works yet,
-
0:48
not even to write the tests.
-
0:50
So I can write a pending test spec to keep track of those tests I have to
-
0:53
write later.
-
0:54
Inside the test folder I'll create a new test suite file called gametest.js
-
1:00
since this suite will focus on functions pertaining to the flow of the game.
-
1:08
So first I'll import chai at the top of the new file so
-
1:12
that we can use it here as an expectation.
-
1:23
And now I'll set up the fundamental describe block for the suite.
-
1:36
And as the first argument I'll write, GAME INSTANCE FUNCTIONS in all caps.
-
1:42
This is only here to make the output nicer for me.
-
1:47
I'll call the function I'm testing here, checkGameStatus.
-
1:50
So I'll create a new test suite for it.
-
2:08
Now, a test spec that I know I should write for
-
2:11
this function confirms that it should tell me when the game is over.
-
2:15
So I'll create a new spec that says, it should tell me when the game is over.
-
2:26
This is a pending test, so in order to mark this test as pending,
-
2:30
I do not add a function as the second argument.
-
2:34
The it block is only written with the first argument describing the test.
-
2:39
And it doesn't contain any expectations yet.
-
2:42
So now if I go over to the console and run npm test,
-
2:50
It showed me that I now have 15 tests passing and one pending.
-
2:56
Notice the pending test is in a different color so they're easier to see.
-
3:01
In my console the test is light blue.
-
3:08
This is really useful as a reminder to myself and
-
3:10
others that some tests need to be written still.
-
3:13
It's like having an outline for my outline.
-
3:15
It communicates some information for getting started.
-
3:22
I can also mark tests as pending by typing an x in front of the pending block.
-
3:27
Like, xdescribe,
-
3:33
or, xit.
-
3:44
For example, if I add a regular test spec to my current suite,
-
3:50
even though I've added a function as a second argument,
-
3:55
I can add x in front of the describe block, and
-
4:00
all of the specs inside will be marked as pending.
-
4:16
I prefer leaving out a callback to using xdescribe,
-
4:20
because I don't have to delete anything later.
-
4:23
I just have to continue writing the test spec as I normally would.
-
4:27
You might see this done both ways, so
-
4:29
feel free to use whichever method you like better.
You need to sign up for Treehouse in order to download course files.
Sign up