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