Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
npm has a number of built in tasks. One example is test. In this video we'll take a look at how to set up npm to run our tests.
Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
There are a number of built-in
tasks that npm can run for you.
0:00
The most common is test.
0:04
You're often encouraged to
install test frameworks and
0:07
build systems globally with the -g flag.
0:10
But that can be a little overkill if
you're working on different projects with
0:14
potentially different versions.
0:17
Let's install our depth dependences
now by running npm install.
0:20
The install command installs all npm
modules that are part of your project.
0:32
In other words, third party modules
that your project depends on.
0:38
But it also installs
development dependencies,
0:42
that is,
modules that you use during development.
0:45
Such modules that can compile Sass,
combine multiple JavaScript files,
0:48
or to test your code
to make sure it works.
0:54
In this example, I've installed
the JavaScript testing framework Mocha for
0:59
testing my JavaScript.
1:03
How do you run Mocha here?
1:06
In your node_modules folder,
there's a hidden directory, .bin.
1:08
And in there, there are command
line executables like Mocha.
1:15
So you can type, node_modules/.bin/mocha.
1:20
And it runs.
1:26
However, you can run executables from
npm without typing in this long command.
1:28
Let's update the test task in our
scripts object with the Mocha command.
1:35
This will run any Mocha
test in the project.
1:50
What weβve done here is
created a test task.
1:54
Now you can run it by typing npm run test.
1:58
Npm can run any binary or
command line application.
2:08
See how much easier this is?
2:12
Now that we donβt need to remember or
type that full path in to Mocha.
2:14
We can make it even
easier by ditching run.
2:20
Because test is a built-in task,
you can leave out the run,
2:23
and simply type npm test.
2:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up