- In this section, we are going to see
how to organize multiple tests into suites.
A suite is a collection of tests.
It helps you organize your thought process
when you have a large code base.
The examples we're going to look at
in this section are example two.
So here we are, we are in lesson one, example two.
I have some JavaScript code that contains several functions.
I have a test file, which organizes tests into suites,
collections of tests, and a package.json file
that just defines some handy scripts to run the tests.
If I open this folder in Visual Studio Code, here we are.
We have the test file,
which we'll look at in detail in a moment.
We have the code, and as you can see here,
I've got some simple functions
to add and subtract two numbers, to multiply and divide,
and I've exported all of them.
This is how you export multiple symbols
in a common JS module.
You set the module exports property to be an object.
In that object, you specify the names of the functions
that you want to export.
So effectively, an object is being exported
and that object has an add function, a subtract function,
a multiply and divide.
Okay, so here we go then.
In our example,
here's a code that we're going to test.
We have an add function, subtract, multiply, divide,
and we export from this module,
from the script, we export all those four functions.
Fair enough.
Okay, so in our test file
we are going to have two different suites.
A suite as a group of tests.
You define a suite using the describe function.
The describe function creates a suite
and it takes two parameters.
The first parameter is the name of the suite.
It's just a free format to text in.
It describes the purpose of the tests in this first suite
and the tests in this second suite.
And the second parameter to the described function
is a Lam function or Lambda or arrow function.
And in here, you define all the tests
for additive operations.
So I'm going to have a bunch of tests in this suite
to test the add and subtract functions.
I've got another suite here which defines
my multiplicative functions.
So in this Lambda, I'll implement my tests
for the multiply and divide operations.
Let's take a look at that file, "example.test.js".
Let's have a look at that.
So here it is.
First of all, I need to import those functions.
So I'm going to import from my example.js file, from here.
Remember, in my example JS, I exported an object.
Okay, so bear that in mind.
What I then import, this basically is an object
that contains a property
called add, subtract, multiply, and divide, right?
So that's an object.
Okay, so here's my first suite.
In this first suite,
which has this textual descriptor,
I have the body of my suite contains two tests.
The first test for the add function,
and the second test for the subtract function.
So just one thing that's worth noting here.
This is an object that contains the imported stuff.
So in that object, I called form that object,
I invoke the add function.
So that is basically invoking the add function
that was exported here.
Okay, so whatever object name you give it there,
you would use that as the prefix.
So I'm calling the add function to add two numbers.
My math suggests that the results should be 30,
and then I'm gonna call the subtract function.
So these are functions, the add and subtract functions.
I've put them under this banner.
That's my suite of additive functions.
One of the reasons for grouping tests
into a suite is that you can run suites separately.
We'll see that in a moment.
You could just run all the functions in this suite
or you can just run all the functions in this suite.
It's a way of collectively deciding what tests to run.
In my second suite, so it has a textual descriptor,
"multiplicative functions."
The parameter here is just a function.
It's just a grouping mechanism, really, inside that group.
I've got another two functions to test,
that the multiply function works,
and that the divide function also works.
Okay, so there we go.
So what's new here is the described function.
This creates a suite.
I've got one suite for my additive functions,
and another suite for my multiplicative functions.
Okay, so that's fair enough.
Now, if you want to, of course, you can run all the tests.
You can say "yarn test," or you can just say "npm run test"
and that will run all the tests,
in all the suites, in all the files in the current folder.
So we'll do that first.
Got a command window here opened into example two.
Okay, so if I just do an npm run test,
it'll run all the tests.
So there were four tests altogether,
two tests in the additive suite
and two tests in the multiplicative suite.
And it runs them all, as we shall see.
Right, so it's detected that file
and it displays the name of the first suite,
"additive functions."
In that suite, it found those two functions.
So remember, what we see being displayed here
is the name of the suite,
and what we see being displayed here,
the test that passed, "adds numbers correctly,"
that's the descriptor for the first test, and it worked.
And the descriptor for the second test,
"subtracts numbers correctly,"
obviously, that's this function here.
And it basically executed all the tests in the file.
So the tests in the additive suite all ran correctly
and they have a green tick there.
And the tests in the multiplicative suite also all ran.
So all together, we had four tests that succeeded.
Okay, so that's marvelous.
But you know, if you have a large code base,
you might have hundreds of tests and dozens of suites,
and you don't want to run all the tests every time,
because it might take too long.
So if you want to,
you can just choose to run tests from a particular suite.
And that's the purpose of suites.
You can choose which tests to run
instead of running the whole thing.
So if you want to run just one suite, you can do this.
You can say "yarn test"
and the -t option followed by a partial suite descriptor.
This is part of the name of my first suite.
Okay, so I could run that command, yarn test -t.
And it looks for any suites that have a name
that partially matches that.
And it'll run the tests in that suite.
You can do the same using npm
and you have to be a bit more careful.
When you say npm run, to run the tests,
you have to have a double dash.
The double dash syntax, when using npm,
it indicates that these parameters here, -t additive,
these parameters are destined for the test command.
Okay? If you didn't put the --
then these parameters would be passed into npm run.
But the -- is like a separator.
It says, no, no, these parameters
aren't part of an npm thing.
These parameters are part of the test command,
so that these parameters will be passed into the test.
And the test will just run the additive suite.
So I'm gonna do that now.
So npm run, I'm gonna run the test script
as defined in the package.json file, remember?
And then, parameters into the test command.
I'm going to pass the -t option
to say which tests I want to run.
All tests in any suite that partially matches
this name, "additive."
Okay, so let's see what that displays.
Quite exciting, isn't it?
So it detected my test file.
It realized that this suite matched that partial name.
So it ran the tests in that suite.
Notice, it also detected the other suite,
but it skipped those tests
because we told it just to run the additive tests.
Okay, so define the tests, grouping tests in suites,
it helps you organizationally
but it also helps you when you're running the tests.
You can choose which tests to run
so that you can get your work done more quickly.