This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Add manifest files to npm packages using npm init

2:00 JavaScript lesson by

We can create a package.json automatically by running npm init and answering the questions it gives. This becomes the “manifest” file for our package; it is used to populate the published npm page as well as to help the package manager to know how to install the package in consuming projects.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We can create a package.json automatically by running npm init and answering the questions it gives. This becomes the “manifest” file for our package; it is used to populate the published npm page as well as to help the package manager to know how to install the package in consuming projects.

Let's run MPM install jest and then save that to our development dependencies. Now, if we look inside of our package .json, inside of the devDependencies, we have this new package listed. In our scripts, we can use that. We have this test script defined, which is just a default echo statement.

Let's change that to run the jest binary. When we run the jest script, this test command is going to run. Let's go to our source directory, and we want to test this file here. I'm going to create an index.test.js file, and this .jest .js extension is one of the patterns that jest recognizes by default.

Inside the file, we could import our module, which we'll call sensitive words, from the sibling file, because the sibling file is in the current directory and it's named with an index.js, the single dot will find it. Now, we can write our test. We'll say test, then, we'll need a description of the test, as well as a function to run.

For the description, for right now, let's check that it says hello world. We can say, expect that when sensitive words is run, that it gives us hello world.

From another pin, I can say MPM test, and our test is failing. If we scroll up, it says that it's not saying hello world. It expected hello world and we got hello universe.

Let's go check our source file. Our source file is saying hello universe currently, let's change that to hello world. Save the file. Now, we can re-run our test script. It's passing. Let's go back to our package .json and underneath our test script, let's make a new script called test watch.

We can have these run the test script and pass it via watch flag, which is the option that jest provides for automatically watching test. We should be able to run MPM run test watch, and our tests are passing, as well as continuing to run.

If we go back to our test file and change world to be universe again. Now if we run this, our test will be failing and we can't open up our source file and change the world to be universe, as well. Save that, and now, our tests are passing again.

We can run our tests in watch mode and have our source files and tests all working together.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?